Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[js] 实现一个自动化柯里化方法 #24

Open
VaJoy opened this issue Sep 5, 2017 · 4 comments
Open

[js] 实现一个自动化柯里化方法 #24

VaJoy opened this issue Sep 5, 2017 · 4 comments

Comments

@VaJoy
Copy link
Member

VaJoy commented Sep 5, 2017

//TODO - 实现一个自动化柯里化方法 curry

const curriedSum = curry((x, y, z) => x + y + z);

curriedSum(1, 2)(3);   // 6
curriedSum(1, 2)(3);   // 6
curriedSum(1)(2)(3);   // 6
curriedSum(1, 2, 3);   // 6

思考下如何实现这个 curry 函数,不建议参考别人代码

@zpdf
Copy link

zpdf commented Sep 8, 2017

function curry (fn){ const length = fn.length; return function curriedFn(...args){ if(args.length>=length){ return fn(...args) }else{ return curriedFn.bind(null,...args) } } }

@VaJoy
Copy link
Member Author

VaJoy commented Oct 29, 2017

贴个coco的解法思路,挺抖机灵的:

function add () {
    console.log('进入add');
    var args = Array.prototype.slice.call(arguments);
 
    var fn = function () {
        var arg_fn = Array.prototype.slice.call(arguments);
        console.log('调用fn');
        return add.apply(null, args.concat(arg_fn));
    }
 
    fn.valueOf = function () {
        console.log('调用valueOf');
        return args.reduce(function(a, b) {
            return a + b;
        })
    }
 
    return fn;
}

@Jiasm
Copy link
Member

Jiasm commented Apr 13, 2018

之前写过的一个:

/**
 * 柯里化的一种实现方式
 */

function currying (...arg) {
  val = arg.reduce((cur, old) => cur + old, 0)
  return Object.assign(currying.bind(this, val), {
    valueOf () {
      return val
    },
    toString () {
      return val
    },
    toLocaleString () {
      return val
    }
  })
}
var a = currying(1)(2)

// 因为console.log实现的问题。。所以必须在前边添加一个+来确保能够得到正确的输出
console.log(+a, +a(4), +a(5, 6))

或者一个较为通用的实现方法:https://github.com/Jiasm/notebook/blob/master/currying.js

@uinz
Copy link

uinz commented Jul 25, 2018

function curry(fn) {
    const _inner = (...args1) => args1.length < fn.length
        ? (...args2) => _inner(...args1, ...args2)
        : fn(...args1)
        
    return _inner
}

const curriedSum = curry((x, y, z) => x + y + z);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants