-
Notifications
You must be signed in to change notification settings - Fork 555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Where causes code to be called twice #1181
Comments
Interesting! Sequences are evaluated lazily, which seems to be involved. var bar = [1, 2].map{|x| Foo.new(x) }.toList.where{|x| x.num > 0}.toList |
This is to be expected, since the filtering done by where involve accessing
the value, in addition of the acces for flattening to list. Effectively it
doubles the access to the value, resulting in the effect you see.
You can either do an intermediate flattening as proposed (or any other form
of memoization), or if possible rearrange the order of map so that they
happens last in the chain.
|
I thought this was surprising too - as mhermier says it is because of the way If you have two where clauses then it prints 'Creating foo' six times
An option would be to have Lines 168 to 182 in c2a75f1
|
Take the code
If run, it prints "Creating foo" 4 times. Intuitively I expect it to call only 2 times, and I tested the same thing using C# linq and it is called 2 times.
Is this possible to fix with how WhereSequence works?
At the very least it's something to look out for in case your construction is resource intensive.
The text was updated successfully, but these errors were encountered: