crocks
Version:
A collection of well known Algebraic Datatypes for your utter enjoyment.
35 lines (25 loc) • 893 B
JavaScript
/** @license ISC License (c) copyright 2017 original and current authors */
/** @author Ian Hofmann-Hicks (evil) */
var curry = require('../core/curry')
var isFunction = require('../core/isFunction')
var isWriter =
function (x) { return !!x && isFunction(x.read); }
var applyTransform = function (w) { return w.read(); }
// writerToPair : Monoid m => Writer m a -> Pair m a
// writerToPair : Monoid m => (a -> Writer m a) -> Pair m b
function writerToPair(writer) {
if(isFunction(writer)) {
return function(x) {
var m = writer(x)
if(!isWriter(m)) {
throw new TypeError('writerToPair: Writer returning function required')
}
return applyTransform(m)
}
}
if(isWriter(writer)) {
return applyTransform(writer)
}
throw new TypeError('writerToPair: Writer or Writer returning function required')
}
module.exports = curry(writerToPair)