# 函数式编程笔记

## 前言

### 函数式编程的特点

#### 高阶函数

// 高阶函数-函数作为参数-更灵活
function filter(array, fn) {
let results = []
for(let i = 0;i < array.length; i++) {
if(fn(array[i])) {
results.push(array[i])
}
}
return results
}

// 高阶函数-函数作为返回值 闭包

function makeFn () {
let msg = 'hello'
return function() {
console.log(msg)
}
}

const fn = makeFn()
fn()

makeFn()()


// map
const map = (arr, fn)=>{
let results = []
for(let i of arr) {
reuslts.push(fn(i))
}
return results
}

// every
const every = (arr, fn)=>{
let result = true
for(let i of arr) {
result = fn(i)
if(!result) {
break
}
}
return result
}

// some
const some = (arr, fn)=>{
let result = false
for(let i of arr) {
result = fn(i)
if(result) {
break
}
}
return result
}


#### 闭包

// lodash中的once函数 -只执行一次 闭包
function once(fn) {
let done = false
return function () {
if(!done) {
done = true
return fn.apply(this, arguments)
}
}
}


function makePower(power) {
return function (number) {
return Math.pow(power,number)
}
}

let power2 = makePower(2)
let power3 = makePower(3)

console.log(power2(4))
console.log(power2(5))
console.log(power3(4))


#### 纯函数

// 记忆函数

function getArea (r) {
console.log(r)
return Math.PI * r * r
}

let getAreaWithMemory = _.memoize(getArea)

// 模拟 memoize 实现

function memoize(f) {
let cache = {}
return function () {
let key = JSON.stringify(arguments)
cache[key] = cache[key] || f.apply(f, arguments)
return cache[key]
}
}


#### 柯理化

// 柯理化
function checkAge(min, age) {
return age >= min
}

function checkAge (min) {
return function (age) {
return age >= min
}
}

let checkAge = min=>(age=> age> = min)

let checkAge18 = checkAge(18)
let checkAge19 = checkAge(19)

console.log(checkAge18(20))
console.log(checkAge19(24))


lodash中的柯理化函数

_.curry(func)

function getSun(a,b,c) {
return a+b+c
}

const curried = _.curry(getSum)

console.log(curried(1,2,3))
console.log(curried(1)(2,3))
console.log(curried(1,2)(3))

const macth = _.curry((reg, str)=> {
return str.match(reg)
})

// curry 实现

function curry(func) {
return function curriedFn(...args) {
// 判断实参和形参的个数
if(args.length<function func.length) {
return function() {
return curriedFn(...args.contact(Array.from(arguments)))
}
}
return func(...args)
}
}


#### 函数组合

function compose(f,g){
return function (value) {
return f(g(value))
}
}

// lodash 中的组合函数
_.flowRight()

function compose(...args) {
return function (value) {
return args.reverse().reduce(function(acc, fn){
return fn(acc)
}, value)
}
}

const compose = (...args)=>value=>args.reverse().reduce((acc,fn)=>fn(acc), value)

lodash-fp模块

const f = fp.flowRight(fp.join('-'), fp.map(fp.toLower), fp.split(' '))

console.log(f('NEVER SAY DIE'))

// point free

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


#### Functor 函子

class Container{
constructor(value) {
this._value = value
}
map(fn) {
return new Container(fn(this._value))
}
}

let r = new Container(5).map(x=>x+1).map(x=>x*x)

class Container{
statuc of (value) {
return new Container(value)
}
constructor(value) {
this._value = value
}
map(fn) {
return new Container(fn(this._value))
}
}

let r = Container.of(5).map(x=>x+1).map(x=>x*x)


MayBe 函子

class MayBe {
statuc of (value) {
return new MayBe(value)
}
constructor(value) {
this._value = value
}

map(fn) {
return this.isNothing()? MayBe.of(null) : MayBe.of(fn(this._value))
}

isNothing () {
return this._value === null || this._value === undefind
}
}

posted @ 2020-07-21 16:58  c-137Summer  阅读(86)  评论(0编辑  收藏