JavaScript関数式プログラミングと関数コリック化(進級一)
4016 ワード
最近JavaScript関数式のプログラミングを勉強しています.先の神様を知るための教程を読みました.
ノート
https://zhuanlan.zhihu.com/p/21714695
ノート
/* ------------------------------------------------------------------------------------------------------------
*/
// , , ,
//eg:
var arr = [1, 2, 3, 4, 5]
//array.slice , , ,
var xs = arr.slice(0, 3) //[1,2,3]
console.log(xs) //[1,2,3]
var xs2 = arr.slice(0, 3)
console.log(xs2) //[1,2,3]
/* */
//array.splice , , , 。
arr.splice(0, 3)
console.log(arr) //[4,5]
arr.splice(0, 3)
console.log(arr) //[]
/* , */
// , slice , splice
// ? eg:
var min = 18;
//
var checkage = age => age > min;
// -- > var checkage = function(age){return age>min}
console.log(checkage(20))
//
var checkage = age => age > 18;
console.log(checkage(20))
// ,checkage age, Min, ,
// , , 。
// , checkage 18 , ,
// ,
/* ------------------------------------------------------------------------------------------------------------
*/
/*
(curry) , ,
var add = (x,y) => x+y :
*/
//es5
var add = function (x) {
return function (y) {
return x+y
}
}
//es6
var add = x => (y => x + y);
var add2 = add(2)
console.log(add2(2)) //4
var add200 = add(200)
console.log(add200(200)) //400
// checkage
//es5
var checkage = function (min) {
return function (age) {
return age > min
}
}
var checkage = min => (age => age > min);
var checkage18 = checkage(18)
console.log(checkage18(20)) //true
// , ,
// , , “ ”, :
/*
import { curry } from 'lodash';
//
var match = curry((reg, str) => str.match(reg));
// --> var match = function curry(reg,str){ return str.match(reg) }
var filter = curry((f, arr) => arr.filter(f));
//
var haveSpace = match(/\s+/g);
haveSpace("ffffffff");
//=>null
haveSpace("a b");
//=>[" "]
filter(haveSpace, ["abcdefg", "Hello World"]);
//=>["Hello world"]
*/
/* ------------------------------------------------------------------------------------------------------------
*/
// , :
// h(g(f(s)))
// , ,
// :
var compose = function (f, g) {
return function (x) {
return f(g(x))
}
}
//
var compose = (f, g) => (x => f(g(x)));
var add1 = x => x + 1;
var mul5 = x => x * 5;
var res = compose(mul5, add1)(2);
console.log(res) //15
/*
:
add1 : x = function (x) {return x+1}
mul5 : x = function (x) {return x*5}
f(g(x)) == > f(g(2)) == > f(3) == > return 3*5
}
*/
/* compose , , , N */
var first = arr => arr[0];
var reverse = arr => arr.reverse;
var last = compose(first, reverse);
last([1, 2, 3, 4, 5])
console.log(last([1, 2, 3, 4, 5])) //5
// last = > compose(f(g(x)))
// -->
/* :
compose(
f => function(arr){return arr[0]}
g => function(arr){return arr.reverse}
x => [1,2,3,4,5]
g(x)=>[5,4,3,2,1]
f(g(x))=>5
return f(g(x)) = > return 5
)
last => compose(first,reverse) => 5
var last = function compose(fisrt,reverse){.....}
*/
var str2 = "Hello"
var strc = str2 => str2.concat("???")
console.log(strc(str2))
// : H
var one = str => str.concat("!");
var two = str => str.includes('H');
var strLast = compose(two, one)
strLast("Hello World")
console.log(strLast("Hello World")) //true
参考:知っていますか?Starkwanghttps://zhuanlan.zhihu.com/p/21714695