crocks
Version:
A collection of well known Algebraic Datatypes for your utter enjoyment.
36 lines (26 loc) • 940 B
JavaScript
/** @license ISC License (c) copyright 2017 original and current authors */
/** @author Ian Hofmann-Hicks (evil) */
var Last = require('.')
var First = require('../core/types').proxy('First')
var curry = require('../core/curry')
var isFunction = require('../core/isFunction')
var isSameType = require('../core/isSameType')
var applyTransform = function (first) { return Last(first.valueOf()); }
// firstToLast : First a -> Last a
// firstToLast : (a -> First b) -> a -> Last b
function firstToLast(first) {
if(isFunction(first)) {
return function(x) {
var m = first(x)
if(!isSameType(First, m)) {
throw new TypeError('firstToLast: First returning function required')
}
return applyTransform(m)
}
}
if(isSameType(First, first)) {
return applyTransform(first)
}
throw new TypeError('firstToLast: First or First returning function required')
}
module.exports = curry(firstToLast)