-
Notifications
You must be signed in to change notification settings - Fork 0
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
Currying & 柯里化 #63
Labels
Comments
Ad Blocker Checkerhttp://www.ruanyifeng.com/blog/checker.js function checker() {
var img = document.querySelector('img[src^="http://www.ruanyifeng.com/blog/images"]');
if (img && window.getComputedStyle(img).display === 'none'){
var sponsor = document.querySelector('.entry-sponsor');
var prompt = document.createElement('div');
prompt.style = 'border: 1px solid #c6c6c6;border-radius: 4px;background-color: #f5f2f0;padding: 15px; font-size: 16px;';
prompt.innerHTML = '<p>您使用了广告拦截器,导致本站内容无法显示。</p><p>请将 www.ruanyifeng.com 加入白名单,解除广告屏蔽后,刷新页面,谢谢。</p>';
sponsor.parentNode.replaceChild(prompt, sponsor);
document.querySelector('#main-content').innerHTML = '';
}
}
setTimeout(checker, 1000); |
Repository owner
locked and limited conversation to collaborators
Dec 4, 2018
The function composition of
type F = (x: number) => number;
function compose(functions: F[]): F {
// cury // 柯里化
return function(x) {
//
}
};
/**
* const fn = compose([x => x + 1, x => 2 * x])
* fn(4) // 9
*/ solutions ✅type F = (x: number) => number;
function compose(functions: F[]): F {
// cury // 柯里化
return function(x) {
for(let i = 0; i < functions.length; i++) {
// let func = functions[functions.length - 1 - i];
// x = func(x);
x = functions[functions.length - 1 - i](x);
}
return x;
}
};
/**
* const fn = compose([x => x + 1, x => 2 * x])
* fn(4) // 9
*/ 🚀 // Typescript
type F = (x: number) => number;
function compose(functions: F[]): F {
return function(x) {
return functions.reverse().reduce((acc, current) => acc = current(acc), x)
}
};
/**
* const fn = compose([x => x + 1, x => 2 * x])
* fn(4) // 9
*/ |
https://www.cnblogs.com/xgqfrms/p/11342509.html#4744377 "use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-11-17
* @modified
*
* @description
* @description
* @difficulty Easy Medium Hard
* @complexity O(n)
* @time O(n)
* @augments
* @example
* @link https://javascript.info/currying-partials#advanced-curry-implementation
* @solutions
*
* @best_solutions
*
*/
const log = console.log;
function curry(func) {
return function curried(...args) {
// log(`func.length =`, func.length)
log(`args.length =`, args.length, args)
if (args.length >= func.length) {
// 以获取全部的参数, 将参数传递给 callback function
// apply 接受一个参数数组,改变方法绑定的对象后,方法会立即执行 callback function ✅
// call 接受一个参数列表,改变方法绑定的对象后,方法会立即执行 callback function ✅
// bind 仅仅改变方法绑定的对象,但方法不会立即执行,需要手动调用方法才会执行 callback function ✅
// bind, apply, call改变 this 指向
// ⚠️ 箭头函数,不适用需要使用改变 this 绑定的场景
return func.apply(this, args);
} else {
return function(...args2) {
log(`args2.length =`, args2.length, args2)
// 分步获取参数,并将参数拼接到一起
// 闭包,内部函数可以访问到外部函数的所有属性
return curried.apply(this, args.concat(args2));
}
}
};
}
function sum(a, b, c) {
return a + b + c;
}
// ??? @decorator 装饰器-设计模式
const curriedSum = curry(sum);
log(`\n`, curriedSum(1, 2, 3));
// 6, still callable normally
log(`\n`, curriedSum(1)(2, 3));
// 6, currying of 1st arg
log(`\n`, curriedSum(1)(2)(3));
// 6, full currying
/*
// error
const test1 = isValid(`((}}`);
// ok
const test2 = isValid(`()[]{}`);
log(`❌test =`, test1 ? `✅` : `❌`);
log(`✅test ok =`, test2 ? `✅` : `❌`);
*/
??? |
// bug ❌
// obj = {a: 1, f: () => console.log(`a = ${this.a}`), };
obj = {a: 1, f: function (){ console.log(`a = ${this.a}`)}, };
// {a: 1, f: ƒ}
obj.f()
// a = 1
obj2 = {a: 2}
// {a: 2}
f = obj.f.bind(obj2);
// ƒ (){ console.log(`a = ${this.a}`)}
f();
// a = 2
obj.f.call(obj2);
// a = 2
obj.f.apply(obj2);
// a = 2
/*
apply 接受一个参数数组,改变方法绑定的对象后,方法会立即执行 callback function ✅
call 接受一个参数列表,改变方法绑定的对象后,方法会立即执行 callback function ✅
bind 仅仅改变方法绑定的对象,但方法不会立即执行,需要手动调用方法才会执行 callback function ✅
bind, apply, call改变 this 指向
⚠️ 箭头函数,不适用需要使用改变 this 绑定的场景
*/ |
function curry(func) {
return function curried(...args) {
if (args.length >= func.length) {
return func.apply(this, args);
} else {
return function(...args2) {
return curried.apply(this, args.concat(args2));
}
}
};
} https://javascript.info/currying-partials#advanced-curry-implementation https://zh.javascript.info/currying-partials#gao-ji-ke-li-hua-shi-xian const curry = fn =>
curried = (...args) =>
args.length === fn.length
? fn(...args)
: (arg) => curried(...args, arg) |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
Currying & 柯里化
https://www.cnblogs.com/xgqfrms/p/14025942.html
https://www.cnblogs.com/xgqfrms/p/5730527.html
https://en.wikipedia.org/wiki/Currying
https://zh.wikipedia.org/wiki/%E6%9F%AF%E9%87%8C%E5%8C%96
https://zh.wikipedia.org/wiki/柯里化
The text was updated successfully, but these errors were encountered: