Iterables
The Module Iterables module contains functions working with iterables and generators. These functions work with synchronous and asynchronous input.
Filtering/finding
filter yields values from iterator that match a predicate function. A predicate function is takes an input value and returns true or false. filter
essentially allows iterating over a sub-set of the input data:
dropWhile is the opposite of filter
, yielding values that do not match the predicate.
find Returns first value from iterator that matches predicate.
some Returns true if a value matching a predicate is found.
reduce is useful when you need to collapse the values of an iterable down to a single value. For example, you may have an iterable of numbers and you want a single result, the sum of all values.
reduce
’s first parameter is the source iterable, then a reducer function, and then a start value.
The reducer function takes two parameters, the accumulated value and the current value at this position in the iteration.
In the above example, we sum the currently-accumulated value with the current value, and use a starting value of 0.
Re-shaping
map yields values transformed through a function.
chunks breaks up an iterable into certain sized ‘chunk’ of values. Each chunk is given as an array.
concat combines several iterables, yielding each result from one iterable, then the next and so on. It’s a bit like combining arrays with: const joined = [...array1, ....array2]
flatten returns values from an iterable, but if the value is an array, returns values from within the array.
slice yields a subset of an iterable from a specified start number of steps to an end number of steps, or the completion of the iterable.
Conversions
toArray : Copy contents of iterable to an array. While this can be done with in-built JS functions, ixfx’s toArray
allows you to set limits for the number of items, or duration. This is useful when dealing with infinite generators.
fromArray : Yields values from an array with a delay between each.
fromIterable : Yields values from an input iterable over time.
fromEvent : Yields values from an event (eg a pointer move)
fromFunction : Yields values from a function.
fromFunctionAwaited : Yields values from an asynchronous function.
Synchronising
combineLatestToArray : Values from several iterables are emitted as a batch whenever one of the iterables emits a value. If an iterable has not yet emitted a value, undefined is used.
The output array’s indexes match the index of the sources when calling combineLatestToArray
. Eg in the above example the value from data1
will be in the first position of array v
.
zip combines items at same position. This is useful for matching items between iterables. Only use this when you are sure that the order of iterables makes sense between iterables, eg fourth iteration step in all the input iterables connects data logically.
Comparisons
every Returns true if predicate is true for every item in iterable
equals Returns true if values in two iterables are the same at the same location, judged by a provided equality function.
Order matters with equals
. If two iterables have the same data but at different locations, it will return false.
max /min Yields the currently highest max/min value from an iterable, based on a given ranking function.
unique Return a set of unique items, compared by reference. Ie, that they are the same object, rather than same value.
For working immutable values, use uniqueByValue which compares by value.
Importing
Importing Module Iterables