Currying a proc with keyword arguments in Ruby

后端 未结 4 968
刺人心
刺人心 2021-01-17 22:44

Say I have a generic Proc, Lambda or method which takes an optional second argument:

pow = -> (base, exp: 2) { base         


        
4条回答
  •  伪装坚强ぢ
    2021-01-17 23:31

    • curry does not work with keyword arguments. A curried function is getting one parameter at a time, which is conceptually incompatible with "any order is fine" keyword arguments.
    • curry must know the exact arity. If you just call curry with no arguments, it will ignore any optionals (in case of pow = -> (base, exp=2) { base**exp }, same as curry(1)). Use curry(2) to force both parameters. A curried function can't know an optional parameter is following, and read the future to determine if it should execute or return a curried continuation.

提交回复
热议问题