Re:Announcing Building Git
by kccqzy on Tuesday April 09, 2019 @04:03PM
(#19618418)
Ruby has blocks. Python does not. And Ruby blocks are very extensively used.
[ Parent
| on HN ]
Re:Announcing Building Git
by localhostdotdev on Tuesday April 09, 2019 @07:59PM
(#19620283)
blocks are the same as passing a function or a lambda in python:
[1, 2, 3].map { |i| i + 1 }
[i + 1 for i in [1, 2, 3]]
def plus(i):
return i + 1
map(plus, [1, 2, 3])
map(lambda i: i + 1, [1, 2, 3])Far from it. Ruby blocks aren't really lambdas or anonymous functions. For example when you use return inside a block, it's not just the code in the block is being returned, but the whole enclosing function. And, you don't really "call" a block but yield to it. It's not even an object that you can inspect. It's syntax. It's really a new kind of control structure. Ruby blocks have few equivalents in other languages.
Re:Announcing Building Git
by localhostdotdev on Tuesday April 09, 2019 @10:33PM
(#19621281)
def f(&block)
block.class
end
f { } # => Proc
f(&-> { }) # => Proc
f(&lambda { }) # => Proc
f(&:something) # => Proc
lambda { }.class # => Proc (I think you get the idea)
method(:f).class # => Method
lambda { |i| i + 1 }.call(10) # => 11
you don't typically return in a block, there is implicit return. you would do "next" or "break" depending on what you want to achieve. as far as I know that's the main difference between procs and methods.(not that anybody would do what I do, most people just use blocks, some people use yield, few people use the &block argument, but that's exactly the same)
Sound a lot like inlined lambdas in Kotlin. Lambdas can be passed with the same block-like syntax, and when they're marked as "inline" you can return from the enclosing function from within the lambda (because the lambda will have its body inlined by the compiler).