The difference between when ruby curries and when arguments are required and when ruby is not.

Asked 2 years ago, Updated 2 years ago, 68 views

I can't tell the difference between when you curie and when you don't.
If you use to_proc from a symbol, take the variable length argument, so
Is that why I need arguments?

head:022>f=proc {|x,y|x+y}
 =>#<Proc:0x007fbb311a0088@(irb):22> 
head:023>f.curry(2).call(1).call(2)
 =>3 
head:024>f.curry.call(1).call(2)
 =>3 
head:025>f.curry.call(1,2)
 =>3 
head:026>: +.to_proc.curry.call(2,3)
 =>5 
head:027>: +.to_proc.curry(2).call(2).call(3)
 =>5 
head:028>:+.to_proc.curry.call(2).call(3)#Otherwise, error

ruby

2022-09-30 21:00

1 Answers

If I use to_proc from the symbol, I will take variable length arguments, so do I need them?

It's not necessarily necessary.
Calied Proc is evaluated "when sufficient number of arguments are given".The difference is whether you want to specify a sufficient number of arguments as an argument (arity) at cry or let them decide for themselves at call.

#The number of sufficient arguments is determined to be two if:
f2 = proc { | x, y, * z | x + y + z.inject(:+)}
f2.curry.call(1,2,3,4)
=>10
f2.curry.call(1.call(2,3,4)
=>10
f2.curry.call(1).call(2).call(3,4)# Evaluated when you give two
TypeError: nil can't be covered into Fixnum

I didn't read the ruby source, but the behavior suggests that you are judging by the Proc#arity value.

f=proc {|x,y|x+y}
f.arity#=>2...two real arguments

f2 = proc { | x, y, * z | x + y + z.inject(:+)}
f2.arity#=>-3...two real arguments and one variable length argument

—+.to_proc.arity#=>-1 ... 0 real and 1 variable length argument


2022-09-30 21:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.