UNPKG

macoolka-collection

Version:

`macoolka-collection` Define Data Collection Interface.

184 lines 6.62 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.initSubset = void 0; var O = require("fp-ts/Option"); var pipeable_1 = require("fp-ts/pipeable"); var function_1 = require("fp-ts/function"); function initSubset(option) { var slice = option.slice, notEmpty = option.notEmpty, size = option.size, empty = option.empty, findFirstIndex = option.findFirstIndex; /** * Get all but the first element of an list, creating a new list, or `None` if the list is empty * * @example * import { tail } from 'fp-ts/List' * import { some, none } from 'fp-ts/Option' * * assert.deepStrictEqual(tail([1, 2, 3]), some([2, 3])) * assert.deepStrictEqual(tail([]), none) * * @since 0.5.0 */ function tail(as) { return (0, pipeable_1.pipe)(as, O.fromPredicate(notEmpty), O.map(function () { return slice(1)(as); })); } /** * Get all but the last element of an list, creating a new list, or `None` if the list is empty * * @example * import { head } from 'fp-ts/List' * import { some, none } from 'fp-ts/Option' * * assert.deepStrictEqual(head([1, 2, 3]), some([1, 2])) * assert.deepStrictEqual(head([]), none) * * @since 0.5.0 */ function head(as) { return (0, pipeable_1.pipe)(as, O.fromPredicate(notEmpty), O.map(function () { return slice(0, size(as) - 1)(as); })); } /** * Keep only a number of elements from the start of an list, creating a new list. * `n` must be a natural number * * @example * import { takeLeft } from 'fp-ts/List' * * assert.deepStrictEqual(takeLeft(2)([1, 2, 3]), [1, 2]) * * @since 0.5.0 */ function takeLeft(n) { return function (as) { return n <= 0 ? empty() : slice(0, n)(as); }; } /** * Keep only a number of elements from the end of an list, creating a new list. * `n` must be a natural number * * @example * import { takeRight } from 'fp-ts/List' * * assert.deepStrictEqual(takeRight(2)([1, 2, 3, 4, 5]), [4, 5]) * * @since 0.5.0 */ function takeRight(n) { return function (as) { return n <= 0 ? empty() : slice(-n)(as); }; } function takeLeftWhile(predicate) { return function (as) { return (0, pipeable_1.pipe)(as, findFirstIndex((0, function_1.not)(predicate)), O.map(function (index) { return index === 0 ? empty() : slice(0, index)(as); }), O.getOrElse(function () { return as; })); }; } function takeLeftUntil(predicate) { return function (as) { return (0, pipeable_1.pipe)(as, findFirstIndex((predicate)), O.map(function (index) { return index === 0 ? empty() : slice(0, index)(as); }), O.getOrElse(function () { return as; })); }; } function spanLeft(predicate) { return function (as) { var init = takeLeftWhile(predicate)(as); return { init: init, rest: slice(size(init))(as) }; }; } /** * Skip a number of elements from the start of an list, creating a new list * * @example * import { dropLeft } from 'fp-ts/List' * * assert.deepStrictEqual(dropLeft(2)([1, 2, 3]), [3]) * * @since 0.5.0 */ function skipLeft(n) { return function (as) { return n < 0 ? empty() : slice(n)(as); }; } /** * Skip a number of elements from the end of an list, creating a new list * * @example * import { dropRight } from 'fp-ts/List' * * assert.deepStrictEqual(dropRight(2)([1, 2, 3, 4, 5]), [1, 2, 3]) * * @since 0.5.0 */ function skipRight(n) { return function (as) { return size(as) - n < 0 ? empty() : slice(0, size(as) - n)(as); }; } /** * Remove the longest initial subarray for which all element satisfy the specified predicate, creating a new list * * @example * import { skipLeftWhile } from 'fp-ts/List' * * assert.deepStrictEqual(skipLeftWhile((n: number) => n % 2 === 1)([1, 3, 2, 4, 5]), [2, 4, 5]) * * @since 0.5.0 */ function skipLeftWhile(predicate) { return function (as) { return (0, pipeable_1.pipe)(as, findFirstIndex((0, function_1.not)(predicate)), O.map(function (index) { return index === 0 ? as : slice(index)(as); }), O.getOrElse(function () { return empty(); })); }; } /** * Returns a new `Collection` of the same type which includes entries starting from when predicate first returns true. * * @example * import { skipLeftUntil } from 'fp-ts/List' * * assert.deepStrictEqual(skipLeftUntil((n: number) => n % 2 === 0)([1, 3, 2, 4, 5]), [2, 4, 5]) * * @since 0.5.0 */ function skipLeftUntil(predicate) { return function (as) { return (0, pipeable_1.pipe)(as, findFirstIndex((predicate)), O.map(function (index) { return index === 0 ? as : slice(index)(as); }), O.getOrElse(function () { return empty(); })); }; } /** * Returns a new Collection with 0 size and no values in constant time. * * @example * import { clear } from 'fp-ts/List' * * assert.deepStrictEqual(clear([1, 2, 3, 4]), [])) * * @since 0.5.0 */ function clear(as) { return empty(); } /** * Splits an list into two pieces, the first piece has `n` elements. * * @example * import { splitAt } from 'fp-ts/List' * * assert.deepStrictEqual(splitAt(2)([1, 2, 3, 4, 5]), [[1, 2], [3, 4, 5]]) * * @since 0.5.0 */ function splitAt(n) { return function (as) { return [slice(0, n)(as), slice(n)(as)]; }; } return __assign(__assign({}, option), { head: head, tail: tail, takeLeft: takeLeft, takeLeftWhile: takeLeftWhile, takeLeftUntil: takeLeftUntil, takeRight: takeRight, skipLeft: skipLeft, skipLeftWhile: skipLeftWhile, skipLeftUntil: skipLeftUntil, skipRight: skipRight, splitAt: splitAt, spanLeft: spanLeft, clear: clear }); } exports.initSubset = initSubset; //# sourceMappingURL=Subset.js.map