Generalised Curry - Javascript

烈酒焚心 提交于 2020-01-14 19:05:27

问题


While reading an article on implementation of a generalised curry in Javascript, I stumbled upon this piece of code.

function curry(fn) {
  return (...xs) => {
    if (xs.length === 0) {
      throw Error('EMPTY INVOCATION');
    }
    if (xs.length >= fn.length) {
      return fn(...xs);
    }
    return curry(fn.bind(null, ...xs));
  };
}

I am unable to grok the part of the explanation which states

We create a copy of fn that has the first k arguments bound (partially applied) and pass it to curry as the next fn, with its reduced arity of N - k.

How does the arity of fn reduced to N-k in the subsequent calls? A bound function with k arguments should have an arity of k right?


回答1:


A bound function returns a function with partially applied arguments, so f(a, b, c) becomes f.bind(null, a).bind(null, b)(c).



来源:https://stackoverflow.com/questions/50616087/generalised-curry-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!