I\'m a JS dev, experimenting with functional programming ideas, and I\'m wondering if there\'s anyway to use chains for synchronous functions in the way the promise chains are w
You might be interested in the Identity functor – it allows you to lift any function to operate on the Identity's value – eg, square
and mult
below. You get a chainable interface without having to touch native prototypes ^_^
const Identity = x => ({
runIdentity: x,
map: f => Identity(f(x))
})
const square = x => x * x
const mult = x => y => x * y
let result = Identity(2)
.map(square)
.map(square)
.map(square)
.map(mult(1000))
.runIdentity
console.log(result)
// 256000