lazy-iter.js
v0.1.16
Published
immutable iterator that supports lazy evaluation and chain methods
Downloads
23
Maintainers
Readme
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.jsAPI 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 :: <T>(iterable: Iterable<T>) => Iter<T>
Creates an
Iterobject from the given value. If no value ornullis given, returns an emptyIter. If the value is an iterable, returns anIterthat yields each value from the iterable. If the value is any other type, returns anIterthat yields the value once.repeat :: <T>(value: T | (() => T)) => Iter<T>
Repeat the given value endlessly.
range :: (config: RangeConfig | number = {}, end?: number, step?: number) => Iter<number>
Generates a sequence of numbers within a specified range.
type predicate
isIter :: <T>(value: unknown) => value is Iter<T>
Test if a variable is an
Iterinstance.
Iter<T> instance methods
lazy evaluation methods
append :: (item: T): Iter<T>
Append a single element to the end of an
Iter.chain :: (other: Iterable<T>) => Iter<T>
Concatenates the current
Iterwith the given iterable.chunks :: (size: number): Iter<Iter<T>>
Split an
Iterinto chunks of the given size. Panics ifsizeis not a positive integer.compact :: Iter<Compacted<T>>
Remove all
nullandundefinedelements from theIter, and unwrapsMaybevalues.concat :: (...others: Iterable<T>[]) => Iter<T>
Concatenates the given iterators to this
Iter.cycle :: () => Iter<T>
Repeats the
Iterendlessly.dedup :: () => Iter<T>
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<T>
Removes all but the first of consecutive elements in the
Itersatisfying a given equality relation. Consecutive elementsaandbare considered duplicates ifsameBucket(a, b)returns true.dedupByKey :: <K>(getKey: (value: T) => K) => Iter<T>
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
Iterwhich gives the current iteration count as well as the value.filter :: (fn: (value: T) => boolean) => Iter<T>
Filters the values in the current
Iterusing the provided predicate function.filterMap :: <U>(fn: (value: T) => U | undefined | null | Maybe<U>) => Iter<U>
Filter and map the values in the current
Iterusing the provided function. The filter function takes a value of typeTand 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/undefinedvalue. If the filter function returns aMaybe, the resulting iterator will only contain the values where the filter function returns aJustvalue.flatMap :: <U>(fn: (value: T) => Iterable<U>) => Iter<U>
Maps each value of the current
Iterto anIterusing the given function and flattens the result.flat :: <D extends number = 1>(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<T>
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<T>): Iter<T>
Alternate elements from two iterators until both have run out.
interleaveShortest :: (other: Iterable<T>): Iter<T>
Alternate elements from two iterators until at least one of them has run out.
intersperse :: (value: T) => Iter<T>
Returns a new
Iterthat intersperses the given value between each of the elements of the currentIter. The newIterwill 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 :: <U>(fn: (value: T) => U) => Iter<U>
Creates a new
Iterby applying the given function to each value in the currentIter.merge :: (other: Iterable<T>): Iter<T>
Merge the current
Iterwith the given iterable. The merged iterator will yield elements in ascending order. If two elements are equal, the first element from the currentIterwill come first. If both base iterators are sorted (ascending), the result is sorted.mergeBy :: (other: Iterable<T>, isFirst: (a: T, b: T) => boolean): Iter<T>
Merges the current
Iterwith the given iterable using the providedisFirstfunction. TheisFirstfunction takes two values, first from the currentIterand second from the given iterable, and returns true if the first value should be yielded before the second value.mergeByKey :: <K>(other: Iterable<T>, getKey: (value: T) => K): Iter<T>
Merges the current
Iterwith 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<T>, Iter<T>]
Splits the current
Iterinto two separateIterinstances based on the provided predicate function. The firstItercontains all elements for which the predicate function returns true, and the secondItercontains all elements for which the predicate function returns false.prepend :: (item: T): Iter<T>
Prepends a single element to the beginning of the
Iter.scan :: <U>(fn: (acc: U, value: T) => U | null | undefined | Maybe<U>, initial: U) => Iter<U>
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 aMaybethat is aNothing, the iteration also stops.skip :: (n: number) => Iter<T>
Skips the first
nelements of theIterand returns a newIterstarting from the (n+1)th element. Panics ifnis not a natural integer.skipWhile :: (shouldSkip: (value: T) => boolean) => Iter<T>
Skips elements in the
Iteruntil the given function returns false. After the first false result, the rest of the elements are yielded.slice :: (start: number, end: number): Iter<T>
Returns a new
Iterthat yields the elements of the currentIterin 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<T>
Creates an
Iterstarting 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<T>
Takes the first
nvalues from the currentIter. If the currentIteris shorter thann, the resultingIterwill be shorter thann. Panics ifnis not a natural integer.takeWhile :: (shouldTake: (value: T) => boolean) => Iter<T>
Takes elements from the
Iteras long as the given function returns true. Once the function returns false, the iteration stops.unique :: (): Iter<T>
Return an
Iterthat 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. TheIteris 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 :: <V>(fn: (value: T) => V): Iter<T>
Return an
Iterthat 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 functionfnby hash and equality. The keys are stored in a hash set in the iterator. TheIteris 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
Iterthat yields tuples containing the position of each element in the originalIterand the element itself. The position is represented by thePositionenum, which includesFirst,Middle,Last, andOnly.- If the
Iterhas 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 :: <U>(other: Iterable<U>) => Iter<[T, U]>
Zip this
Iterwith another iterator. This method takes another iterator and returns a newIterthat yields tuples of elements from both iterators. The newIterwill stop when either iterator stops.zipWith :: <V, U = unknown>(other: Iter<U>, fn: (a: T, b: U) => V): Iter<V>
Creates a new
Iterthat pairs each element of the currentIterwith the corresponding element of anotherIter, and applies a function to each pair, yielding the results. The newIterwill 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<T>) => boolean
Tests if the current
Iteris equal to the givenIter. TwoIters are considered equal if they contain the same elements(tests with deep equality) in the same order.eqBy :: (other: Iter<T>, fn: (a: T, b: T) => boolean) => boolean
Tests if the current
Iteris equal to the givenIteraccording to the given equality function. TwoIters are considered equal if they contain the same elements in the same order. The equality function takes two values of typeTand returns a boolean that indicates the two values are equalare equal.every :: (fn: (value: T) => boolean) => boolean
Checks if every element of the
Itersatisfies the given predicate.find :: (fn: (value: T) => boolean) => Maybe<T>
Returns the first value wrapped in
Just<T>in theIterfor which the given predicate function returns true. If no element satisfies the predicate, returnsNothing.findIndex :: (fn: (value: T) => boolean) => Maybe<number>
Returns the index of the first element wrapped in
Just<number>in theIterthat satisfies the given predicate function. If no element satisfies the predicate, returnsNothing.findMap :: <U>(fn: (value: T) => U | undefined | null | Maybe<U>): Maybe<U>
Applies function to the elements of
Iterand returns the first valid result.iter.findMap(fn)is equivalent toiter.filterMap(fn).first().first :: : Maybe<T>
Returns the first value wrapped in
Just<T>in theIter. If theIteris empty, returnsNothing.groupToMap :: <K>(keySelector: (value: T) => K): Map<K, Iter<T>>
Reduces the
Iterto aMapwhere the keys are values returned by the given function, and the values areIters of the elements that were grouped by the given function.groupToObject :: <k extends PropertyKey>(keySelector: (value: T) => K): Record<k, Iter<T>>
Reduces the
Iterto aObjectwhere the keys are values returned by the given function, and the values areIters of the elements that were grouped by the given function.isEmpty :: (): boolean
Tests if the current
Itercontains no elements.isUnique :: (): boolean
Tests if the current
Itercontains non duplicate elements. Duplicates are detected by by hash and equality.isUniqueByKey :: <K>(fn: (value: T) => K): boolean
Tests if the current
Itercontains non duplicate elements according to the given keying function. Duplicates are detected by comparing the key they map to with the keying functionfnby hash and equality. The keys are stored in aSetin the iterator.join :: (sep: string): string
Join all elements of the
Iterinto a single string, separated by the given separator.last :: () => Maybe<T>
Returns the last value in the
Iter, wrapped inJust<T>. If theIteris empty, returnsNothing.max :: () => Maybe<T>
Returns the maximum value in the
Iter. If several elements are equally maximum, the last element is returned. If theIteris empty, returnsNothing.maxBy :: (fn: (a: T, b: T) => Ordering): Maybe<T>
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
Iteris empty, returnsNothing.maxBykey :: (fn: (value: T) => Comparable) => Maybe<T>
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
Iteris empty, returnsNothing.min :: () => Maybe<T>
Returns the minimum value in the
Iter. If several elements are equally minimum, the first element is returned. If theIteris empty, returnsNothing.minBy :: (fn: (a: T, b: T) => Ordering) => Maybe<T>
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
Iteris empty, returnsNothing.minByKey :: (fn: (value: T) => Comparable) => Maybe<T>
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
Iteris empty, returnsNothing.ne :: (item: T): Iter<T>
Checks if the current
Iteris not equal to the givenIter. TwoIters 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<T>, fn: (a: T, b: T) => boolean): boolean
Tests if the current
Iteris not equal to the givenIterusing the given comparison function. TwoIters 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 typeTand returns a boolean that indicates the two values are not equal.nth :: (n: number) => Maybe<T>
Returns the nth element of the
Iter, if it exists.some :: (fn: (value: T) => boolean) => boolean
Checks if any element of the
Itersatisfies the given predicate function.reduce :: <U>(fn: (acc: U, value: T) => U, initial: U) => U
Reduces the
Iterto 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
Iterto aMap. The provided functiontoEntrytakes a value of typeTand returns a tuple of type[K, V]. The resultingMapwill have the resulting keys of typeKand 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
Iterto its corresponding entry in the object. The provided functiontoEntrytakes a value of typeTand returns a tuple of type[K, V]. The resultingObjectwill have the resulting keys of typeKand the resulting values of typeV.toSet :: () => Set<T>
Converts the
Iterto aSet. Note that the order of the elements in the resultingSetis not guaranteed to be the same as the order of elements in the originalIter, because theItermight contain duplicates.toString :: () => string
Returns a string representation of the
Iter.
eager evaluate values and return lazy Iter
rev :: () => Iter<T>
Reverses an
Iter's direction. Usually,Iters iterate from left to right. After using rev(), anIterwill instead iterate from right to left. This is only possible if theIterhas an end.
enum
Position
const enum Position { First = 'first', Middle = 'middle', Last = 'last', Only = 'only', }Exported as object
PandPosition.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
OandOrdering.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.
