javascript fp demo

function eq (y) {
  return function forX(x) {
    return x === y
  }
}

function mod (y) {
  return function forX (x) {
    return x % y
  }
}

function compose (fn2, fn1) {
  return function composed (val) {
    return fn2(fn1(val))
  }
}

function not (fn) {
  return function negated (...args) {
    return !fn(...args)
  }
}

isOdd = compose(
  eq(1),
  mod(2)
)

isEven = not(isOdd)

let fiveIsOdd = isOdd(5)
let fourIsEven = isEven(4)

console.log(`5 is odd ${fiveIsOdd}`)
console.log('4 is even', fourIsEven)
posted @ 2019-08-08 20:26  不要相信我  阅读(223)  评论(0编辑  收藏  举报