lazy-iter.js v0.1.16
immutable iterator that supports lazy evaluation and chain methods
for more infomation about Maybe
, Just
and Nothing
, see error-null-handle
install
npm install lazy-iter.js
API reference
- append
- chain
- chunks
- compact
- concat
- count
- cycle
- dedup
- dedupBy
- dedupByKey
- each
- enumerate
- eq
- eqBy
- every
- filter
- filterMap
- flatMap
- flat
- find
- findIndex
- findMap
- first
- groupToMap
- groupToObject
- interleave
- interleaveShortest
- inspect
- intersperse
- isEmpty
- isIter
- isUnique
- isUniqueByKey
- iter
- join
- last
- map
- max
- maxBy
- maxByKey
- merge
- mergeBy
- mergeByKey
- min
- minBy
- minByKey
- ne
- neBy
- nth
- partition
- prepend
- range
- reduce
- repeat
- rev
- scan
- skip
- skipWhile
- slice
- some
- stepBy
- take
- takeWhile
- toArray
- toMap
- toObject
- toSet
- toString
- unique
- uniqueByKey
- withPosition
- zip
- zipWith
API to create Iter
instance
iter :: \(iterable: Iterable\) => Iter\
Creates an
Iter
object from the given value. If no value ornull
is given, returns an emptyIter
. If the value is an iterable, returns anIter
that yields each value from the iterable. If the value is any other type, returns anIter
that yields the value once.repeat :: \(value: T | (() => T)) => Iter\
Repeat the given value endlessly.
range :: (config: RangeConfig | number = {}, end?: number, step?: number) => Iter\
Generates a sequence of numbers within a specified range.
type predicate
isIter :: \(value: unknown) => value is Iter\
Test if a variable is an
Iter
instance.
Iter<T>
instance methods
lazy evaluation methods
append :: (item: T): Iter\
Append a single element to the end of an
Iter
.chain :: (other: Iterable\) => Iter\
Concatenates the current
Iter
with the given iterable.chunks :: (size: number): Iter\<Iter\>
Split an
Iter
into chunks of the given size. Panics ifsize
is not a positive integer.compact :: Iter\<Compacted\>
Remove all
null
andundefined
elements from theIter
, and unwrapsMaybe
values.concat :: (...others: Iterable\[]) => Iter\
Concatenates the given iterators to this
Iter
.cycle :: () => Iter\
Repeats the
Iter
endlessly.dedup :: () => Iter\
Removes all but the first of consecutive duplicate elements in the
Iter
. Duplicates are detected using deep equality.dedupBy :: (sameBucket: (a: T, b: T) => boolean) => Iter\
Removes all but the first of consecutive elements in the
Iter
satisfying a given equality relation. Consecutive elementsa
andb
are considered duplicates ifsameBucket(a, b)
returns true.dedupByKey :: \(getKey: (value: T) => K) => Iter\
Removes all but the first of consecutive elements in the iterator based on a key derived from each element. Consecutive elements are considered duplicates if they map to the same key (tests with
Object.is
).enumerate :: () => Iter<number, T>
Creates an
Iter
which gives the current iteration count as well as the value.filter :: (fn: (value: T) => boolean) => Iter\
Filters the values in the current
Iter
using the provided predicate function.filterMap :: \(fn: (value: T) => U | undefined | null | Maybe\) => Iter\
Filter and map the values in the current
Iter
using the provided function. The filter function takes a value of typeT
and returns a value of typeU | undefined | null | Maybe<U>
. The resulting iterator will only contain the values where the filter function returns a non-null
/undefined
value. If the filter function returns aMaybe
, the resulting iterator will only contain the values where the filter function returns aJust
value.flatMap :: \(fn: (value: T) => Iterable\) => Iter\
Maps each value of the current
Iter
to anIter
using the given function and flattens the result.flat :: \(depth?: D): FlattedIter\<T, D>
Flattens an iterable to a specified depth. Panics if the depth is not a positive integer.
inspect :: (fn: (value: T) => void) => Iter\
Does something with each element of an Iter, passing the value on. When using iterators, you’ll often chain several of them together. While working on such code, you might want to check out what’s happening at various parts in the pipeline. To do that, insert a call to inspect().
It’s more common for inspect() to be used as a debugging tool than to exist in your final code, but applications may find it useful in certain situations when errors need to be logged before being discarded.
interleave :: (other: Iterable\): Iter\
Alternate elements from two iterators until both have run out.
interleaveShortest :: (other: Iterable\): Iter\
Alternate elements from two iterators until at least one of them has run out.
intersperse :: (value: T) => Iter\
Returns a new
Iter
that intersperses the given value between each of the elements of the currentIter
. The newIter
will yield the first element of the currentIter
, then the given value, then the second element of the currentIter
, then the given value again, and so on. The given value can be a function that returns a value, in which case the value returned by the function will be used as the interspersed value.map :: \(fn: (value: T) => U) => Iter\
Creates a new
Iter
by applying the given function to each value in the currentIter
.merge :: (other: Iterable\): Iter\
Merge the current
Iter
with the given iterable. The merged iterator will yield elements in ascending order. If two elements are equal, the first element from the currentIter
will come first. If both base iterators are sorted (ascending), the result is sorted.mergeBy :: (other: Iterable\, isFirst: (a: T, b: T) => boolean): Iter\
Merges the current
Iter
with the given iterable using the providedisFirst
function. TheisFirst
function takes two values, first from the currentIter
and second from the given iterable, and returns true if the first value should be yielded before the second value.mergeByKey :: \(other: Iterable\, getKey: (value: T) => K): Iter\
Merges the current
Iter
with the given iterable using the given function to extract a key from each element. The elements are merged in ascending order of their keys.partition :: (fn: (value: T) => boolean) => Iter\, Iter\
Splits the current
Iter
into two separateIter
instances based on the provided predicate function. The firstIter
contains all elements for which the predicate function returns true, and the secondIter
contains all elements for which the predicate function returns false.prepend :: (item: T): Iter\
Prepends a single element to the beginning of the
Iter
.scan :: \(fn: (acc: U, value: T) => U | null | undefined | Maybe\, initial: U) => Iter\
Similar to reduce, but yields all intermediate results. The function given to this method takes an accumulator and a value from the current
Iter
, and returns the next accumulator value. If the function returns null or undefined, the iteration stops. If the function returns aMaybe
that is aNothing
, the iteration also stops.skip :: (n: number) => Iter\
Skips the first
n
elements of theIter
and returns a newIter
starting from the (n+1)th element. Panics ifn
is not a natural integer.skipWhile :: (shouldSkip: (value: T) => boolean) => Iter\
Skips elements in the
Iter
until the given function returns false. After the first false result, the rest of the elements are yielded.slice :: (start: number, end: number): Iter\
Returns a new
Iter
that yields the elements of the currentIter
in the given range. Panics if the start index is greater than the end index. Panics if the start or end index is not a natural integer.stepBy :: (step: number): Iter\
Creates an
Iter
starting at the start point, but stepping by the given amount at each iteration. Note the first element of the iterator will always be returned, regardless of the step given. Panics if the step amount is not a positive integer.take :: (n: number) => Iter\
Takes the first
n
values from the currentIter
. If the currentIter
is shorter thann
, the resultingIter
will be shorter thann
. Panics ifn
is not a natural integer.takeWhile :: (shouldTake: (value: T) => boolean) => Iter\
Takes elements from the
Iter
as long as the given function returns true. Once the function returns false, the iteration stops.unique :: (): Iter\
Return an
Iter
that filters out elements that have already been produced once during the iteration. Duplicates are detected by comparing the elements by value and reference. The values are stored in a set in the iterator. TheIter
is stable, returning the non-duplicate items in the order in which they occur in the adaptedIter
. In a set of duplicate items, the first item encountered is the item retained.uniqueByKey :: \(fn: (value: T) => V): Iter\
Return an
Iter
that filters out elements that have already been produced once during the iteration. Duplicates are detected by comparing the key they map to with the keying functionfn
by hash and equality. The keys are stored in a hash set in the iterator. TheIter
is stable, returning the non-duplicate items in the order in which they occur in the adaptedIter
. In a set of duplicate items, the first item encountered is the item retained.withPosition :: : Iter<Position, T>
Returns an
Iter
that yields tuples containing the position of each element in the originalIter
and the element itself. The position is represented by thePosition
enum, which includesFirst
,Middle
,Last
, andOnly
.- If the
Iter
has only one element, it is marked asOnly
. - The first element is marked as
First
. - The last element is marked as
Last
. - All other elements are marked as
Middle
.
- If the
zip :: \(other: Iterable\) => Iter<T, U>
Zip this
Iter
with another iterator. This method takes another iterator and returns a newIter
that yields tuples of elements from both iterators. The newIter
will stop when either iterator stops.zipWith :: \<V, U = unknown>(other: Iter\, fn: (a: T, b: U) => V): Iter\
Creates a new
Iter
that pairs each element of the currentIter
with the corresponding element of anotherIter
, and applies a function to each pair, yielding the results. The newIter
will stop when either iterator stops.
eager evaluation methods
count :: () => number
Returns the number of items in the
Iter
.each :: (fn: (value: T) => void) => void
Executes the given function once for each element in the
Iter
.eq :: (other: Iter\) => boolean
Tests if the current
Iter
is equal to the givenIter
. TwoIter
s are considered equal if they contain the same elements(tests with deep equality) in the same order.eqBy :: (other: Iter\, fn: (a: T, b: T) => boolean) => boolean
Tests if the current
Iter
is equal to the givenIter
according to the given equality function. TwoIter
s are considered equal if they contain the same elements in the same order. The equality function takes two values of typeT
and returns a boolean that indicates the two values are equalare equal.every :: (fn: (value: T) => boolean) => boolean
Checks if every element of the
Iter
satisfies the given predicate.find :: (fn: (value: T) => boolean) => Maybe\
Returns the first value wrapped in
Just<T>
in theIter
for which the given predicate function returns true. If no element satisfies the predicate, returnsNothing
.findIndex :: (fn: (value: T) => boolean) => Maybe\
Returns the index of the first element wrapped in
Just<number>
in theIter
that satisfies the given predicate function. If no element satisfies the predicate, returnsNothing
.findMap :: \(fn: (value: T) => U | undefined | null | Maybe\): Maybe\
Applies function to the elements of
Iter
and returns the first valid result.iter.findMap(fn)
is equivalent toiter.filterMap(fn).first()
.first :: : Maybe\
Returns the first value wrapped in
Just<T>
in theIter
. If theIter
is empty, returnsNothing
.groupToMap :: \(keySelector: (value: T) => K): Map\<K, Iter\>
Reduces the
Iter
to aMap
where the keys are values returned by the given function, and the values areIter
s of the elements that were grouped by the given function.groupToObject :: \(keySelector: (value: T) => K): Record\<k, Iter\>
Reduces the
Iter
to aObject
where the keys are values returned by the given function, and the values areIter
s of the elements that were grouped by the given function.isEmpty :: (): boolean
Tests if the current
Iter
contains no elements.isUnique :: (): boolean
Tests if the current
Iter
contains non duplicate elements. Duplicates are detected by by hash and equality.isUniqueByKey :: \(fn: (value: T) => K): boolean
Tests if the current
Iter
contains non duplicate elements according to the given keying function. Duplicates are detected by comparing the key they map to with the keying functionfn
by hash and equality. The keys are stored in aSet
in the iterator.join :: (sep: string): string
Join all elements of the
Iter
into a single string, separated by the given separator.last :: () => Maybe\
Returns the last value in the
Iter
, wrapped inJust<T>
. If theIter
is empty, returnsNothing
.max :: () => Maybe\
Returns the maximum value in the
Iter
. If several elements are equally maximum, the last element is returned. If theIter
is empty, returnsNothing
.maxBy :: (fn: (a: T, b: T) => Ordering): Maybe\
Returns the element that gives the maximum value with respect to the specified comparison function. If several elements are equally maximum, the last element is returned. If the
Iter
is empty, returnsNothing
.maxBykey :: (fn: (value: T) => Comparable) => Maybe\
Returns the element that gives the maximum value from the specified function. If several elements are equally maximum, the last element is returned. If the
Iter
is empty, returnsNothing
.min :: () => Maybe\
Returns the minimum value in the
Iter
. If several elements are equally minimum, the first element is returned. If theIter
is empty, returnsNothing
.minBy :: (fn: (a: T, b: T) => Ordering) => Maybe\
Returns the element that gives the minimum value from the specified function. If several elements are equally minimum, the first element is returned. If the
Iter
is empty, returnsNothing
.minByKey :: (fn: (value: T) => Comparable) => Maybe\
Returns the element that gives the minimum value from the specified function. If several elements are equally minimum, the first element is returned. If the
Iter
is empty, returnsNothing
.ne :: (item: T): Iter\
Checks if the current
Iter
is not equal to the givenIter
. TwoIter
s are considered not equal if they contain different elements (tests with deep equality) in the same order, or if they have different lengths.neBy :: (other: Iter\, fn: (a: T, b: T) => boolean): boolean
Tests if the current
Iter
is not equal to the givenIter
using the given comparison function. TwoIter
s are considered not equal if they contain the different elements in the same order, or if they have different lengths. The comparison function takes two values of typeT
and returns a boolean that indicates the two values are not equal.nth :: (n: number) => Maybe\
Returns the nth element of the
Iter
, if it exists.some :: (fn: (value: T) => boolean) => boolean
Checks if any element of the
Iter
satisfies the given predicate function.reduce :: \(fn: (acc: U, value: T) => U, initial: U) => U
Reduces the
Iter
to a single value using the provided reducer function and an initial accumulator value.toArray :: () => T[]
Returns an array containing all elements of the
Iter
.toMap :: \<K, V>(toEntry: (value: T) => K, V): Map\<K, V>
Converts an
Iter
to aMap
. The provided functiontoEntry
takes a value of typeT
and returns a tuple of type[K, V]
. The resultingMap
will have the resulting keys of typeK
and the resulting values of typeV
.toObject :: \<K, V>(toEntry: (value: T) => K, V): Record\<K, V>
Returns a new object, mapping each element of the
Iter
to its corresponding entry in the object. The provided functiontoEntry
takes a value of typeT
and returns a tuple of type[K, V]
. The resultingObject
will have the resulting keys of typeK
and the resulting values of typeV
.toSet :: () => Set\
Converts the
Iter
to aSet
. Note that the order of the elements in the resultingSet
is not guaranteed to be the same as the order of elements in the originalIter
, because theIter
might contain duplicates.toString :: () => string
Returns a string representation of the
Iter
.
eager evaluate values and return lazy Iter
rev :: () => Iter\
Reverses an
Iter
's direction. Usually,Iter
s iterate from left to right. After using rev(), anIter
will instead iterate from right to left. This is only possible if theIter
has an end.
enum
Position
const enum Position { First = 'first', Middle = 'middle', Last = 'last', Only = 'only', }
Exported as object
P
andPosition
.import { P } from 'lazy-iter.js'
import { Position } from 'lazy-iter.js'
P.First
: This is the first element.P.Middle
: This is neither the first nor the last element.P.Last
: This is the last element.P.Only
: This is the only element.
Ordering
const enum Ordering { Less = -1, Equal = 0, Greater = 1, }
Exported as object
O
andOrdering
.import { O } from 'lazy-iter.js'
import { Ordering } from 'lazy-iter.js'
O.Less
: The first element is less than the secendO.Equal
: The two elements are equal.O.Greater
: The first element is greater than the second.