Skip to content

Latest commit

 

History

History
385 lines (372 loc) · 37.3 KB

transducers.md

File metadata and controls

385 lines (372 loc) · 37.3 KB

Home > @thi.ng/transducers

transducers package

Classes

Class Description
Range Simple class wrapper around given range interval and implementing Iterable and IReducible interfaces, the latter is used to accelerate use with reduce().
Reduced

Functions

Function Description
add(init) Reducer to compute sum of values with given init value. Default: 0
add(xs)
add(init, xs)
asIterable(src) Helper function / generator to (re)provide given iterable in iterator form.
assocMap() Reducer accepting key-value pairs / tuples and transforming / adding them to an ES6 Map.
assocMap(xs)
assocObj() Reducer accepting key-value pairs / tuples and updating / adding them to an object.
assocObj(xs)
benchmark() Stateful transducer. Ignores the actual input values, but produces time measurements since last value processed, e.g. for use in async usage contexts.
benchmark(src)
comp(a) Transducer composition. Returns new transducer which applies given transducers in left-to-right order.
comp(a, b, c, d, e, f, g, h, i, j)
comp(a, b, c, d, e, f, g, h, i, j, fns)
comp(a, b)
comp(a, b, c)
comp(a, b, c, d)
comp(a, b, c, d, e)
comp(a, b, c, d, e, f)
comp(a, b, c, d, e, f, g)
comp(a, b, c, d, e, f, g, h)
comp(a, b, c, d, e, f, g, h, i)
concat(xs) Yields iterator producing concatenation of given iterables. Undefined & null inputs are silently ignored, however any such values produced or contained in an input will remain.
conj() Reducer. Like push(), but for ES6 Sets.
conj(xs)
converge(pred) Transducer which for each input x (apart from the very first one) applies given predicate pred to previous input and x. Only passes values downstream as long as the predicate returns a falsy result. Once the result is truthy, x is considered converged and the transformation is terminated (by emitting a reduced value).
converge(pred, src)
convolve1d(opts)
convolve1d(opts, indices)
convolve2d(opts)
convolve2d(opts, indices)
count(offset, step) Reducer which ignores incoming values and instead only counts them, optionally using given start and step counter values.
count(xs)
count(offset, xs)
count(offset, step, xs)
curve(start, end, steps, rate) Iterator producing an exponential curve (with adjustable curvature) between start and end values over num steps. This is the exponential equivalent of line.
cycle(input, num) Iterator which yields an infinite repetition of given input iterable's values. Produces no values if input is empty. If num is given, only that many cycles will be emitted.
dedupe(equiv)
dedupe(src)
dedupe(equiv, src)
distinct(opts)
distinct(src)
distinct(opts, src)
div(init) Reducer to compute successive division of values using given init value.
div(init, xs)
drop(n)
drop(n, src)
dropNth(n)
dropNth(n, src)
dropWhile(pred)
dropWhile(src)
dropWhile(pred, src)
dup(x) Returns the concatentation of x with itself. If input is an iterable, it MUST be finite!
dup(x)
dup(x)
duplicate(n)
duplicate(n, src)
every(pred) Reducer which applies optional pred function to each value and terminates early if the predicate returned a falsy result. If no predicate is given the values are checked via JS native truthiness rules (i.e. 0, "", false, null, undefined are all falsy).Returns true if *all* values passed test.
every(xs)
every(pred, xs)
extendSides(src, numLeft, numRight) Yields iterator of given iterable which repeats the first and/or last value(s) numLeft/numRight times (default: 1).
fill(start) Reducer which starts filling array with results from given start index (default: 0). Use fillN() for typed array targets (same impl, but provides correct result type).
fill(xs)
fill(start, xs)
fillN(start) Like fill() reducer, but for numeric arrays (incl. typed arrays).
fillN(xs)
fillN(start, xs)
filter(pred)
filter(pred, src)
filterFuzzy(query, opts) Returns transducer which calls fuzzyMatch for each value and discards all non-matching values.
filterFuzzy(query, src)
filterFuzzy(query, opts, src)
flatten()
flatten(src)
flattenWith(fn)
flattenWith(fn, src)
frequencies()
frequencies(xs)
frequencies(key)
frequencies(key, xs)
groupByMap(opts)
groupByMap(xs)
groupByMap(opts, xs)
groupByObj(opts)
groupByObj(xs)
groupByObj(opts, xs)
indexed(from)
indexed(src)
indexed(from, src)
interleave(sep)
interleave(sep, src)
interpolate(fn, window, n) Higher order interpolation transducer. The resulting transducer forms a sliding window and calls fn (the given interpolation function) n times with the current window and a normalized time value to produce the requested number of interpolated values per interval.
interpolate(fn, window, n, src)
interpolateHermite(n) Pre-configured version of interpolate() for numeric values and using cubic hermite interpolation.
interpolateHermite(n, src)
interpolateLinear(n) Pre-configured version of interpolate() for numeric values and using pairwise linear interpolation.
interpolateLinear(n, src)
interpose(sep)
interpose(sep, src)
iterate(fn, seed, num) Yields an infinite iterator of the inductive sequence:f(x+1) = f(f(x))
iterator(xform, xs) Takes a transducer and input iterable. Returns iterator of transformed results.
iterator1(xform, xs) Optimized version of iterator() for transducers which are guaranteed to:1) Only produce none or a single result per input 2) Do not require a completion reduction step
juxtR(r1) Composes a new reducer from the ones given, in order to produce multiple reductions in parallel from the same input.
juxtR(r1, r2)
juxtR(r1, r2, r3)
juxtR(r1, r2, r3, rs)
keep(pred)
keep(src)
keep(pred, src)
keys(x) Iterator which yields all names of given object's own properties (Similar to Object.keys()).
labeled(id)
labeled(id, src)
last()
last(xs)
map(fn) Transducer. Applies mapping function fn to each received value and passes result downstream to next reducer.
map(fn, src)
mapcat(fn) Transducer. Similar to map(), but expects the given mapping function fn to return an iterable result (or null) and then emits each value of the result individually downstream. null or undefined result values will be skipped / omitted.
mapcat(fn, src)
mapDeep(spec) Transducer. Same as map(deepTransform(spec))See deepTransform for details.
mapDeep(spec, src)
mapIndexed(fn, offset) Transducer. Similar to map(), but given fn takes two arguments: index and value to transform.
mapIndexed(fn, src)
mapIndexed(fn, offset, src)
mapKeys(keys, copy) Takes a keys object of transformation functions and returns a transducer which expects object values. For each input x then calls transformation functions for each key k in keys. I.e. executes keys[k](x[k], x) and reassigns result. By default creates a shallow copy of each x.
mapKeys(keys, src)
mapKeys(keys, copy, src)
mapNth(n, fn) Transducer. Similar to map(), but only transforms every n-th input value and passes intermediate values unchanged downstream.
mapNth(n, offset, fn)
mapNth(n, fn, src)
mapNth(n, offset, fn, src)
mapVals(fn, copy) Transducer. Similar to map(), but expects object values and the given function fn is applied to each enumerable property value and the results reassigned to their original keys.
mapVals(fn, src)
mapVals(fn, copy, src)
matchFirst(pred) Transducer composition / syntax sugar for:
matchFirst(pred, src)
matchLast(pred) Transducer composition / syntax sugar for:
matchLast(pred, src)
max()
max(xs)
maxCompare(init, cmp)
maxCompare(init, xs)
maxCompare(init, cmp, xs)
mean() Reducer computing mean of received inputs. Returns 0 if no inputs were processed.
mean(xs)
min()
min(xs)
minCompare(init, cmp)
minCompare(init, xs)
minCompare(init, cmp, xs)
movingAverage(period) Computes the Simple Moving Average of given period.
movingAverage(period, src)
movingMedian(n, opts) Transducer. Similar to movingAverage(), but yields median of sliding window and supports non-numeric inputs.
movingMedian(n, src)
movingMedian(n, opts, src)
mul(init) Reducer to compute product of values with optional init value (default: 1).
mul(xs)
mul(init, xs)
multiplex(a) Yields a new transducer which applies given transducers in parallel (using juxt() & step) and produces tuples of results.
multiplex(a, b)
multiplex(a, b, c)
multiplex(a, b, c, d)
multiplex(a, b, c, d, e)
multiplex(a, b, c, d, e, f)
multiplex(a, b, c, d, e, f, g)
multiplex(a, b, c, d, e, f, g, h)
multiplexObj(xforms, rfn) Transducer. Similar to (and building on) multiplex(), but takes an object of transducers and produces a result object for each input.
multiplexObj(xforms, src)
multiplexObj(xforms, rfn, src)
normRange(n, inclLast) Yields sequence of n+1 monotonically increasing numbers in the closed interval (0.0 .. 1.0). If n <= 0, yields nothing.
padLast(n, fill) Ensures the total number of transformed values will be multiples of n.
padLast(n, fill, src)
page(page, pageLen) Pagination helper. Returns transducer which extracts only items for given page number (and page length, default 10).
page(page, src)
page(page, pageLen, src)
pairs(x) Iterator yielding key-value pairs of given object's own properties and their values. Same as zip(keys(x), vals(x)).
palindrome(x) Returns the concatentation of x with its own duplicate in reverse order. If input is an iterable, it MUST be finite!
palindrome(x)
palindrome(x)
partition(size) Transducer to create overlapping and non-overlapping sliding windows of inputs. Window size and progress speed can be configured via size and step. By default only full / complete partitions are emitted. However, if all is true, the last partition is allowed to be incomplete / partially filled only.
partition(size, all)
partition(size, step)
partition(size, step, all)
partition(size, src)
partition(size, all, src)
partition(size, step, src)
partition(size, step, all, src)
partitionBy(fn, stateful) Transducer. Applies given fn to each incoming value and collects values until the return value of the fn has changed. Once this happens yields chunk of buffered values.
partitionBy(fn, src)
partitionBy(fn, stateful, src)
partitionOf(sizes) Transducer. Yields cyclic sequence of user defined variable sized chunks. The last partition emitted is allowed to be incomplete.
partitionOf(sizes, src)
partitionSort(n, opts) Transducer. Composition of partition() and mapcat() which yields a **partially** sorted sequence of input values. Sorting is performed on sliding / non-overlapping chunks of n inputs.
partitionSort(n, src)
partitionSort(n, opts, src)
partitionSync(keys, opts) Transducer intended for synchronization and provenance tracking of possibly previously merged inputs. Partitions the input into labeled tuple objects with the object keys obtained from the user provided keyfn (which is applied to each input value).
partitionSync(keys, src)
partitionSync(keys, opts, src)
partitionTime(period) Transducer. Yields tumbling, non-overlapping windows/partitions of input values, with the window size defined by given realtime period (in milliseconds).
partitionTime(period, src)
peek() Transducer version of , i.e. extracts the last item of an array.
peek(src)
permutations(a) Iterator yielding the Cartesian Product of the given iterables.
permutations(a, b)
permutations(a, b, c)
permutations(a, b, c, d)
permutations(src)
pluck(key) Transducer which looks up given key in each input and yields sequence of these values.
pluck(key, src)
push()
push(xs)
pushSort(cmp) Similar to push(), but sorts result array upon completion using optionally given comparator (default compare).
pushSort(cmp, xs)
range()
range(to)
range(from, to)
range(from, to, step)
range2d(toX, toY)
range2d(fromX, toX, fromY, toY)
range2d(fromX, toX, fromY, toY, stepX, stepY)
range3d(toX, toY, toZ)
range3d(fromX, toX, fromY, toY, fromZ, toZ)
range3d(fromX, toX, fromY, toY, fromZ, toZ, stepX, stepY, stepZ)
reduce(rfn, xs)
reduce(rfn, acc, xs)
reduce(rfn, xs)
reduce(rfn, acc, xs)
reductions(rfn)
reductions(rfn, xs)
rename(kmap, rfn)
rename(kmap, rfn, src)
repeat(x, n) Iterator yielding an infinite (by default) repetition of given value x. If n is given, only produces that many values.See also: repeatedly()
repeatedly(fn, n) Iterator yielding return values of given no-arg function fn. If n is given, only that many values will be produced, else the iterator is infinite.
reverse(input) Yields iterator which consumes input and yield its values in reverse order. Important: Input MUST be finite.
run(tx, xs) Transforms xs with given transducer and optional side effect without any reduction step. If fx is given it will be called with every value produced by the transducer. If fx is *not* given, the transducer is assumed to include at least one sideEffect step itself. Returns nothing.
run(tx, xs)
run(tx, fx, xs)
run(tx, fx, xs)
sample(prob) Transducer which only yields values with given prob probability (0.0 .. 1.0 range). Supports custom PRNGs via IRandom interface.
sample(prob, rnd)
sample(prob, src)
sample(prob, rnd, src)
scan(rfn, init) Transducer which performs "scan" operation via given reducer.
scan(rfn, init, src)
selectKeys(keys) Transducer which yields sequence of transformed objects, each only only containing the given keys. If a key's value is undefined (or missing entirely) it will be omitted in the result.
selectKeys(keys, src)
slidingWindow(size, partial) Sliding window transducer, similar to partition(size, 1), but supports initially partially filled windows, if partial is set to true (default). Each emitted window is a shallow copy of the internal accumulation buffer.
slidingWindow(size, src)
slidingWindow(size, partial, src)
some(pred) Similar to every() reducer, but only requires at least 1 value to succeed predicate test (and then immediately terminates with true as result).
some(xs)
some(pred, xs)
sortedKeys(x, cmp) Syntax sugar for Object.keys(x).sort() with support for custom comparator (default: compare) and yielding iterator of sorted keys.
str(sep)
str(sep, xs)
streamShuffle(n, maxSwaps) Transducer. Creates internal sliding window of n values and performs maxSwaps random shuffle operations for each new value and yields values in shuffled order. By default maxSwaps is the same as the chosen chunk size.
streamShuffle(n, src)
streamShuffle(n, maxSwaps, src)
streamSort(n, opts) Transducer. Similar to partitionSort(), however uses proper sliding window and insertion sort instead of fully sorting window as done by partitionSort.
streamSort(n, src)
streamSort(n, opts, src)
struct(fields) Higher-order transducer to converts linear input into structured objects using given field specs and ordering.
struct(fields, src)
sub(init) Reducer to successively subtract values from optional init value (default: 0).
sub(xs)
sub(init, xs)
swizzle(order) Transducer which performs value reordering on inputs using provided property order. Accepts arrays or objects as input, but always yields arrays.
swizzle(order, src)
symmetric(src) Yields an iterator of all src values, followed by the same values in reverse order. Efficiently builds the reversed order via an internal linked list.
take(n) Transducer which only yields the first n values and then terminates transformation (by emitting a reduced value).
take(n, src)
takeLast(n) Transducer which only yields the last n values. Assumes input source is finite (of course).
takeLast(n, src)
takeNth(n) Transducer which only yields every n-th value from the input source.
takeNth(n, src)
takeWhile(pred) Transducer which applies predicate pred to each input and only yields values as long as the predicate returned a truthy result. Once the result is falsy, transformation is terminated (by emitting a reduced value).
takeWhile(src)
takeWhile(pred, src)
throttle(pred) Similar to filter(), but works with possibly stateful predicates to achieve rate limiting capabilities. Emits only values when predicate returns a truthy value.
throttle(pred, src)
throttleTime(delay) Time-based version of throttle(). Ignores any new values in the delay interval since the last accepted value.
throttleTime(delay, src)
toggle(on, off, initial) Stateful transducer which accepts any input and flips between given on / off values for every value received. The initial state can be optionally provided (default: false) and must be given if used as an iterator.
toggle(on, off, initial, src)
transduce(tx, rfn)
transduce(tx, rfn, xs)
transduce(tx, rfn, xs)
transduce(tx, rfn, acc, xs)
transduce(tx, rfn, acc, xs)
tween(opts) Keyframe based interpolator. Yields a sequence of num+1 equally spaced, tweened values from given keyframe tuples (stops). Keyframes are defined as [time, value] tuples. Only values in the closed [min..max] time interval will be computed.
vals(x) Iterator which yields all values of given object's own properties (Similar to Object.values()).
wordWrap(lineLength, opts) Returns transducer partitioning words into variable sized arrays based on given lineLength (default 80).
wordWrap(lineLength, src)
wordWrap(lineLength, opts, src)
wrapSides(src, numLeft, numRight) Yields iterator of src with the last numLeft values of src prepended at the beginning and/or the first numRight values appended at the end.
zip(a) Accepts a number of iterables and combines them into an iterable of tuples of successively consumed input values.
zip(a, b)
zip(a, b, c)
zip(a, b, c, d)
zip(a, b, c, d, e)
zip(a, b, c, d, e, f)
zip(a, b, c, d, e, f, g)
zip(a, b, c, d, e, f, g, h)

Interfaces

Interface Description
Convolution1DOpts
Convolution2DOpts
ConvolutionOpts
DistinctOpts
FilterFuzzyOpts
GroupByOpts
IReducible
IXform Interface for types able to provide some internal functionality (or derive some related transformation) as Transducer. Implementations of this interface can be directly passed to all functions in this package where a Transducer arg is expected.
PartitionSyncOpts
Reducer
SortOpts
StructField
TransformSpec
TweenOpts
WordWrapOpts

Variables

Variable Description
$$reduce
buildKernel1d
buildKernel2d
cat Transducer to concatenate iterable values. Iterates over each input and emits individual values down stream, therefore removing one level of nesting from the input.
choices Returns an infinite iterator of random choices and their (optional) weights. If weights is given, it must have at least the same size as choices. If omitted, each choice will have same probability.
compR Reducer composition helper, internally used by various transducers during initialization. Takes existing reducer rfn (a 3-tuple) and a reducing function fn. Returns a new reducer tuple.
deepTransform Higher-order deep object transformer used by mapDeep(). Accepts a nested spec array reflecting same key structure as the object to be mapped, but with functions or sub-specs as their values. Returns a new function, which when called, recursively applies nested transformers in post-order traversal (child transformers are run first) and returns the result of the root transformer.
delayed Yields transducer which wraps incoming values in promises, which each resolve after specified delay time (in ms).
ensureReduced
groupBinary Creates a bottom-up, unbalanced binary tree of desired depth and choice of data structures. Any value can be indexed, as long as a numeric representation (key) can be obtained. This numeric key is produced by the supplied key function. IMPORTANT: the returned values MUST be unsigned and less than the provided bit length (i.e. 0 .. (2^bits) - 1 range).By default the tree is constructed using plain objects for branches, with left branches stored as "l" and right ones as "r". The original values are stored at the lowest tree level using a customizable nested reducer. By default leaves are collected in arrays (using the push() reducer), but any suitable reducer can be used (e.g. conj() to collect values into sets).Index by lowest 4-bits of ID value:
isReduced
keySelector
line Iterator yielding steps + 1 interpolated values on a line in the closed [start .. end] interval.
lookup1d Returns function accepting a single index arg used to lookup value in given array. No bounds checks are done.
lookup2d Returns function accepting a single [x, y] index tuple, used to lookup value in given array. Useful for transducers processing 2D data.
lookup3d Same as lookup2d, but for 3D data. The index ordering of the source data MUST be in Z, Y, X order (i.e. a stack of row major 2D slices). No bounds checks are done.
noop No-op / pass-through transducer, essentially the same as: map((x) => x), but faster. Useful for testing and / or to keep existing values in a multiplex() tuple lane.
padSides Returns iterator of src padded with value x, repeated numLeft/numRight times (default: 1). By default both sides are padded, but can be adjusted by setting either of them to zero. numRight defaults to same value as numLeft.
permutationsN Iterator yielding the Cartesian Product for n items of m values each.
pushCopy
reduced
reducer Convenience helper for building a full Reducer using the identity function (i.e. (x) => x) as completion step (true for 90% of all bundled transducers).
renamer
sideEffect Helper transducer. Applies given fn to each input value, presumably for side effects. Discards function's result and yields original inputs.
step Single-step transducer execution wrapper. Returns array if transducer produces multiple results and undefined if there was no output. Else returns single result value.
trace
unreduced

Type Aliases

Type Alias Description
ConvolutionKernel1D
ConvolutionKernel2D
ConvolutionKernel3D
LabelFn
ReductionFn
Transducer
TransformFn
TransformSubSpec
TxLike