Re:"Useless syntax sugar": Numbered block parameters in Ruby
by jonahx on Wednesday October 11, 2023 @02:56PM
(#37848758)
> ghci> filter (\x -> (length x) > 2) ["a","bb","ccc","dddd"]
["ccc","dddd"]... I find the Haskell syntax concise and clear...
> ["a","bb","ccc","dddd"].filter &(2.method(:<) << :length.to_proc)
I can't imagine a stronger case for GP's thesis. The point-free ruby rewrite is so baroque and needlessly circuitous... the extra variable is a far lesser evil, and more concise:
["a","bb","ccc","dddd"].filter { |x| x.length > 2 }
Re:"Useless syntax sugar": Numbered block parameters in Ruby
by inopinatus on Thursday October 12, 2023 @12:15AM
(#37853426)
my instinct is
["a","bb","ccc","dddd"].grep /.../
but then, I'm a recovering perl hacker
Re:"Useless syntax sugar": Numbered block parameters in Ruby
by jonahx on Thursday October 12, 2023 @01:25AM
(#37853718)
Love it!
Re:"Useless syntax sugar": Numbered block parameters in Ruby
by dragonwriter on Thursday October 12, 2023 @03:53AM
(#37854556)
Yeah, mirroring the Haskell code, while possible in Ruby, is definitely not the best Ruby-aware approach, either for concision or clarity. The composability is available, but the problems where it is the best solution are different.
Re:"Useless syntax sugar": Numbered block parameters in Ruby
by inopinatus on Wednesday October 11, 2023 @05:17PM
(#37850364)
risky move choosing illustrative examples that have specific alternative solutions:
["a","bb","ccc","dddd"].grep /.../
=> ["ccc", "dddd"]
anyone who wants to suggest that a regex is not a function can fight me
Re:"Useless syntax sugar": Numbered block parameters in Ruby
by inopinatus on Thursday October 12, 2023 @09:47PM
(#37865589)
Even without patching the standard library:
irb> [[1,2],[3,4]].each_with_object(:*).map(&:reduce)
=> [2, 12]
Given the mechanics of this I wouldn't claim it meets a strict definition of point-free, but that's an iterated application of operator-like methods omitting any explicit named/positional argument references.
Ruby doesn't have functions, but any one paramter block (to which any one parameter callable, and some other things, can be converted with an &) can be passed as the block parameter to map. And, yes, you can use function composition and similar operations to build up more complex callables from existing ones without an explicit new block or lambda.