crocks
Version:
A collection of well known Algebraic Datatypes for your utter enjoyment.
32 lines (25 loc) • 832 B
JavaScript
/** @license ISC License (c) copyright 2016 original and current authors */
/** @author Ian Hofmann-Hicks (evil) */
var compose = require('../core/compose')
var curry = require('../core/curry')
var isFunction = require('../core/isFunction')
var isContravariant = require('../core/isContravariant')
var fl = require('../core/flNames')
// contramap : Functor f => (b -> a) -> f b -> f a
function contramap(fn, m) {
if(!isFunction(fn)) {
throw new TypeError(
'contramap: Function required for first argument'
)
}
if(isFunction(m)) {
return compose(m, fn)
}
if(isContravariant(m)) {
return (m[fl.contramap] || m.contramap).call(m, fn)
}
throw new TypeError(
'contramap: Function or Contavariant Functor of the same type required for second argument'
)
}
module.exports = curry(contramap)