3 ms·
It's just forEach that returns the collection/sequence instead of returning Unit. Don't overthink it. It's not like map.
by kevinherron 10y ago
It's just forEach that returns the collection/sequence instead of returning Unit. Don't overthink it. It's not like map.
- jorgemf 10y ago> On iterables it behaves like forEach but also returns the iterable instance further. And on sequences it returns a wrapping sequence, which applies the given action lazily as the elements are being iterated. like forEach and also returns the iterable instance or like map, it is the same. For me the key thing is that onEach applies lazily for sequences.
- kevinherron 10y agoNo, it's not like map. Map builds a new collection based on the function you provide it. At the end of a map call you end up with a potentially transformed collection instance.
- jorgemf 10y agoYou should consider change your tone when you talk to people. It is quite rude. It is not a map and it is not a forEach, that is why they created it. > At the end of a map call you end up with a potentially transformed collection instance The difference between onEach and map is that map returns a new collection that can have different type of elements. But onEach can modify the internal state of the objects when applied. Also onEach is lazy and map is not lazy in kotlin (as far as I know). Why did I make the comparison with map and not with forEach? Because forEach is basically a translation of a for and map is a functional function and onEach it is also a functional function. You can agree or disagree with my way of trying to explain things, but being rude in your comments saying you are not right (when I know) and them providing another solution that you also know it isn't right, feels like a troll comment.
- agumonkey 10y agoMay I describe it as an effectful identity ?