News for hackers. Stuff that matters.
Re:Kotlin 1.1: What’s coming in the standard library by nemo1618 on Tuesday January 24, 2017 @08:19PM (#13477563)
"onEach" looks helpful. I actually just added such a function to my own Go extension project, although I named it "tee". The benefit is that when you have a long chain of map -> filter -> reduce, you can insert a tee as a sort of "porthole" into the current state at any location in the chain. Very handy for things like debugging and logging.
Re:Kotlin 1.1: What’s coming in the standard library by khc on Tuesday January 24, 2017 @09:19PM (#13477863)
it looks exactly like Java's peek(), not sure why they don't use the same naming.
Re:Kotlin 1.1: What’s coming in the standard library by jorgemf on Tuesday January 24, 2017 @09:31PM (#13477927)
It is not like java peek at all (or I am missing what peek do your refer to).

Peek is to get the reference of the first element. OnEach makes a lazy operation for every element on the sequence. I would say that onEach is more like map but lazy.

Re:Kotlin 1.1: What’s coming in the standard library by kevinherron on Tuesday January 24, 2017 @11:39PM (#13478577)
It's just forEach that returns the collection/sequence instead of returning Unit. Don't overthink it. It's not like map.
Re:Kotlin 1.1: What’s coming in the standard library by jorgemf on Wednesday January 25, 2017 @12:04AM (#13478693)
> 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.

Re:Kotlin 1.1: What’s coming in the standard library by kevinherron on Wednesday January 25, 2017 @12:25AM (#13478807)
No, 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.

Re:Kotlin 1.1: What’s coming in the standard library by jorgemf on Wednesday January 25, 2017 @04:58AM (#13480122)
You 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.

Re:Kotlin 1.1: What’s coming in the standard library by agumonkey on Wednesday January 25, 2017 @02:20AM (#13479430)
May I describe it as an effectful identity ?
Re:Kotlin 1.1: What’s coming in the standard library by jorgemf on Wednesday January 25, 2017 @04:53AM (#13480096)
It is not an identity because you can modify the internal state of the objects. And it is also lazy:

arrayOf("A","B","C").onEach{ print("onEach $it")}.forEach{ print("forEach $it")}

prints:

onEach A forEach A onEach B forEach B onEach C forEach C

Re:Kotlin 1.1: What’s coming in the standard library by agumonkey on Wednesday January 25, 2017 @05:35AM (#13480282)
thanks
Re:Kotlin 1.1: What’s coming in the standard library by khc on Saturday January 28, 2017 @02:41AM (#13505392)
Java's stream has a peek(), which also makes a lazy operation for every element in the stream.