You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First of all, this creates a Set every single time, on each element of our collection.
This is not true. Set is created only once and then its contains function is passes as an argument to filter method. This function is then invoked per each element of someCollection.
In this case a wrapping function is be created around Set(a,b,c).contains, something like x:Int => Set(a,b,c).contains(x) and this function is then passed as an argument for filter. Set is created inside wrapping function, so it's creation is performed once per element of someCollection.
Also, in this item it is worth to mention eagerness of monadic operations on scala collections.
In provided example:
This is not true. Set is created only once and then its contains function is passes as an argument to filter method. This function is then invoked per each element of someCollection.
You're right. Will fix.
Easy way (though no obvious) is to add .view at the beginning of the chain. This makes whole chain lazy.
Views are considered deprecated. Iterable is better.
https://github.com/alexandru/scala-best-practices/blob/master/sections/3-architecture.md#34-should-be-mindful-of-the-garbage-collector
This is not true. Set is created only once and then its
contains
function is passes as an argument tofilter
method. This function is then invoked per each element ofsomeCollection
.Compare to similar code snippet:
In this case a wrapping function is be created around
Set(a,b,c).contains
, something likex:Int => Set(a,b,c).contains(x)
and this function is then passed as an argument forfilter
.Set
is created inside wrapping function, so it's creation is performed once per element ofsomeCollection
.Also, in this item it is worth to mention eagerness of monadic operations on scala collections.
In provided example:
by default each step produces new collection (opposite to laziness of C#'s linq), which produces lots of garbage when chain is long.
Easy way (though no obvious) is to add
.view
at the beginning of the chain. This makes whole chain lazy.The text was updated successfully, but these errors were encountered: