Point Free

这是一种函数编码模式: 把数据处理的过程定义成和数据无关的合成运算,不需要用到数据参数,只是简单合成运算步骤,但需要定义一些辅助的基本运算函数

for example:  采用了lodash的fp

1 const fp = require("lodash/fp");
2 
3 const f = fp.flowRight(fp.join('-'), fp.map(fp.toLower), fp.split(' '));
4 
5 console.log(f("NEVER SAY DIE"))
fp

 这是打印结果

 

 

非Point Free模式

1 //Hello World => hello_world
2 function f(word) {
3   return word.toLowerCase().replace(/\s+/g, "_");
4 }
5 const result = f("Hello World");
6 console.log(result);
View Code

结果

 

 

采用Point Free

1 //Hello World => hello_world
2 const func = fp.flowRight(fp.replace(/\s+/g, "_"), fp.toLower);
3 
4 const value = func("Hello World");
5 console.log(value);
View Code

 

结果

 

 

Point Free其实就是是一种函数的组合

 

下面来试一下, 把“ world wild web" 转换成 ‘W. W. W‘

1 const newFunc = fp.flowRight(
2   fp.join(". "),
3   fp.map(fp.flowRight(fp.first, fp.toUpper)),
4   fp.split(" ")
5 );
6 
7 console.log(newFunc("world wild web"));
View Code

 

posted @ 2021-02-09 14:30  芋圆小宝  阅读(105)  评论(0)    收藏  举报