UNPKG

1.45 kBTypeScriptView Raw
1import type { Maybe } from "./null.js";
2/**
3 * Lisp-like sequence abstraction for arbitrary types using `first` &
4 * `next` operations only.
5 *
6 * @remarks
7 * Unlike ES6 iterators this approach does not conflate both operations
8 * and `first()` can be called any number of times to obtain the current
9 * value (if any) from the sequence.
10 */
11export interface ISeq<T> {
12 /**
13 * Returns the sequence's first value or `undefined` if there're no
14 * further values.
15 *
16 * @remarks
17 * If the sequence is guaranteed to not include `undefined` values,
18 * a simple check for `seq.first() === undefined` is sufficient to
19 * determine the end. If the sequence DOES contain `undefined`
20 * values, the check should use `seq.next()`.
21 */
22 first(): Maybe<T>;
23 /**
24 * Returns a new sequence of the remaining elements or `undefined`
25 * if there're no further values.
26 *
27 * @remarks
28 * In general, implementations of this interface MUST always return
29 * a new sequence instance and not mutate some internal cursor. I.e.
30 * `seq.next() !== seq`
31 */
32 next(): Maybe<ISeq<T>>;
33}
34/**
35 * Interface for data types providing an {@link ISeq} abstraction.
36 */
37export interface ISeqable<T> {
38 /**
39 * Returns an {@link ISeq} of the type's data or `undefined` if
40 * there're no values available. See {@link ISeq.next}
41 */
42 seq(): Maybe<ISeq<T>>;
43}
44//# sourceMappingURL=seq.d.ts.map
\No newline at end of file