3 ms·
Properly used, it's just a short-hand for small expressions, where adding formally declared parameters adds 50% or more bulk to the code. {|a, b| a + b} ver
by kazinator 8y ago
Properly used, it's just a short-hand for small expressions, where adding formally declared parameters adds 50% or more bulk to the code.
{|a, b| a + b} versus {@1 + @2}
type of thing. Note also that a and b are not more informative than @1 and @2.
Both these examples convey one message: "I'm a function that adds two arguments".
Here is a useful power: omit the smaller numbers:
# TXR Lisp:
5> (mapcar (aret @3) '((a n 1 10) (b m 2 20)))
(1 2)
6> (mapcar (aret @2) '((a n 1 10) (b m 2 20)))
(n m)
aret: a)pply list to function, which ret)urns an expression.
Simply by using @20, we get a twenty-argument function with 19 don't-cares.
- ricardobeat 8y agoPerl is still available if you want to use it. Naming arguments “a” and “b” is widely considered bad practice either way, of course it’s not more informative.
- philwelch 8y agoSo you would prefer {|dividend, divisor| dividend/divisor} over {@1/@2}?
- rajangdavis 8y agoOn a team, I would prefer the former over the latter. As a single contributor that's trying to hack together an idea, the second option is not so bad; however, it will probably need to be changed far off in the future. I think named block parameters are cool in the sense that they enable some expressiveness but bad in the sense that they lose some readability. I won't know how I truly feel until I have to maintain/read a large codebase where they are used.
- philwelch 8y agoHow do you feel about the Groovy/Kotlin style that would translate (roughly speaking): [1,2,3].map{|n| n * 2} to: [1,2,3].map{it * 2} ? It's not generalizable to multi-argument lambdas, but multi-argument lambdas are the ones that are more important to name. (e.g. with reduce, {|memo, it| memo += it})
- rajangdavis 8y agoI don't feel strongly one way or the other to be honest. It looks nice but it's not a syntax that I have been wanting or needing. If it enables people to express themselves better through code, then I'm all for it. I think as far as Ruby goes, I don't like the idea of introducing a new abstraction for something that already has a solution; however, I wouldn't villify someone for using it unless they wrote something that took a large number of arguments.
- vorg 8y agoThe underscore is fairly pleasant to read: [1,2,3].map{_*2} I think that's what Scala has. For multi-args, perhaps: [1,2,3].reduce{_1+=_2}
- vidarh 8y agoUnderscore is very commonly used to signifiy "I don't care about this argument" in Ruby, so at least on it's own it'd be confusing, and _1 is a valid local variable name, so would risk breaking existing code, though I've personally not seen it used, so maybe it'd be an acceptable risk.
- kazinator 8y ago(lambda (a b) (/ a b)) is not "a large code base".
- kazinator 8y agoSpeaking of "over", of course that word should be used instead of the cryptic one-letter / name: begin anonymous function arguments are dividend and divisor return dividend over divisor end There; no cryptic one-character anything. No curly braces, no commas, no operators.
- deleted 8y ago[deleted]
- kazinator 8y ago{by whom}