天天看點

函數式程式設計-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

           

謝謝觀看,如有不足敬請指教

繼續閱讀