You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
var compose = function () {
var args = arguments;
var start = args.length - 1;
return function () {
var i = start;
var result = args[start].apply(this, arguments);
while (i--) result = args[i].call(this, result);
return result;
};
};
compose
/**
*/
const add = (a, b) => {
return a + b;
}
add.bind(null, 1).bind(null, 2)(); // => 3
add.bind(null, 1).bind(null, 3)(); // => 4
add.bind(null, 1)(2); // => 3
/*
// var curry = function (fn) {
// var args = [].slice.call(arguments, 1);
// return function () {
// var newArgs = args.concat([].slice.call(arguments, 1));
// return fn.apply(this, newArgs);
// };
// };
// var curry = (fn, ...args) => (...arguments) => fn.apply(this, [...args, ...arguments])
var curry = (fn, ...args) => (...arguments) => fn(...args, ...arguments);
var addCurry = curry(add, 1, 2);
var addCurry1 = curry(add);
var addCurry2 = curry(add, 1);
console.log(addCurry());
console.log(addCurry1(1, 2));
console.log(addCurry2(2));
// addCurry() //=>3
/***
*/
console.log(curry1(add)(1)(2));
/**
*/
// 虽然 ajax 这个函数非常通用,但在重复调用的时候参数冗余
ajax('POST', 'www.test.com', "name=kevin")
ajax('POST', 'www.test2.com', "name=kevin")
ajax('POST', 'www.test3.com', "name=kevin")
// 利用 curry
var ajaxCurry = curry(ajax);
// 以 POST 类型请求数据
var post = ajaxCurry('POST');
post('www.test1.com', "name=kevin");
post('www.test2.com', "name=kevin");
// 以 POST 类型请求来自于 www.test.com 的数据
var postFromTest = post('www.test.com');
postFromTest("name=kevin");
postFromTest("name=xxx");
/**
*/
/**
*/
const g = n => n + 1;
const f = n => n * 2;
const e = n => n - 1;
const gData = g(10);
const fData = f(gData)
console.log('fData', fData);
/**
*/
var compose = function (f, g) {
return function (x) {
return f(g(x));
};
};
console.log(compose(f, g)(10));
console.log(compose(g, compose(f, e))(10));
// 这是两个函数组装,如果多个呢? compose(d, compose(c, compose(b, a))) 太难读了
// compose(d, c, b, a)
/**
// 从右 => 左
console.log(compose(g, f)(10));
console.log(compose(g, f, e)(10));
/* 注意这当中的回调函数 (prev, curr) => prev + curr
*/
/**
/**
*/
async await使用顺序执行 待优化
函数式
备注:函数很单纯,不要存放任何的变量,然后通过compose 或者 pipe 来进行合并一起 本质还是reduce filter
The text was updated successfully, but these errors were encountered: