fpEs 用 JavaScript 进行函数式编程 项目简介
fpEs 是一个 JavaScript 库,可让你使用 EcmaScript(Javascript) 进行函数式编程。作者表示,之所以开发这个项目是因为自己需要使用 Optional & Rx-like & PubSub 这些功能的一些特性,如果同时引入它们,项目又会变得很重。因此他的这个实现只包含核心功能,并且使用起来更加清晰。用法引入整个库import fpEs from 'fpEs';该库包含了 5 个模块,可以单独引用它们:Facades:maybemonadiopublisherFP functions:fppattern只需引用你需要的东西:import Maybe from "fpEs";
// or this one:
/*
import Maybe from "fpEs/maybe";
*/
var m = Maybe.just(1); // It worksorimport {
compose, curry,
} from "fpEs";orimport {
compose, curry,
} from "fpEs/fp";Common FP (Compose, Curry)import {
compose, curry,
} from "fpEs/fp";
// compose
console.log(compose((x)=>x-8, (x)=>x+10, (x)=>x*10)(4)) // 42
console.log(compose((x)=>x+2, (x,y)=>x*y)(4,10)) // 42
// curry
console.log(curry((x, y, z) => x + y + z)(1,2,3)) // 6
console.log(curry((x, y, z) => x + y + z)(1)(2,3)) // 6
console.log(curry((x, y, z) => x + y + z)(1,2)(3)) // 6
console.log(curry((x, y, z) => x + y + z)(1)(2)(3)) // 6
// or this one:
/*
import Maybe from "fpEs/maybe";
*/
var m = Maybe.just(1); // It worksorimport {
compose, curry,
} from "fpEs";orimport {
compose, curry,
} from "fpEs/fp";Common FP (Compose, Curry)import {
compose, curry,
} from "fpEs/fp";
// compose
console.log(compose((x)=>x-8, (x)=>x+10, (x)=>x*10)(4)) // 42
console.log(compose((x)=>x+2, (x,y)=>x*y)(4,10)) // 42
// curry
console.log(curry((x, y, z) => x + y + z)(1,2,3)) // 6
console.log(curry((x, y, z) => x + y + z)(1)(2,3)) // 6
console.log(curry((x, y, z) => x + y + z)(1,2)(3)) // 6
console.log(curry((x, y, z) => x + y + z)(1)(2)(3)) // 6