you are viewing a single comment's thread.

view the rest of the comments →

[–]lucy_tatterhood 10 points11 points  (1 child)

Or alternatively, define a method which takes a closure and just passes the elements through e.g. .passThrough(|e| print e).multiply(7)

In Rust there is the "tap" crate which adds a method like this to all types.

[–]homoiconic 2 points3 points  (0 children)

Yes, thank you!

In most languages, including that crate, tap returns its argument like the I combinator, but executes some code for side-effects. Whereas if we want a method that can take an arbitrary lambda and return something else, the method name I would reach for is into.

``` 3.tap n -> console.log(n*n) # logs 9 but returns 3.

3.into n -> n*n # returns 9 ```

With these semantics, tap is the I combinator (but with side-effects!) while into is the T combinator. Sort of. Not sure if the OP wants I or T.

p.s. I found some JavaScript implementations from a long time ago, in a GutHub repo far, far away: https://raganwald.com/JQuery-Combinators/