Re:Is Lisp Still Unique? Or at Least Different? (2002)
by zmmmmm on Tuesday August 21, 2018 @02:48AM
(#17807566)
If you want you can do very functional stuff in Groovy. Here's an example of function composition:
f = { it * 3 }
g = { it + 4 }
h = f << g
h(2)
18
The reason this is not particularly common usage is not because you can't do it, but because most people don't find it very intuitive.
Re:Is Lisp Still Unique? Or at Least Different? (2002)
by vorg on Tuesday August 21, 2018 @08:54AM
(#17809098)
The unintuitive part is the choice of operator `<<` instead of `>>`. in your example `f << g` means do `g` first (i.e. 2 + 4 = 6) then do `f` on the result (i.e. 6 * 3 = 18). I would have expected `f >> g` for this behavior.
Re:Is Lisp Still Unique? Or at Least Different? (2002)
by dllthomas on Friday August 24, 2018 @10:47PM
(#17839265)
I really don't follow your expectation here. "Data moves in the direction of the arrows" seems natural to me. Can you unpack it at all? I'd like to understand where you're coming from.
Re:Is Lisp Still Unique? Or at Least Different? (2002)
by zmmmmm on Tuesday August 21, 2018 @05:58PM
(#17814209)
It's following the semantics of wider use of `<<`` in groovy which denotes forwarding contents of one object into another, eg: adding to a list
list << x
or writing to a stream: new File('test.txt') << 'hello world'
So if you read it as "forward the result of g into f" then it makes a little more sense.But I agree, it's still not very intuitive and for readability I would normally just write:
h = { f(g(it)) }
Which I think is far clearer. But that's partly an argument against functional programming style itself which would ordinarily see function composition as more idiomatic.
Also the code you wrote won't pass production code review at any decent Java shop.