var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
import { apFirst as apFirst_, apS as apS_, apSecond as apSecond_ } from './Apply';
import { bind as bind_, chainFirst as chainFirst_ } from './Chain';
import { fromEquals } from './Eq';
import { fromEitherK as fromEitherK_ } from './FromEither';
import { identity, pipe } from './function';
import { bindTo as bindTo_, flap as flap_, let as let__ } from './Functor';
import * as _ from './internal';
import * as N from './number';
import { fromCompare } from './Ord';
import * as RNEA from './ReadonlyNonEmptyArray';
import { separated } from './Separated';
import { filterE as filterE_, wiltDefault, witherDefault } from './Witherable';
import { guard as guard_ } from './Zero';
// -------------------------------------------------------------------------------------
// refinements
// -------------------------------------------------------------------------------------
/**
* Test whether a `ReadonlyArray` is empty.
*
* @example
* import { isEmpty } from 'fp-ts/ReadonlyArray'
*
* assert.strictEqual(isEmpty([]), true)
*
* @category refinements
* @since 2.5.0
*/
export var isEmpty = function (as) { return as.length === 0; };
/**
* Test whether a `ReadonlyArray` is non empty.
*
* @category refinements
* @since 2.5.0
*/
export var isNonEmpty = RNEA.isNonEmpty;
// -------------------------------------------------------------------------------------
// constructors
// -------------------------------------------------------------------------------------
/**
* Prepend an element to the front of a `ReadonlyArray`, creating a new `ReadonlyNonEmptyArray`.
*
* @example
* import { prepend } from 'fp-ts/ReadonlyArray'
* import { pipe } from 'fp-ts/function'
*
* assert.deepStrictEqual(pipe([2, 3, 4], prepend(1)), [1, 2, 3, 4])
*
* @since 2.10.0
*/
export var prepend = RNEA.prepend;
/**
* Less strict version of [`prepend`](#prepend).
*
* @since 2.11.0
*/
export var prependW = RNEA.prependW;
/**
* Append an element to the end of a `ReadonlyArray`, creating a new `ReadonlyNonEmptyArray`.
*
* @example
* import { append } from 'fp-ts/ReadonlyArray'
* import { pipe } from 'fp-ts/function'
*
* assert.deepStrictEqual(pipe([1, 2, 3], append(4)), [1, 2, 3, 4])
*
* @since 2.10.0
*/
export var append = RNEA.append;
/**
* Less strict version of [`append`](#append).
*
* @since 2.11.0
*/
export var appendW = RNEA.appendW;
/**
* Return a `ReadonlyArray` of length `n` with element `i` initialized with `f(i)`.
*
* **Note**. `n` is normalized to a non negative integer.
*
* @example
* import { makeBy } from 'fp-ts/ReadonlyArray'
*
* const double = (n: number): number => n * 2
* assert.deepStrictEqual(makeBy(5, double), [0, 2, 4, 6, 8])
*
* @category constructors
* @since 2.5.0
*/
export var makeBy = function (n, f) { return (n <= 0 ? empty : RNEA.makeBy(f)(n)); };
/**
* Create a `ReadonlyArray` containing a value repeated the specified number of times.
*
* **Note**. `n` is normalized to a non negative integer.
*
* @example
* import { replicate } from 'fp-ts/ReadonlyArray'
*
* assert.deepStrictEqual(replicate(3, 'a'), ['a', 'a', 'a'])
*
* @category constructors
* @since 2.5.0
*/
export var replicate = function (n, a) { return makeBy(n, function () { return a; }); };
export function fromPredicate(predicate) {
return function (a) { return (predicate(a) ? [a] : empty); };
}
// -------------------------------------------------------------------------------------
// conversions
// -------------------------------------------------------------------------------------
/**
* @category conversions
* @since 2.11.0
*/
export var fromOption = function (ma) { return (_.isNone(ma) ? empty : [ma.value]); };
/**
* Transforms an `Either` to a `ReadonlyArray`.
*
* @category conversions
* @since 2.11.0
*/
export var fromEither = function (e) { return (_.isLeft(e) ? empty : [e.right]); };
/**
* Less strict version of [`match`](#match).
*
* The `W` suffix (short for **W**idening) means that the handler return types will be merged.
*
* @category pattern matching
* @since 2.11.0
*/
export var matchW = function (onEmpty, onNonEmpty) {
return function (as) {
return isNonEmpty(as) ? onNonEmpty(as) : onEmpty();
};
};
/**
* @category pattern matching
* @since 2.11.0
*/
export var match = matchW;
/**
* Less strict version of [`matchLeft`](#matchleft).
*
* @category pattern matching
* @since 2.11.0
*/
export var matchLeftW = function (onEmpty, onNonEmpty) {
return function (as) {
return isNonEmpty(as) ? onNonEmpty(RNEA.head(as), RNEA.tail(as)) : onEmpty();
};
};
/**
* Break a `ReadonlyArray` into its first element and remaining elements.
*
* @example
* import { matchLeft } from 'fp-ts/ReadonlyArray'
*
* const len: (as: ReadonlyArray) => number = matchLeft(() => 0, (_, tail) => 1 + len(tail))
* assert.strictEqual(len([1, 2, 3]), 3)
*
* @category pattern matching
* @since 2.10.0
*/
export var matchLeft = matchLeftW;
/**
* Alias of [`matchLeft`](#matchleft).
*
* @category pattern matching
* @since 2.5.0
*/
export var foldLeft = matchLeft;
/**
* Less strict version of [`matchRight`](#matchright).
*
* @category pattern matching
* @since 2.11.0
*/
export var matchRightW = function (onEmpty, onNonEmpty) {
return function (as) {
return isNonEmpty(as) ? onNonEmpty(RNEA.init(as), RNEA.last(as)) : onEmpty();
};
};
/**
* Break a `ReadonlyArray` into its initial elements and the last element.
*
* @category pattern matching
* @since 2.10.0
*/
export var matchRight = matchRightW;
/**
* Alias of [`matchRight`](#matchright).
*
* @category pattern matching
* @since 2.5.0
*/
export var foldRight = matchRight;
// -------------------------------------------------------------------------------------
// combinators
// -------------------------------------------------------------------------------------
/**
* @category sequencing
* @since 2.7.0
*/
export var chainWithIndex = function (f) {
return function (as) {
if (isEmpty(as)) {
return empty;
}
var out = [];
for (var i = 0; i < as.length; i++) {
out.push.apply(out, f(i, as[i]));
}
return out;
};
};
/**
* Same as `reduce` but it carries over the intermediate steps.
*
* @example
* import { scanLeft } from 'fp-ts/ReadonlyArray'
*
* assert.deepStrictEqual(scanLeft(10, (b, a: number) => b - a)([1, 2, 3]), [10, 9, 7, 4])
*
* @since 2.5.0
*/
export var scanLeft = function (b, f) {
return function (as) {
var len = as.length;
var out = new Array(len + 1);
out[0] = b;
for (var i = 0; i < len; i++) {
out[i + 1] = f(out[i], as[i]);
}
return out;
};
};
/**
* Fold an array from the right, keeping all intermediate results instead of only the final result
*
* @example
* import { scanRight } from 'fp-ts/ReadonlyArray'
*
* assert.deepStrictEqual(scanRight(10, (a: number, b) => b - a)([1, 2, 3]), [4, 5, 7, 10])
*
* @since 2.5.0
*/
export var scanRight = function (b, f) {
return function (as) {
var len = as.length;
var out = new Array(len + 1);
out[len] = b;
for (var i = len - 1; i >= 0; i--) {
out[i] = f(as[i], out[i + 1]);
}
return out;
};
};
/**
* Calculate the number of elements in a `ReadonlyArray`.
*
* @since 2.10.0
*/
export var size = function (as) { return as.length; };
/**
* Test whether an array contains a particular index
*
* @since 2.5.0
*/
export var isOutOfBound = RNEA.isOutOfBound;
export function lookup(i, as) {
return as === undefined ? function (as) { return lookup(i, as); } : isOutOfBound(i, as) ? _.none : _.some(as[i]);
}
/**
* Get the first element in an array, or `None` if the array is empty
*
* @example
* import { head } from 'fp-ts/ReadonlyArray'
* import { some, none } from 'fp-ts/Option'
*
* assert.deepStrictEqual(head([1, 2, 3]), some(1))
* assert.deepStrictEqual(head([]), none)
*
* @since 2.5.0
*/
export var head = function (as) { return (isNonEmpty(as) ? _.some(RNEA.head(as)) : _.none); };
/**
* Get the last element in an array, or `None` if the array is empty
*
* @example
* import { last } from 'fp-ts/ReadonlyArray'
* import { some, none } from 'fp-ts/Option'
*
* assert.deepStrictEqual(last([1, 2, 3]), some(3))
* assert.deepStrictEqual(last([]), none)
*
* @since 2.5.0
*/
export var last = function (as) { return (isNonEmpty(as) ? _.some(RNEA.last(as)) : _.none); };
/**
* Get all but the first element of an array, creating a new array, or `None` if the array is empty
*
* @example
* import { tail } from 'fp-ts/ReadonlyArray'
* import { some, none } from 'fp-ts/Option'
*
* assert.deepStrictEqual(tail([1, 2, 3]), some([2, 3]))
* assert.deepStrictEqual(tail([]), none)
*
* @since 2.5.0
*/
export var tail = function (as) {
return isNonEmpty(as) ? _.some(RNEA.tail(as)) : _.none;
};
/**
* Get all but the last element of an array, creating a new array, or `None` if the array is empty
*
* @example
* import { init } from 'fp-ts/ReadonlyArray'
* import { some, none } from 'fp-ts/Option'
*
* assert.deepStrictEqual(init([1, 2, 3]), some([1, 2]))
* assert.deepStrictEqual(init([]), none)
*
* @since 2.5.0
*/
export var init = function (as) {
return isNonEmpty(as) ? _.some(RNEA.init(as)) : _.none;
};
/**
* Keep only a max number of elements from the start of an `ReadonlyArray`, creating a new `ReadonlyArray`.
*
* **Note**. `n` is normalized to a non negative integer.
*
* @example
* import * as RA from 'fp-ts/ReadonlyArray'
* import { pipe } from 'fp-ts/function'
*
* const input: ReadonlyArray = [1, 2, 3]
* assert.deepStrictEqual(pipe(input, RA.takeLeft(2)), [1, 2])
*
* // out of bounds
* assert.strictEqual(pipe(input, RA.takeLeft(4)), input)
* assert.strictEqual(pipe(input, RA.takeLeft(-1)), input)
*
* @since 2.5.0
*/
export var takeLeft = function (n) {
return function (as) {
return isOutOfBound(n, as) ? as : n === 0 ? empty : as.slice(0, n);
};
};
/**
* Keep only a max number of elements from the end of an `ReadonlyArray`, creating a new `ReadonlyArray`.
*
* **Note**. `n` is normalized to a non negative integer.
*
* @example
* import * as RA from 'fp-ts/ReadonlyArray'
* import { pipe } from 'fp-ts/function'
*
* const input: ReadonlyArray = [1, 2, 3]
* assert.deepStrictEqual(pipe(input, RA.takeRight(2)), [2, 3])
*
* // out of bounds
* assert.strictEqual(pipe(input, RA.takeRight(4)), input)
* assert.strictEqual(pipe(input, RA.takeRight(-1)), input)
*
* @since 2.5.0
*/
export var takeRight = function (n) {
return function (as) {
return isOutOfBound(n, as) ? as : n === 0 ? empty : as.slice(-n);
};
};
export function takeLeftWhile(predicate) {
return function (as) {
var out = [];
for (var _i = 0, as_1 = as; _i < as_1.length; _i++) {
var a = as_1[_i];
if (!predicate(a)) {
break;
}
out.push(a);
}
var len = out.length;
return len === as.length ? as : len === 0 ? empty : out;
};
}
var spanLeftIndex = function (as, predicate) {
var l = as.length;
var i = 0;
for (; i < l; i++) {
if (!predicate(as[i])) {
break;
}
}
return i;
};
export function spanLeft(predicate) {
return function (as) {
var _a = splitAt(spanLeftIndex(as, predicate))(as), init = _a[0], rest = _a[1];
return { init: init, rest: rest };
};
}
/**
* Drop a max number of elements from the start of an `ReadonlyArray`, creating a new `ReadonlyArray`.
*
* **Note**. `n` is normalized to a non negative integer.
*
* @example
* import * as RA from 'fp-ts/ReadonlyArray'
* import { pipe } from 'fp-ts/function'
*
* const input: ReadonlyArray = [1, 2, 3]
* assert.deepStrictEqual(pipe(input, RA.dropLeft(2)), [3])
* assert.strictEqual(pipe(input, RA.dropLeft(0)), input)
* assert.strictEqual(pipe(input, RA.dropLeft(-1)), input)
*
* @since 2.5.0
*/
export var dropLeft = function (n) {
return function (as) {
return n <= 0 || isEmpty(as) ? as : n >= as.length ? empty : as.slice(n, as.length);
};
};
/**
* Drop a max number of elements from the end of an `ReadonlyArray`, creating a new `ReadonlyArray`.
*
* **Note**. `n` is normalized to a non negative integer.
*
* @example
* import * as RA from 'fp-ts/ReadonlyArray'
* import { pipe } from 'fp-ts/function'
*
* const input: ReadonlyArray = [1, 2, 3]
* assert.deepStrictEqual(pipe(input, RA.dropRight(2)), [1])
* assert.strictEqual(pipe(input, RA.dropRight(0)), input)
* assert.strictEqual(pipe(input, RA.dropRight(-1)), input)
*
* @since 2.5.0
*/
export var dropRight = function (n) {
return function (as) {
return n <= 0 || isEmpty(as) ? as : n >= as.length ? empty : as.slice(0, as.length - n);
};
};
export function dropLeftWhile(predicate) {
return function (as) {
var i = spanLeftIndex(as, predicate);
return i === 0 ? as : i === as.length ? empty : as.slice(i);
};
}
/**
* Find the first index for which a predicate holds
*
* @example
* import { findIndex } from 'fp-ts/ReadonlyArray'
* import { some, none } from 'fp-ts/Option'
*
* assert.deepStrictEqual(findIndex((n: number) => n === 2)([1, 2, 3]), some(1))
* assert.deepStrictEqual(findIndex((n: number) => n === 2)([]), none)
*
* @since 2.5.0
*/
export var findIndex = function (predicate) {
return function (as) {
for (var i = 0; i < as.length; i++) {
if (predicate(as[i])) {
return _.some(i);
}
}
return _.none;
};
};
export function findFirst(predicate) {
return function (as) {
for (var i = 0; i < as.length; i++) {
if (predicate(as[i])) {
return _.some(as[i]);
}
}
return _.none;
};
}
/**
* Find the first element returned by an option based selector function
*
* @example
* import { findFirstMap } from 'fp-ts/ReadonlyArray'
* import { some, none } from 'fp-ts/Option'
*
* interface Person {
* readonly name: string
* readonly age?: number
* }
*
* const persons: ReadonlyArray = [{ name: 'John' }, { name: 'Mary', age: 45 }, { name: 'Joey', age: 28 }]
*
* // returns the name of the first person that has an age
* assert.deepStrictEqual(findFirstMap((p: Person) => (p.age === undefined ? none : some(p.name)))(persons), some('Mary'))
*
* @since 2.5.0
*/
export var findFirstMap = function (f) {
return function (as) {
for (var i = 0; i < as.length; i++) {
var out = f(as[i]);
if (_.isSome(out)) {
return out;
}
}
return _.none;
};
};
export function findLast(predicate) {
return function (as) {
for (var i = as.length - 1; i >= 0; i--) {
if (predicate(as[i])) {
return _.some(as[i]);
}
}
return _.none;
};
}
/**
* Find the last element returned by an option based selector function
*
* @example
* import { findLastMap } from 'fp-ts/ReadonlyArray'
* import { some, none } from 'fp-ts/Option'
*
* interface Person {
* readonly name: string
* readonly age?: number
* }
*
* const persons: ReadonlyArray = [{ name: 'John' }, { name: 'Mary', age: 45 }, { name: 'Joey', age: 28 }]
*
* // returns the name of the last person that has an age
* assert.deepStrictEqual(findLastMap((p: Person) => (p.age === undefined ? none : some(p.name)))(persons), some('Joey'))
*
* @since 2.5.0
*/
export var findLastMap = function (f) {
return function (as) {
for (var i = as.length - 1; i >= 0; i--) {
var out = f(as[i]);
if (_.isSome(out)) {
return out;
}
}
return _.none;
};
};
/**
* Returns the index of the last element of the list which matches the predicate
*
* @example
* import { findLastIndex } from 'fp-ts/ReadonlyArray'
* import { some, none } from 'fp-ts/Option'
*
* interface X {
* readonly a: number
* readonly b: number
* }
* const xs: ReadonlyArray = [{ a: 1, b: 0 }, { a: 1, b: 1 }]
* assert.deepStrictEqual(findLastIndex((x: { readonly a: number }) => x.a === 1)(xs), some(1))
* assert.deepStrictEqual(findLastIndex((x: { readonly a: number }) => x.a === 4)(xs), none)
*
*
* @since 2.5.0
*/
export var findLastIndex = function (predicate) {
return function (as) {
for (var i = as.length - 1; i >= 0; i--) {
if (predicate(as[i])) {
return _.some(i);
}
}
return _.none;
};
};
/**
* Insert an element at the specified index, creating a new array, or returning `None` if the index is out of bounds
*
* @example
* import { insertAt } from 'fp-ts/ReadonlyArray'
* import { some } from 'fp-ts/Option'
*
* assert.deepStrictEqual(insertAt(2, 5)([1, 2, 3, 4]), some([1, 2, 5, 3, 4]))
*
* @since 2.5.0
*/
export var insertAt = function (i, a) {
return function (as) {
return i < 0 || i > as.length ? _.none : _.some(RNEA.unsafeInsertAt(i, a, as));
};
};
/**
* Change the element at the specified index, creating a new array, or returning `None` if the index is out of bounds
*
* @example
* import { updateAt } from 'fp-ts/ReadonlyArray'
* import { some, none } from 'fp-ts/Option'
*
* assert.deepStrictEqual(updateAt(1, 1)([1, 2, 3]), some([1, 1, 3]))
* assert.deepStrictEqual(updateAt(1, 1)([]), none)
*
* @since 2.5.0
*/
export var updateAt = function (i, a) {
return modifyAt(i, function () { return a; });
};
/**
* Delete the element at the specified index, creating a new array, or returning `None` if the index is out of bounds
*
* @example
* import { deleteAt } from 'fp-ts/ReadonlyArray'
* import { some, none } from 'fp-ts/Option'
*
* assert.deepStrictEqual(deleteAt(0)([1, 2, 3]), some([2, 3]))
* assert.deepStrictEqual(deleteAt(1)([]), none)
*
* @since 2.5.0
*/
export var deleteAt = function (i) {
return function (as) {
return isOutOfBound(i, as) ? _.none : _.some(unsafeDeleteAt(i, as));
};
};
/**
* Apply a function to the element at the specified index, creating a new array, or returning `None` if the index is out
* of bounds
*
* @example
* import { modifyAt } from 'fp-ts/ReadonlyArray'
* import { some, none } from 'fp-ts/Option'
*
* const double = (x: number): number => x * 2
* assert.deepStrictEqual(modifyAt(1, double)([1, 2, 3]), some([1, 4, 3]))
* assert.deepStrictEqual(modifyAt(1, double)([]), none)
*
* @since 2.5.0
*/
export var modifyAt = function (i, f) {
return function (as) {
return isOutOfBound(i, as) ? _.none : _.some(unsafeUpdateAt(i, f(as[i]), as));
};
};
/**
* Reverse an array, creating a new array
*
* @example
* import { reverse } from 'fp-ts/ReadonlyArray'
*
* assert.deepStrictEqual(reverse([1, 2, 3]), [3, 2, 1])
*
* @since 2.5.0
*/
export var reverse = function (as) { return (as.length <= 1 ? as : as.slice().reverse()); };
/**
* Extracts from an array of `Either` all the `Right` elements. All the `Right` elements are extracted in order
*
* @example
* import { rights } from 'fp-ts/ReadonlyArray'
* import { right, left } from 'fp-ts/Either'
*
* assert.deepStrictEqual(rights([right(1), left('foo'), right(2)]), [1, 2])
*
* @since 2.5.0
*/
export var rights = function (as) {
var r = [];
for (var i = 0; i < as.length; i++) {
var a = as[i];
if (a._tag === 'Right') {
r.push(a.right);
}
}
return r;
};
/**
* Extracts from an array of `Either` all the `Left` elements. All the `Left` elements are extracted in order
*
* @example
* import { lefts } from 'fp-ts/ReadonlyArray'
* import { left, right } from 'fp-ts/Either'
*
* assert.deepStrictEqual(lefts([right(1), left('foo'), right(2)]), ['foo'])
*
* @since 2.5.0
*/
export var lefts = function (as) {
var r = [];
for (var i = 0; i < as.length; i++) {
var a = as[i];
if (a._tag === 'Left') {
r.push(a.left);
}
}
return r;
};
/**
* Sort the elements of an array in increasing order, creating a new array
*
* @example
* import { sort } from 'fp-ts/ReadonlyArray'
* import * as N from 'fp-ts/number'
*
* assert.deepStrictEqual(sort(N.Ord)([3, 2, 1]), [1, 2, 3])
*
* @since 2.5.0
*/
export var sort = function (O) {
return function (as) {
return as.length <= 1 ? as : as.slice().sort(O.compare);
};
};
// TODO: curry and make data-last in v3
/**
* Apply a function to pairs of elements at the same index in two arrays, collecting the results in a new array. If one
* input array is short, excess elements of the longer array are discarded.
*
* @example
* import { zipWith } from 'fp-ts/ReadonlyArray'
*
* assert.deepStrictEqual(zipWith([1, 2, 3], ['a', 'b', 'c', 'd'], (n, s) => s + n), ['a1', 'b2', 'c3'])
*
* @since 2.5.0
*/
export var zipWith = function (fa, fb, f) {
var fc = [];
var len = Math.min(fa.length, fb.length);
for (var i = 0; i < len; i++) {
fc[i] = f(fa[i], fb[i]);
}
return fc;
};
export function zip(as, bs) {
if (bs === undefined) {
return function (bs) { return zip(bs, as); };
}
return zipWith(as, bs, function (a, b) { return [a, b]; });
}
/**
* The function is reverse of `zip`. Takes an array of pairs and return two corresponding arrays
*
* @example
* import { unzip } from 'fp-ts/ReadonlyArray'
*
* assert.deepStrictEqual(unzip([[1, 'a'], [2, 'b'], [3, 'c']]), [[1, 2, 3], ['a', 'b', 'c']])
*
* @since 2.5.0
*/
export var unzip = function (as) {
var fa = [];
var fb = [];
for (var i = 0; i < as.length; i++) {
fa[i] = as[i][0];
fb[i] = as[i][1];
}
return [fa, fb];
};
/**
* Prepend an element to every member of an array
*
* @example
* import { prependAll } from 'fp-ts/ReadonlyArray'
*
* assert.deepStrictEqual(prependAll(9)([1, 2, 3, 4]), [9, 1, 9, 2, 9, 3, 9, 4])
*
* @since 2.10.0
*/
export var prependAll = function (middle) {
var f = RNEA.prependAll(middle);
return function (as) { return (isNonEmpty(as) ? f(as) : as); };
};
/**
* Places an element in between members of an array
*
* @example
* import { intersperse } from 'fp-ts/ReadonlyArray'
*
* assert.deepStrictEqual(intersperse(9)([1, 2, 3, 4]), [1, 9, 2, 9, 3, 9, 4])
*
* @since 2.9.0
*/
export var intersperse = function (middle) {
var f = RNEA.intersperse(middle);
return function (as) { return (isNonEmpty(as) ? f(as) : as); };
};
/**
* Rotate a `ReadonlyArray` by `n` steps.
*
* @example
* import { rotate } from 'fp-ts/ReadonlyArray'
*
* assert.deepStrictEqual(rotate(2)([1, 2, 3, 4, 5]), [4, 5, 1, 2, 3])
*
* @since 2.5.0
*/
export var rotate = function (n) {
var f = RNEA.rotate(n);
return function (as) { return (isNonEmpty(as) ? f(as) : as); };
};
export function elem(E) {
return function (a, as) {
if (as === undefined) {
var elemE_1 = elem(E);
return function (as) { return elemE_1(a, as); };
}
var predicate = function (element) { return E.equals(element, a); };
var i = 0;
for (; i < as.length; i++) {
if (predicate(as[i])) {
return true;
}
}
return false;
};
}
/**
* Remove duplicates from an array, keeping the first occurrence of an element.
*
* @example
* import { uniq } from 'fp-ts/ReadonlyArray'
* import * as N from 'fp-ts/number'
*
* assert.deepStrictEqual(uniq(N.Eq)([1, 2, 1]), [1, 2])
*
* @since 2.5.0
*/
export var uniq = function (E) {
var f = RNEA.uniq(E);
return function (as) { return (isNonEmpty(as) ? f(as) : as); };
};
/**
* Sort the elements of an array in increasing order, where elements are compared using first `ords[0]`, then `ords[1]`,
* etc...
*
* @example
* import { sortBy } from 'fp-ts/ReadonlyArray'
* import { contramap } from 'fp-ts/Ord'
* import * as S from 'fp-ts/string'
* import * as N from 'fp-ts/number'
* import { pipe } from 'fp-ts/function'
*
* interface Person {
* readonly name: string
* readonly age: number
* }
* const byName = pipe(S.Ord, contramap((p: Person) => p.name))
* const byAge = pipe(N.Ord, contramap((p: Person) => p.age))
*
* const sortByNameByAge = sortBy([byName, byAge])
*
* const persons = [{ name: 'a', age: 1 }, { name: 'b', age: 3 }, { name: 'c', age: 2 }, { name: 'b', age: 2 }]
* assert.deepStrictEqual(sortByNameByAge(persons), [
* { name: 'a', age: 1 },
* { name: 'b', age: 2 },
* { name: 'b', age: 3 },
* { name: 'c', age: 2 }
* ])
*
* @since 2.5.0
*/
export var sortBy = function (ords) {
var f = RNEA.sortBy(ords);
return function (as) { return (isNonEmpty(as) ? f(as) : as); };
};
/**
* A useful recursion pattern for processing a `ReadonlyArray` to produce a new `ReadonlyArray`, often used for "chopping" up the input
* `ReadonlyArray`. Typically `chop` is called with some function that will consume an initial prefix of the `ReadonlyArray` and produce a
* value and the tail of the `ReadonlyArray`.
*
* @example
* import { Eq } from 'fp-ts/Eq'
* import * as RA from 'fp-ts/ReadonlyArray'
* import * as N from 'fp-ts/number'
* import { pipe } from 'fp-ts/function'
*
* const group = (S: Eq): ((as: ReadonlyArray) => ReadonlyArray>) => {
* return RA.chop(as => {
* const { init, rest } = pipe(as, RA.spanLeft((a: A) => S.equals(a, as[0])))
* return [init, rest]
* })
* }
* assert.deepStrictEqual(group(N.Eq)([1, 1, 2, 3, 3, 4]), [[1, 1], [2], [3, 3], [4]])
*
* @since 2.5.0
*/
export var chop = function (f) {
var g = RNEA.chop(f);
return function (as) { return (isNonEmpty(as) ? g(as) : empty); };
};
/**
* Splits a `ReadonlyArray` into two pieces, the first piece has max `n` elements.
*
* @example
* import { splitAt } from 'fp-ts/ReadonlyArray'
*
* assert.deepStrictEqual(splitAt(2)([1, 2, 3, 4, 5]), [[1, 2], [3, 4, 5]])
*
* @since 2.5.0
*/
export var splitAt = function (n) {
return function (as) {
return n >= 1 && isNonEmpty(as) ? RNEA.splitAt(n)(as) : isEmpty(as) ? [as, empty] : [empty, as];
};
};
/**
* Splits a `ReadonlyArray` into length-`n` pieces. The last piece will be shorter if `n` does not evenly divide the length of
* the `ReadonlyArray`. Note that `chunksOf(n)([])` is `[]`, not `[[]]`. This is intentional, and is consistent with a recursive
* definition of `chunksOf`; it satisfies the property that:
*
* ```ts
* chunksOf(n)(xs).concat(chunksOf(n)(ys)) == chunksOf(n)(xs.concat(ys)))
* ```
*
* whenever `n` evenly divides the length of `as`.
*
* @example
* import { chunksOf } from 'fp-ts/ReadonlyArray'
*
* assert.deepStrictEqual(chunksOf(2)([1, 2, 3, 4, 5]), [[1, 2], [3, 4], [5]])
*
* @since 2.5.0
*/
export var chunksOf = function (n) {
var f = RNEA.chunksOf(n);
return function (as) { return (isNonEmpty(as) ? f(as) : empty); };
};
/**
* @category lifting
* @since 2.11.0
*/
export var fromOptionK = function (f) {
return function () {
var a = [];
for (var _i = 0; _i < arguments.length; _i++) {
a[_i] = arguments[_i];
}
return fromOption(f.apply(void 0, a));
};
};
export function comprehension(input, f, g) {
if (g === void 0) { g = function () { return true; }; }
var go = function (scope, input) {
return isNonEmpty(input)
? pipe(RNEA.head(input), chain(function (x) { return go(pipe(scope, append(x)), RNEA.tail(input)); }))
: g.apply(void 0, scope) ? [f.apply(void 0, scope)]
: empty;
};
return go(empty, input);
}
/**
* @since 2.11.0
*/
export var concatW = function (second) {
return function (first) {
return isEmpty(first) ? second : isEmpty(second) ? first : first.concat(second);
};
};
/**
* @since 2.11.0
*/
export var concat = concatW;
export function union(E) {
var unionE = RNEA.union(E);
return function (first, second) {
if (second === undefined) {
var unionE_1 = union(E);
return function (second) { return unionE_1(second, first); };
}
return isNonEmpty(first) && isNonEmpty(second) ? unionE(second)(first) : isNonEmpty(first) ? first : second;
};
}
export function intersection(E) {
var elemE = elem(E);
return function (xs, ys) {
if (ys === undefined) {
var intersectionE_1 = intersection(E);
return function (ys) { return intersectionE_1(ys, xs); };
}
return xs.filter(function (a) { return elemE(a, ys); });
};
}
export function difference(E) {
var elemE = elem(E);
return function (xs, ys) {
if (ys === undefined) {
var differenceE_1 = difference(E);
return function (ys) { return differenceE_1(ys, xs); };
}
return xs.filter(function (a) { return !elemE(a, ys); });
};
}
var _map = function (fa, f) { return pipe(fa, map(f)); };
var _mapWithIndex = function (fa, f) { return pipe(fa, mapWithIndex(f)); };
var _ap = function (fab, fa) { return pipe(fab, ap(fa)); };
var _chain = function (ma, f) { return pipe(ma, chain(f)); };
var _filter = function (fa, predicate) {
return pipe(fa, filter(predicate));
};
var _filterMap = function (fa, f) { return pipe(fa, filterMap(f)); };
var _partition = function (fa, predicate) {
return pipe(fa, partition(predicate));
};
var _partitionMap = function (fa, f) { return pipe(fa, partitionMap(f)); };
var _partitionWithIndex = function (fa, predicateWithIndex) { return pipe(fa, partitionWithIndex(predicateWithIndex)); };
var _partitionMapWithIndex = function (fa, f) { return pipe(fa, partitionMapWithIndex(f)); };
var _alt = function (fa, that) { return pipe(fa, alt(that)); };
var _reduce = function (fa, b, f) { return pipe(fa, reduce(b, f)); };
var _foldMap = function (M) {
var foldMapM = foldMap(M);
return function (fa, f) { return pipe(fa, foldMapM(f)); };
};
var _reduceRight = function (fa, b, f) { return pipe(fa, reduceRight(b, f)); };
var _reduceWithIndex = function (fa, b, f) {
return pipe(fa, reduceWithIndex(b, f));
};
var _foldMapWithIndex = function (M) {
var foldMapWithIndexM = foldMapWithIndex(M);
return function (fa, f) { return pipe(fa, foldMapWithIndexM(f)); };
};
var _reduceRightWithIndex = function (fa, b, f) {
return pipe(fa, reduceRightWithIndex(b, f));
};
var _filterMapWithIndex = function (fa, f) { return pipe(fa, filterMapWithIndex(f)); };
var _filterWithIndex = function (fa, predicateWithIndex) { return pipe(fa, filterWithIndex(predicateWithIndex)); };
var _extend = function (fa, f) { return pipe(fa, extend(f)); };
var _traverse = function (F) {
var traverseF = traverse(F);
return function (ta, f) { return pipe(ta, traverseF(f)); };
};
/* istanbul ignore next */
var _traverseWithIndex = function (F) {
var traverseWithIndexF = traverseWithIndex(F);
return function (ta, f) { return pipe(ta, traverseWithIndexF(f)); };
};
/** @internal */
export var _chainRecDepthFirst = function (a, f) { return pipe(a, chainRecDepthFirst(f)); };
/** @internal */
export var _chainRecBreadthFirst = function (a, f) { return pipe(a, chainRecBreadthFirst(f)); };
/**
* @category constructors
* @since 2.5.0
*/
export var of = RNEA.of;
/**
* @since 2.7.0
*/
export var zero = function () { return empty; };
/**
* Less strict version of [`alt`](#alt).
*
* The `W` suffix (short for **W**idening) means that the return types will be merged.
*
* @example
* import * as RA from 'fp-ts/ReadonlyArray'
* import { pipe } from 'fp-ts/function'
*
* assert.deepStrictEqual(
* pipe(
* [1, 2, 3],
* RA.altW(() => ['a', 'b'])
* ),
* [1, 2, 3, 'a', 'b']
* )
*
* @category error handling
* @since 2.9.0
*/
export var altW = function (that) {
return function (fa) {
return fa.concat(that());
};
};
/**
* Identifies an associative operation on a type constructor. It is similar to `Semigroup`, except that it applies to
* types of kind `* -> *`.
*
* In case of `ReadonlyArray` concatenates the inputs into a single array.
*
* @example
* import * as RA from 'fp-ts/ReadonlyArray'
* import { pipe } from 'fp-ts/function'
*
* assert.deepStrictEqual(
* pipe(
* [1, 2, 3],
* RA.alt(() => [4, 5])
* ),
* [1, 2, 3, 4, 5]
* )
*
* @category error handling
* @since 2.5.0
*/
export var alt = altW;
/**
* @since 2.5.0
*/
export var ap = function (fa) {
return chain(function (f) { return pipe(fa, map(f)); });
};
/**
* Composes computations in sequence, using the return value of one computation to determine the next computation.
*
* @example
* import * as RA from 'fp-ts/ReadonlyArray'
* import { pipe } from 'fp-ts/function'
*
* assert.deepStrictEqual(
* pipe(
* [1, 2, 3],
* RA.chain((n) => [`a${n}`, `b${n}`])
* ),
* ['a1', 'b1', 'a2', 'b2', 'a3', 'b3']
* )
* assert.deepStrictEqual(
* pipe(
* [1, 2, 3],
* RA.chain(() => [])
* ),
* []
* )
*
* @category sequencing
* @since 2.5.0
*/
export var chain = function (f) { return function (ma) {
return pipe(ma, chainWithIndex(function (_, a) { return f(a); }));
}; };
/**
* @category sequencing
* @since 2.5.0
*/
export var flatten = /*#__PURE__*/ chain(identity);
/**
* `map` can be used to turn functions `(a: A) => B` into functions `(fa: F) => F` whose argument and return types
* use the type constructor `F` to represent some computational context.
*
* @category mapping
* @since 2.5.0
*/
export var map = function (f) { return function (fa) {
return fa.map(function (a) { return f(a); });
}; };
/**
* @category mapping
* @since 2.5.0
*/
export var mapWithIndex = function (f) { return function (fa) {
return fa.map(function (a, i) { return f(i, a); });
}; };
/**
* @category filtering
* @since 2.5.0
*/
export var separate = function (fa) {
var left = [];
var right = [];
for (var _i = 0, fa_1 = fa; _i < fa_1.length; _i++) {
var e = fa_1[_i];
if (e._tag === 'Left') {
left.push(e.left);
}
else {
right.push(e.right);
}
}
return separated(left, right);
};
/**
* @category filtering
* @since 2.5.0
*/
export var filter = function (predicate) {
return function (as) {
return as.filter(predicate);
};
};
/**
* @category filtering
* @since 2.5.0
*/
export var filterMapWithIndex = function (f) {
return function (fa) {
var out = [];
for (var i = 0; i < fa.length; i++) {
var optionB = f(i, fa[i]);
if (_.isSome(optionB)) {
out.push(optionB.value);
}
}
return out;
};
};
/**
* @category filtering
* @since 2.5.0
*/
export var filterMap = function (f) {
return filterMapWithIndex(function (_, a) { return f(a); });
};
/**
* @category filtering
* @since 2.5.0
*/
export var compact = /*#__PURE__*/ filterMap(identity);
/**
* @category filtering
* @since 2.5.0
*/
export var partition = function (predicate) {
return partitionWithIndex(function (_, a) { return predicate(a); });
};
/**
* @category filtering
* @since 2.5.0
*/
export var partitionWithIndex = function (predicateWithIndex) {
return function (as) {
var left = [];
var right = [];
for (var i = 0; i < as.length; i++) {
var a = as[i];
if (predicateWithIndex(i, a)) {
right.push(a);
}
else {
left.push(a);
}
}
return separated(left, right);
};
};
/**
* @category filtering
* @since 2.5.0
*/
export var partitionMap = function (f) {
return partitionMapWithIndex(function (_, a) { return f(a); });
};
/**
* @category filtering
* @since 2.5.0
*/
export var partitionMapWithIndex = function (f) {
return function (fa) {
var left = [];
var right = [];
for (var i = 0; i < fa.length; i++) {
var e = f(i, fa[i]);
if (e._tag === 'Left') {
left.push(e.left);
}
else {
right.push(e.right);
}
}
return separated(left, right);
};
};
/**
* @category filtering
* @since 2.5.0
*/
export var filterWithIndex = function (predicateWithIndex) {
return function (as) {
return as.filter(function (a, i) { return predicateWithIndex(i, a); });
};
};
/**
* @since 2.5.0
*/
export var extend = function (f) { return function (wa) {
return wa.map(function (_, i) { return f(wa.slice(i)); });
}; };
/**
* @since 2.5.0
*/
export var duplicate = /*#__PURE__*/ extend(identity);
/**
* @category folding
* @since 2.5.0
*/
export var foldMapWithIndex = function (M) {
return function (f) {
return function (fa) {
return fa.reduce(function (b, a, i) { return M.concat(b, f(i, a)); }, M.empty);
};
};
};
/**
* @category folding
* @since 2.5.0
*/
export var reduce = function (b, f) {
return reduceWithIndex(b, function (_, b, a) { return f(b, a); });
};
/**
* @category folding
* @since 2.5.0
*/
export var foldMap = function (M) {
var foldMapWithIndexM = foldMapWithIndex(M);
return function (f) { return foldMapWithIndexM(function (_, a) { return f(a); }); };
};
/**
* @category folding
* @since 2.5.0
*/
export var reduceWithIndex = function (b, f) { return function (fa) {
var len = fa.length;
var out = b;
for (var i = 0; i < len; i++) {
out = f(i, out, fa[i]);
}
return out;
}; };
/**
* @category folding
* @since 2.5.0
*/
export var reduceRight = function (b, f) {
return reduceRightWithIndex(b, function (_, a, b) { return f(a, b); });
};
/**
* @category folding
* @since 2.5.0
*/
export var reduceRightWithIndex = function (b, f) { return function (fa) {
return fa.reduceRight(function (b, a, i) { return f(i, a, b); }, b);
}; };
/**
* @category traversing
* @since 2.6.3
*/
export var traverse = function (F) {
var traverseWithIndexF = traverseWithIndex(F);
return function (f) { return traverseWithIndexF(function (_, a) { return f(a); }); };
};
/**
* @category traversing
* @since 2.6.3
*/
export var sequence = function (F) {
return function (ta) {
return _reduce(ta, F.of(zero()), function (fas, fa) {
return F.ap(F.map(fas, function (as) { return function (a) { return pipe(as, append(a)); }; }), fa);
});
};
};
/**
* @category sequencing
* @since 2.6.3
*/
export var traverseWithIndex = function (F) {
return function (f) {
return reduceWithIndex(F.of(zero()), function (i, fbs, a) {
return F.ap(F.map(fbs, function (bs) { return function (b) { return pipe(bs, append(b)); }; }), f(i, a));
});
};
};
/**
* @category filtering
* @since 2.6.5
*/
export var wither = function (F) {
var _witherF = _wither(F);
return function (f) { return function (fa) { return _witherF(fa, f); }; };
};
/**
* @category filtering
* @since 2.6.5
*/
export var wilt = function (F) {
var _wiltF = _wilt(F);
return function (f) { return function (fa) { return _wiltF(fa, f); }; };
};
/**
* @since 2.6.6
*/
export var unfold = function (b, f) {
var out = [];
var bb = b;
// eslint-disable-next-line no-constant-condition
while (true) {
var mt = f(bb);
if (_.isSome(mt)) {
var _a = mt.value, a = _a[0], b_1 = _a[1];
out.push(a);
bb = b_1;
}
else {
break;
}
}
return out;
};
/**
* @category type lambdas
* @since 2.5.0
*/
export var URI = 'ReadonlyArray';
/**
* @category instances
* @since 2.5.0
*/
export var getShow = function (S) { return ({
show: function (as) { return "[".concat(as.map(S.show).join(', '), "]"); }
}); };
/**
* @category instances
* @since 2.5.0
*/
export var getSemigroup = function () { return ({
concat: function (first, second) { return (isEmpty(first) ? second : isEmpty(second) ? first : first.concat(second)); }
}); };
/**
* Returns a `Monoid` for `ReadonlyArray`.
*
* @example
* import { getMonoid } from 'fp-ts/ReadonlyArray'
*
* const M = getMonoid()
* assert.deepStrictEqual(M.concat([1, 2], [3, 4]), [1, 2, 3, 4])
*
* @category instances
* @since 2.5.0
*/
export var getMonoid = function () { return ({
concat: getSemigroup().concat,
empty: empty
}); };
/**
* Derives an `Eq` over the `ReadonlyArray` of a given element type from the `Eq` of that type. The derived `Eq` defines two
* arrays as equal if all elements of both arrays are compared equal pairwise with the given `E`. In case of arrays of
* different lengths, the result is non equality.
*
* @example
* import * as S from 'fp-ts/string'
* import { getEq } from 'fp-ts/ReadonlyArray'
*
* const E = getEq(S.Eq)
* assert.strictEqual(E.equals(['a', 'b'], ['a', 'b']), true)
* assert.strictEqual(E.equals(['a'], []), false)
*
* @category instances
* @since 2.5.0
*/
export var getEq = function (E) {
return fromEquals(function (xs, ys) { return xs.length === ys.length && xs.every(function (x, i) { return E.equals(x, ys[i]); }); });
};
/**
* Derives an `Ord` over the `ReadonlyArray` of a given element type from the `Ord` of that type. The ordering between two such
* arrays is equal to: the first non equal comparison of each arrays elements taken pairwise in increasing order, in
* case of equality over all the pairwise elements; the longest array is considered the greatest, if both arrays have
* the same length, the result is equality.
*
* @example
* import { getOrd } from 'fp-ts/ReadonlyArray'
* import * as S from 'fp-ts/string'
*
* const O = getOrd(S.Ord)
* assert.strictEqual(O.compare(['b'], ['a']), 1)
* assert.strictEqual(O.compare(['a'], ['a']), 0)
* assert.strictEqual(O.compare(['a'], ['b']), -1)
*
*
* @category instances
* @since 2.5.0
*/
export var getOrd = function (O) {
return fromCompare(function (a, b) {
var aLen = a.length;
var bLen = b.length;
var len = Math.min(aLen, bLen);
for (var i = 0; i < len; i++) {
var ordering = O.compare(a[i], b[i]);
if (ordering !== 0) {
return ordering;
}
}
return N.Ord.compare(aLen, bLen);
});
};
/**
* @category instances
* @since 2.11.0
*/
export var getUnionSemigroup = function (E) {
var unionE = union(E);
return {
concat: function (first, second) { return unionE(second)(first); }
};
};
/**
* @category instances
* @since 2.11.0
*/
export var getUnionMonoid = function (E) { return ({
concat: getUnionSemigroup(E).concat,
empty: empty
}); };
/**
* @category instances
* @since 2.11.0
*/
export var getIntersectionSemigroup = function (E) {
var intersectionE = intersection(E);
return {
concat: function (first, second) { return intersectionE(second)(first); }
};
};
/**
* @category instances
* @since 2.11.0
*/
export var getDifferenceMagma = function (E) {
var differenceE = difference(E);
return {
concat: function (first, second) { return differenceE(second)(first); }
};
};
/**
* @category instances
* @since 2.7.0
*/
export var Functor = {
URI: URI,
map: _map
};
/**
* @category mapping
* @since 2.10.0
*/
export var flap = /*#__PURE__*/ flap_(Functor);
/**
* @category instances
* @since 2.10.0
*/
export var Pointed = {
URI: URI,
of: of
};
/**
* @category instances
* @since 2.7.0
*/
export var FunctorWithIndex = {
URI: URI,
map: _map,
mapWithIndex: _mapWithIndex
};
/**
* @category instances
* @since 2.10.0
*/
export var Apply = {
URI: URI,
map: _map,
ap: _ap
};
/**
* Combine two effectful actions, keeping only the result of the first.
*
* @since 2.5.0
*/
export var apFirst = /*#__PURE__*/ apFirst_(Apply);
/**
* Combine two effectful actions, keeping only the result of the second.
*
* @since 2.5.0
*/
export var apSecond = /*#__PURE__*/ apSecond_(Apply);
/**
* @category instances
* @since 2.7.0
*/
export var Applicative = {
URI: URI,
map: _map,
ap: _ap,
of: of
};
/**
* @category instances
* @since 2.10.0
*/
export var Chain = {
URI: URI,
map: _map,
ap: _ap,
chain: _chain
};
/**
* @category instances
* @since 2.7.0
*/
export var Monad = {
URI: URI,
map: _map,
ap: _ap,
of: of,
chain: _chain
};
/**
* Composes computations in sequence, using the return value of one computation to determine the next computation and
* keeping only the result of the first.
*
* @example
* import * as RA from 'fp-ts/ReadonlyArray'
* import { pipe } from 'fp-ts/function'
*
* assert.deepStrictEqual(
* pipe(
* [1, 2, 3],
* RA.chainFirst(() => ['a', 'b'])
* ),
* [1, 1, 2, 2, 3, 3]
* )
* assert.deepStrictEqual(
* pipe(
* [1, 2, 3],
* RA.chainFirst(() => [])
* ),
* []
* )
*
* @category sequencing
* @since 2.5.0
*/
export var chainFirst =
/*#__PURE__*/ chainFirst_(Chain);
/**
* @category instances
* @since 2.7.0
*/
export var Unfoldable = {
URI: URI,
unfold: unfold
};
/**
* @category instances
* @since 2.7.0
*/
export var Alt = {
URI: URI,
map: _map,
alt: _alt
};
/**
* @category instances
* @since 2.11.0
*/
export var Zero = {
URI: URI,
zero: zero
};
/**
* @category do notation
* @since 2.11.0
*/
export var guard = /*#__PURE__*/ guard_(Zero, Pointed);
/**
* @category instances
* @since 2.7.0
*/
export var Alternative = {
URI: URI,
map: _map,
ap: _ap,
of: of,
alt: _alt,
zero: zero
};
/**
* @category instances
* @since 2.7.0
*/
export var Extend = {
URI: URI,
map: _map,
extend: _extend
};
/**
* @category instances
* @since 2.7.0
*/
export var Compactable = {
URI: URI,
compact: compact,
separate: separate
};
/**
* @category instances
* @since 2.7.0
*/
export var Filterable = {
URI: URI,
map: _map,
compact: compact,
separate: separate,
filter: _filter,
filterMap: _filterMap,
partition: _partition,
partitionMap: _partitionMap
};
/**
* @category instances
* @since 2.7.0
*/
export var FilterableWithIndex = {
URI: URI,
map: _map,
mapWithIndex: _mapWithIndex,
compact: compact,
separate: separate,
filter: _filter,
filterMap: _filterMap,
partition: _partition,
partitionMap: _partitionMap,
partitionMapWithIndex: _partitionMapWithIndex,
partitionWithIndex: _partitionWithIndex,
filterMapWithIndex: _filterMapWithIndex,
filterWithIndex: _filterWithIndex
};
/**
* @category instances
* @since 2.7.0
*/
export var Foldable = {
URI: URI,
reduce: _reduce,
foldMap: _foldMap,
reduceRight: _reduceRight
};
/**
* @category instances
* @since 2.7.0
*/
export var FoldableWithIndex = {
URI: URI,
reduce: _reduce,
foldMap: _foldMap,
reduceRight: _reduceRight,
reduceWithIndex: _reduceWithIndex,
foldMapWithIndex: _foldMapWithIndex,
reduceRightWithIndex: _reduceRightWithIndex
};
/**
* @category instances
* @since 2.7.0
*/
export var Traversable = {
URI: URI,
map: _map,
reduce: _reduce,
foldMap: _foldMap,
reduceRight: _reduceRight,
traverse: _traverse,
sequence: sequence
};
/**
* @category instances
* @since 2.7.0
*/
export var TraversableWithIndex = {
URI: URI,
map: _map,
mapWithIndex: _mapWithIndex,
reduce: _reduce,
foldMap: _foldMap,
reduceRight: _reduceRight,
reduceWithIndex: _reduceWithIndex,
foldMapWithIndex: _foldMapWithIndex,
reduceRightWithIndex: _reduceRightWithIndex,
traverse: _traverse,
sequence: sequence,
traverseWithIndex: _traverseWithIndex
};
/**
* @category sequencing
* @since 2.11.0
*/
export var chainRecDepthFirst = function (f) {
return function (a) {
var todo = __spreadArray([], f(a), true);
var out = [];
while (todo.length > 0) {
var e = todo.shift();
if (_.isLeft(e)) {
todo.unshift.apply(todo, f(e.left));
}
else {
out.push(e.right);
}
}
return out;
};
};
/**
* @category instances
* @since 2.11.0
*/
export var ChainRecDepthFirst = {
URI: URI,
map: _map,
ap: _ap,
chain: _chain,
chainRec: _chainRecDepthFirst
};
/**
* @category sequencing
* @since 2.11.0
*/
export var chainRecBreadthFirst = function (f) {
return function (a) {
var initial = f(a);
var todo = [];
var out = [];
function go(e) {
if (_.isLeft(e)) {
f(e.left).forEach(function (v) { return todo.push(v); });
}
else {
out.push(e.right);
}
}
for (var _i = 0, initial_1 = initial; _i < initial_1.length; _i++) {
var e = initial_1[_i];
go(e);
}
while (todo.length > 0) {
go(todo.shift());
}
return out;
};
};
/**
* @category instances
* @since 2.11.0
*/
export var ChainRecBreadthFirst = {
URI: URI,
map: _map,
ap: _ap,
chain: _chain,
chainRec: _chainRecBreadthFirst
};
var _wither = /*#__PURE__*/ witherDefault(Traversable, Compactable);
var _wilt = /*#__PURE__*/ wiltDefault(Traversable, Compactable);
/**
* @category instances
* @since 2.7.0
*/
export var Witherable = {
URI: URI,
map: _map,
compact: compact,
separate: separate,
filter: _filter,
filterMap: _filterMap,
partition: _partition,
partitionMap: _partitionMap,
reduce: _reduce,
foldMap: _foldMap,
reduceRight: _reduceRight,
traverse: _traverse,
sequence: sequence,
wither: _wither,
wilt: _wilt
};
/**
* Filter values inside a context.
*
* @example
* import { pipe } from 'fp-ts/function'
* import * as RA from 'fp-ts/ReadonlyArray'
* import * as T from 'fp-ts/Task'
*
* const filterE = RA.filterE(T.ApplicativePar)
* async function test() {
* assert.deepStrictEqual(
* await pipe(
* [-1, 2, 3],
* filterE((n) => T.of(n > 0))
* )(),
* [2, 3]
* )
* }
* test()
*
* @since 2.11.0
*/
export var filterE = /*#__PURE__*/ filterE_(Witherable);
/**
* @category instances
* @since 2.11.0
*/
export var FromEither = {
URI: URI,
fromEither: fromEither
};
/**
* @category lifting
* @since 2.11.0
*/
export var fromEitherK = /*#__PURE__*/ fromEitherK_(FromEither);
// -------------------------------------------------------------------------------------
// unsafe
// -------------------------------------------------------------------------------------
/**
* @category unsafe
* @since 2.5.0
*/
export var unsafeInsertAt = RNEA.unsafeInsertAt;
/**
* @category unsafe
* @since 2.5.0
*/
export var unsafeUpdateAt = function (i, a, as) {
return isNonEmpty(as) ? RNEA.unsafeUpdateAt(i, a, as) : as;
};
/**
* @category unsafe
* @since 2.5.0
*/
export var unsafeDeleteAt = function (i, as) {
var xs = as.slice();
xs.splice(i, 1);
return xs;
};
/**
* @category conversions
* @since 2.5.0
*/
export var toArray = function (as) { return as.slice(); };
/**
* @category conversions
* @since 2.5.0
*/
export var fromArray = function (as) { return (isEmpty(as) ? empty : as.slice()); };
// -------------------------------------------------------------------------------------
// utils
// -------------------------------------------------------------------------------------
/**
* An empty array
*
* @since 2.5.0
*/
export var empty = RNEA.empty;
export function every(predicate) {
return function (as) { return as.every(predicate); };
}
/**
* Check if a predicate holds true for any array member.
*
* @example
* import { some } from 'fp-ts/ReadonlyArray'
* import { pipe } from 'fp-ts/function'
*
* const isPositive = (n: number): boolean => n > 0
*
* assert.deepStrictEqual(pipe([-1, -2, 3], some(isPositive)), true)
* assert.deepStrictEqual(pipe([-1, -2, -3], some(isPositive)), false)
*
* @since 2.9.0
*/
export var some = function (predicate) {
return function (as) {
return as.some(predicate);
};
};
/**
* Alias of [`some`](#some)
*
* @since 2.11.0
*/
export var exists = some;
/**
* Places an element in between members of a `ReadonlyArray`, then folds the results using the provided `Monoid`.
*
* @example
* import * as S from 'fp-ts/string'
* import { intercalate } from 'fp-ts/ReadonlyArray'
*
* assert.deepStrictEqual(intercalate(S.Monoid)('-')(['a', 'b', 'c']), 'a-b-c')
*
* @since 2.12.0
*/
export var intercalate = function (M) {
var intercalateM = RNEA.intercalate(M);
return function (middle) { return match(function () { return M.empty; }, intercalateM(middle)); };
};
// -------------------------------------------------------------------------------------
// do notation
// -------------------------------------------------------------------------------------
/**
* @category do notation
* @since 2.9.0
*/
export var Do = /*#__PURE__*/ of(_.emptyRecord);
/**
* @category do notation
* @since 2.8.0
*/
export var bindTo = /*#__PURE__*/ bindTo_(Functor);
var let_ = /*#__PURE__*/ let__(Functor);
export {
/**
* @category do notation
* @since 2.13.0
*/
let_ as let };
/**
* @category do notation
* @since 2.8.0
*/
export var bind = /*#__PURE__*/ bind_(Chain);
/**
* @category do notation
* @since 2.8.0
*/
export var apS = /*#__PURE__*/ apS_(Apply);
// -------------------------------------------------------------------------------------
// deprecated
// -------------------------------------------------------------------------------------
/**
* Use `ReadonlyNonEmptyArray` module instead.
*
* @category zone of death
* @since 2.5.0
* @deprecated
*/
export var range = RNEA.range;
/**
* Use [`prepend`](#prepend) instead.
*
* @category zone of death
* @since 2.5.0
* @deprecated
*/
export var cons = RNEA.cons;
/**
* Use [`append`](#append) instead.
*
* @category zone of death
* @since 2.5.0
* @deprecated
*/
export var snoc = RNEA.snoc;
/**
* Use [`prependAll`](#prependall) instead.
*
* @category zone of death
* @since 2.9.0
* @deprecated
*/
export var prependToAll = prependAll;
/**
* This instance is deprecated, use small, specific instances instead.
* For example if a function needs a `Functor` instance, pass `RA.Functor` instead of `RA.readonlyArray`
* (where `RA` is from `import RA from 'fp-ts/ReadonlyArray'`)
*
* @category zone of death
* @since 2.5.0
* @deprecated
*/
export var readonlyArray = {
URI: URI,
compact: compact,
separate: separate,
map: _map,
ap: _ap,
of: of,
chain: _chain,
filter: _filter,
filterMap: _filterMap,
partition: _partition,
partitionMap: _partitionMap,
mapWithIndex: _mapWithIndex,
partitionMapWithIndex: _partitionMapWithIndex,
partitionWithIndex: _partitionWithIndex,
filterMapWithIndex: _filterMapWithIndex,
filterWithIndex: _filterWithIndex,
alt: _alt,
zero: zero,
unfold: unfold,
reduce: _reduce,
foldMap: _foldMap,
reduceRight: _reduceRight,
traverse: _traverse,
sequence: sequence,
reduceWithIndex: _reduceWithIndex,
foldMapWithIndex: _foldMapWithIndex,
reduceRightWithIndex: _reduceRightWithIndex,
traverseWithIndex: _traverseWithIndex,
extend: _extend,
wither: _wither,
wilt: _wilt
};