How to write this LINQ expression in another .SelectMany() form ?
var result =
from a in numbersA
where a < 3
from b in numbersB
where b < 5
select new { a, b };
?
var result = numbersA.Where(x => x < 3).Select.. ?
This is a rough translation of what the compiler would do:
var result = numbersA.Where(a => a < 3)
.SelectMany(a => numbersB, (a, b) => new { a, b })
.Where(z => z.b < 5)
.Select(z => new { z.a, z.b });
Now you can write this more efficiently as:
var result = numbersA.Where(a => a < 3)
.SelectMany(a => numbersB.Where(b => b < 5),
(a, b) => new { a, b });
... but that's not what the compiler would do. It's not clear whether your aim is to see what the compiler does, or to just write a query.
Something like
var result = numbersA.Where(a => a < 3).SelectMany(a =>
numbersB.Where(b => b < 5).Select(b => new { a, b }));
Note that it maybe be more efficient to filter numbersB once only:
var filteredB = numbersB.Where(b => b < 5).ToArray();
var result = numbersA.Where(a => a < 3).SelectMany(a =>
filteredB.Select(b => new { a, b }));
Related
Scala Code:
trait RNG {
}
type Rand[+A] = RNG => (A, RNG)
def map[A,B](s: Rand[A])(f: A => B): Rand[B] = rng => {
val (a, rng2) = s(rng)
(f(a), rng2)
}
def flatMap[A,B](f: Rand[A])(g: A => Rand[B]): Rand[B] = rng => {
val (a, r1) = f(rng)
g(a)(r1)
}
def map2_1[A,B,C](ra: Rand[A], rb: Rand[B])(f: (A, B) => C): Rand[C] = flatMap(ra) (a => map(rb)(b => f(a,b)))
def map2_2[A,B,C](ra: Rand[A], rb: Rand[B])(f: (A, B) => C): Rand[C] = for {
a <- ra
b <- rb } yield f(a,b)
Problem: map2_1 - good. map2_2 - not good: Errors: Cannot resolve symbol flatMap, Cannot resolve symbol map
Help please.
map2_2 is not compiled in Scala 2.13
I expected for-comprehensions to work.
I have this:
a = {'x' => 3}
b = {'x': 3}
c = {x: 3}
d = {:x => 3}
e = {:'x' => 3}
So, I have that b = c = d = e = {:x => 3}, meanwhile a = {"x" => 3} but a.class == b.class.
I don't understand what the difference is between a and the rest of variables.
In b,c,d, and e, the key is a Symbol.
In a, the key is a String.
a = { 'x' => 3 } #=> { "x" => 3 }
b = { 'x': 3 } #=> { :x => 3 }
c = { x: 3 } #=> { :x => 3 }
d = { :x => 3 } #=> { :x => 3 }
e = { :'x' => 3 } #=> { :x => 3 }
Your variable a hash has "x" key as a string, while other variables have that key as symbol.
Calling class on an object in Ruby returns its class, in your example it is Hash. In other words, the constructor of all hash instances, such as {x: 3} is Hash object.
There is a significant difference between String and Symbol classes in ruby:
explanation on SO;
good article on the topic.
By convention, all but the very first hash notations cast keys to the Symbol instance, while the first one uses the key (String instance in this particular case) as is. (To be more precise: b and c cast key to the Symbol instance, d and e do not cast anything, but keys given in these cases are Symbol instances already.)
Since ('x' == :x) == false, a hash differs from the latters.
lets say i got 3 hashes:
hash1={
a => 1,
b => 2,
c => 4
}
hash2={
b => 1,
c => 3
}
hash3={
a => 2,
b => 1,
c => 3,
d => 4
}
I want to average stuff to a new hash according to keys, so new hash will be
result={a=>1.5,b=>4/3,c=>10/3,d=>4}
meaning if key not exists in one hash we don't count it as 0.
is there an elegant way?
result = {}
[hash1, hash2, hash3].each{|h| h.each{|k, v| (result[k] ||= []).push(v)}}
result.each{|k, v| result[k] = v.inject(:+).to_f/v.length}
result # =>
# {
# a => 1.5,
# b => 1.3333333333333333,
# c => 3.3333333333333335,
# d => 4.0
# }
I have two collections that I want to intersect, and perform a sum operation on matching elements.
For example the collections are (in pseudo code):
col1 = { {"A", 5}, {"B", 3}, {"C", 2} }
col2 = { {"B", 1}, {"C", 8}, {"D", 6} }
and the desired result is:
intersection = { {"B", 4}, {"C", 10} }
I know how to use an IEqualityComparer to match the elements on their name, but how to sum the values while doing the intersection?
EDIT:
The starting collections haven't two items with the same name.
Let's say your input data looks like this:
IEnumerable<Tuple<string, int>> firstSequence = ..., secondSequence = ...;
If the strings are unique in each sequence (i.e there can be no more than a single {"A", XXX} in either sequence) you can join like this:
var query = from tuple1 in firstSequence
join tuple2 in secondSequence on tuple1.Item1 equals tuple2.Item1
select Tuple.Create(tuple1.Item1, tuple1.Item2 + tuple2.Item2);
You might also want to consider using a group by, which would be more appropriate if this uniqueness doesn't hold:
var query = from tuple in firstSequence.Concat(secondSequence)
group tuple.Item2 by tuple.Item1 into g
select Tuple.Create(g.Key, g.Sum());
If neither is what you want, please clarify your requirements more precisely.
EDIT: After your clarification that these are dictionaries - your existing solution is perfectly fine. Here's another alternative with join:
var joined = from kvp1 in dict1
join kvp2 in dict2 on kvp1.Key equals kvp2.Key
select new { kvp1.Key, Value = kvp1.Value + kvp2.Value };
var result = joined.ToDictionary(t => t.Key, t => t.Value);
or in fluent syntax:
var result = dict1.Join(dict2,
kvp => kvp.Key,
kvp => kvp.Key,
(kvp1, kvp2) => new { kvp1.Key, Value = kvp1.Value + kvp2.Value })
.ToDictionary(a => a.Key, a => a.Value);
This will give the result, but there are some caveats. It does an union of the two collections and then it groups them by letter. So if, for example, col1 contained two A elements, it would sum them together and, because now they are 2 A, it would return them.
var col1 = new[] { new { L = "A", N = 5 }, new { L = "B", N = 3 }, new { L = "C", N = 2 } };
var col2 = new[] { new { L = "B", N = 1 }, new { L = "C", N = 8 }, new { L = "D", N = 6 } };
var res = col1.Concat(col2)
.GroupBy(p => p.L)
.Where(p => p.Count() > 1)
.Select(p => new { L = p.Key, N = p.Sum(q => q.N) })
.ToArray();
The best I came up with until now is (my collections are actually Dictionary<string, int> instances):
var intersectingKeys = col1.Keys.Intersect(col2.Keys);
var intersection = intersectingKeys
.ToDictionary(key => key, key => col1[key] + col2[key]);
I'm not sure if it will perform well, at least is it readable.
If your intersection algorithm will result in anonymous type, i.e. ...Select(new { Key = key, Value = value}) then you can easily sum it
result.Sum(e => e.Value);
If you want to sum the "while" doing the intersection, add the value to the accumulator value when adding to the result set.
How can i right this in method Expression!
var query =
from l in list where l.Key == "M"
select new { Value = l, Length = l.Length };
You want to turn it into a sequence of extension method calls?
var query = list.Where(l => l.Key == "M")
.Select(l => new { Value = l, Length = l.Length });