5 ms·
I'm not sure I get the lambda issue. If you want a > 1 line lambda why not just define a function? def a(x): def b(): print x re
by mclin 17y ago
I'm not sure I get the lambda issue. If you want a > 1 line lambda why not just define a function?
def a(x):
def b():
print x
return b
Not sure what a prototypical ruby usecase would be.
- gcampbell 17y agoThe other side of the argument: why support anonymous functions, but restrict them to one line? That seems highly arbitrary.
- kevingadd 17y agoNot one line, one statement.
- fuzzyman 17y agoNope - not arbitrary, it is because of the indentation scoping rules. Try coming up with a syntax for statement based lambdas that isn't ugly as heck.
- jashkenas 17y agoFirst, links for context. Guido's take: http://www.artima.com/weblogs/viewpost.jsp?thread=147358 http://www.artima.com/weblogs/viewpost.jsp?thread=147358 Arcieri's take: http://www.unlimitednovelty.com/2009/03/indentation-sensitivity-post-mortem.html http://www.unlimitednovelty.com/2009/03/indentation-sensitiv... It's not hard if we disallow statements, and make everything into an expression that returns a value -- something that Ruby does right. The value of a block of statements is just the value of the last statement in the block. So, whether you call it a function, or a method, or a lambda, or a block, whitespace with multi-line expressions can be made to work. The commonly-cited lambda-with-multiple-prints example, in CoffeeScript: count: -> print 'one' print 'two' print 'three'
- brutimus 17y agoI don't understand the argument either.. I use multi-line lambdas with ifs and forloops all the time. f = lambda x:( x if ( x**2 > 10 or x**2 < 5 ) else x**3 )
- cturner 17y agoI don't think you can assign variables in these blocks, nor do sequential statements. Imagine you wanted to reduce a list, and then do something on it. Print statements don't work either.
- 0x44 17y agoThe prototypical ruby case would be: def some_method(x, &block) yield x end then to call it: some_method "hey" { |v| print v } That might not look like much, but it allows you to create methods that can extend the language expressively and then use them without extra crufty syntax or sigils obstructing their meaning.
- mclin 17y ago> some_method "hey" { |v| print v } I'm sure ruby is great once you're initiated, but despite >10 years of programming and having learned basic, c, pascal, lisp, java, c++, python, perl, bash, php and javascript, I have no idea what code does. Don't mean to harsh, just an observation.
- 0x44 17y agoI made that method absurdly generic, how about: get "/hi" do |url| print url end ?