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

实现函数柯理化 #78

Open
huabingtao opened this issue Jul 10, 2020 · 1 comment
Open

实现函数柯理化 #78

huabingtao opened this issue Jul 10, 2020 · 1 comment
Assignees
Labels
javaScript This is something about JavaScript work this is a works

Comments

@huabingtao
Copy link
Owner

No description provided.

@huabingtao huabingtao added javaScript This is something about JavaScript work this is a works labels Jul 10, 2020
@huabingtao huabingtao self-assigned this Jul 10, 2020
@huabingtao
Copy link
Owner Author

实现函数柯理化

// 木易杨
function currying(fn, length) {
  length = length || fn.length; 	// 注释 1
  return function (...args) {			// 注释 2
    return args.length >= length	// 注释 3
    	? fn.apply(this, args)			// 注释 4
      : currying(fn.bind(this, ...args), length - args.length) // 注释 5
  }
}

// Test
const fn = currying(function(a, b, c) {
    console.log([a, b, c]);
});

fn("a", "b", "c") // ["a", "b", "c"]
fn("a", "b")("c") // ["a", "b", "c"]
fn("a")("b")("c") // ["a", "b", "c"]
fn("a")("b", "c") // ["a", "b", "c"]

@huabingtao huabingtao moved this to 已完成 in JavaScript Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
javaScript This is something about JavaScript work this is a works
Projects
Status: 已完成
Development

No branches or pull requests

1 participant