98 lines
2.3 KiB
JavaScript
98 lines
2.3 KiB
JavaScript
/**
|
|
* ```ts
|
|
* interface Separated<E, A> {
|
|
* readonly left: E
|
|
* readonly right: A
|
|
* }
|
|
* ```
|
|
*
|
|
* Represents a result of separating a whole into two parts.
|
|
*
|
|
* @since 2.10.0
|
|
*/
|
|
import { pipe } from './function';
|
|
import { flap as flap_ } from './Functor';
|
|
// -------------------------------------------------------------------------------------
|
|
// constructors
|
|
// -------------------------------------------------------------------------------------
|
|
/**
|
|
* @category constructors
|
|
* @since 2.10.0
|
|
*/
|
|
export var separated = function (left, right) { return ({ left: left, right: right }); };
|
|
var _map = function (fa, f) { return pipe(fa, map(f)); };
|
|
var _mapLeft = function (fa, f) { return pipe(fa, mapLeft(f)); };
|
|
var _bimap = function (fa, g, f) { return pipe(fa, bimap(g, f)); };
|
|
/**
|
|
* `map` can be used to turn functions `(a: A) => B` into functions `(fa: F<A>) => F<B>` whose argument and return types
|
|
* use the type constructor `F` to represent some computational context.
|
|
*
|
|
* @category mapping
|
|
* @since 2.10.0
|
|
*/
|
|
export var map = function (f) {
|
|
return function (fa) {
|
|
return separated(left(fa), f(right(fa)));
|
|
};
|
|
};
|
|
/**
|
|
* Map a function over the first type argument of a bifunctor.
|
|
*
|
|
* @category error handling
|
|
* @since 2.10.0
|
|
*/
|
|
export var mapLeft = function (f) {
|
|
return function (fa) {
|
|
return separated(f(left(fa)), right(fa));
|
|
};
|
|
};
|
|
/**
|
|
* Map a pair of functions over the two type arguments of the bifunctor.
|
|
*
|
|
* @category mapping
|
|
* @since 2.10.0
|
|
*/
|
|
export var bimap = function (f, g) {
|
|
return function (fa) {
|
|
return separated(f(left(fa)), g(right(fa)));
|
|
};
|
|
};
|
|
/**
|
|
* @category type lambdas
|
|
* @since 2.10.0
|
|
*/
|
|
export var URI = 'Separated';
|
|
/**
|
|
* @category instances
|
|
* @since 2.10.0
|
|
*/
|
|
export var Bifunctor = {
|
|
URI: URI,
|
|
mapLeft: _mapLeft,
|
|
bimap: _bimap
|
|
};
|
|
/**
|
|
* @category instances
|
|
* @since 2.10.0
|
|
*/
|
|
export var Functor = {
|
|
URI: URI,
|
|
map: _map
|
|
};
|
|
/**
|
|
* @category mapping
|
|
* @since 2.10.0
|
|
*/
|
|
export var flap = /*#__PURE__*/ flap_(Functor);
|
|
// -------------------------------------------------------------------------------------
|
|
// utils
|
|
// -------------------------------------------------------------------------------------
|
|
/**
|
|
* @since 2.10.0
|
|
*/
|
|
export var left = function (s) { return s.left; };
|
|
/**
|
|
* @since 2.10.0
|
|
*/
|
|
export var right = function (s) { return s.right; };
|