What needs to happen for an η-reduction to suddenly appear in a ruby?

Suppose I have def f(a); a + 1; end def f(a); a + 1; end def f(a); a + 1; end and apply f to the list, it usually looks like [1,2,3].map { |x| fx } [1,2,3].map { |x| fx } .

And when I want to use η-reduction ( (λx.Mx) → M ), I have an error here:

 > [1,2,3].map f ArgumentError: wrong number of arguments (0 for 1) from (irb):2:in `f' from (irb):4 from /usr/bin/irb:12:in `<main>' 

What needs to happen for it to become valid Ruby syntax?

+4
source share
3 answers
 [1, 2, 3].map &method(:f) => [2, 3, 4] 

See Object#method .

+3
source

The card is waiting for a block. You could do something like this if you want:

 f = lambda { |a| a+1 } [1,2,3].map &f 

This way you pass the block as an argument. To pass blocks through arguments, you need to use the and sign.

+2
source

You can also do this:

 print (1..3).collect{|i| i+1} #=> [2, 3, 4] 

Alternatively, this too:

 (1..3).map(&:succ) #=> [2, 3, 4] 
0
source

All Articles