moy-fp
Version:
A functional programming library.
47 lines (44 loc) • 846 B
JavaScript
/**
* Identity is a Functor, Monad and Applicative
* can map
* can join
* can chain
* can ap
* always use for containing a value in a box
* you may not need it if you are not sensitive in functional programming
*/
const Identity = value => Object.seal(
Object.create(null, {
value: {
value,
},
[Symbol.toStringTag]: {
value: 'Identity',
},
map: {
value: function map(fn){
return Identity.of(fn(this.value))
},
},
join: {
value: function join(){
return this.value
},
},
chain: {
value: function chain(fn){
return fn(this.value)
},
},
ap: {
value: function ap(I){
return I.map(this.value)
},
},
})
)
/**
* Functor Identity => a -> Identity a
*/
Identity.of = Identity
export default Identity