天天看点

函数式编程-PointFree篇

函数式编程-PointFree篇
概念
PrintFree其实是一种编程风格,我们可以把数据处理的过程定义与数据无关的合成运算,不需要用到代表数据的哪个参数
只需要把运算步骤合成到一起,,在使用这种模式之前需要定义一些辅助的基本运算函数
           
优点
1、不需要指明处理的数据
2、只需要合成运算过程
3、需要定义一些辅助的基本运算函数
           
案例演示1
let str1 = 'Hello World'

//非pointFree模式

function fn1(world) {
    return world.toLowerCase().replace(/\s+/g, "_")
}

console.log(fn1(str1)); //hello_world

//pointFree模式

const fn2 = fp.flowRight(fp.replace(/\s+/g, '_'), fp.toLower);

console.log(fn2(str1)); //hello_world
           
案例演示2
let str2 = 'world wild web'

//实现效果W. W. W

const fn3 = fp.flowRight(fp.join(". "), fp.map(fp.flowRight(fp.first, fp.toUpper)), fp.split(' '))

console.log(fn3(str2));//W. W. W

           

谢谢观看,如有不足敬请指教

继续阅读