Re:Destroy All Ifs – A Perspective from Functional Programming
by kmiroslav on Saturday July 16, 2016 @09:38PM
(#12108656)
Groovy and Kotlin use the implicit "it" parameter for lambdas that take just one parameter, which is very convenient:
[ Parent
| on HN ]
Re:Destroy All Ifs – A Perspective from Functional Programming
by SatvikBeri on Sunday July 17, 2016 @05:59AM
(#12109608)
Scala allows underscores, and sequential underscores refer to the next element, so you can do e.g.
list(1, 2, 3, 4).reduce(_ + _) == 10
Re:Destroy All Ifs – A Perspective from Functional Programming
by kmiroslav on Sunday July 17, 2016 @10:21AM
(#12110239)
Doesn't that make the parameter anonymous, though? Can you println that _ and see the value of the current element?
Re:Destroy All Ifs – A Perspective from Functional Programming
by vorg on Sunday July 17, 2016 @05:22PM
(#12111898)
Use a function that prints then returns its parameter:
list(1, 2, 3, 4).reduce{print(_) + _} == 10
Use it if you or your language hasn't defined such a function: list(1, 2, 3, 4).reduce{it:= _; println(it); it + _} == 10
In fact, any name for it will do.
Re:Destroy All Ifs – A Perspective from Functional Programming
by SatvikBeri on Sunday July 17, 2016 @01:52PM
(#12110985)
Yeah, that's the tradeoff–gain the ability to work with multiple parameters but lose the ability to reuse a single one.
Re:Destroy All Ifs – A Perspective from Functional Programming
by eru on Sunday July 17, 2016 @02:27AM
(#12109236)
And Haskell uses operator sections: (==0) would be similar to { it == 0 }. Alas, the section syntax get a bit cumbersome when you want to compose two or more of them, like in this translation of your example:
filter ((0==) . (%2)) [1, 2, 3, 4]