Links
Links are the things you use when calling Chains.run() or Chains.prepare() . These are the little functions which work with data flowing through the chain, and pass it on to the next link.
Typically several links are used to make a chain. If you’ve only got one link, perhaps there is a simpler way to do it.
Here’s an overview of the in-built links
Math functions
- min, max, average, sum, tally
Filtering
- filter, drop
- rank, rankArray
Changing the shape of values
- chunk
- reduce
Timing of values
- debounce, delay
Stopping a chain
- duration
- take
Math
min / max
For every input value, min and max will always emit the smallest or largest value seen so far.
Works with chains that output numbers or array of numbers. Non-numbers are skipped.
average()
Taking a numeric input (or array of numbers), average continuously returns a running average.
Non-numbers in the input stream are skipped.
sum()
Taking a numeric input (or array of numbers), sum returns the current total.
Non-numbers in the input stream are skipped.
tally
tally throws away the input value and instead emits the total number of values produced.
Example usage:
Filtering
filter
filter only passes values for which the predicate function returns true.
drop
drop is the opposite of filter
. Instead dropping values which the predicate function returns true
rank / rankArray
rank allows values to be scored using some function, emitting the ‘best’ value. rankArray is the same, but it works within a set of values.
The ranking function gets two parameters, a
and b
, and is expected to return a string value denoting which is the best, or ‘eq’ if they are equal.
Eg, ranking objects based off a ‘size’ field:
rank
has some options about when to emit a value downstream. By default, it won’t emit a new value if it’s equally ranked. And by default it won’t emit a value until it ‘beats’ the last value.
You can change these, for example:
rankArray works instead with arrays as input values. By default, it behaves similarly to rank
, but checks the contents of the array. If the withinArrays
option is set to true, it will instead emit the highest value within each array, and not care about previous values.
Changing shape
transform
transform Takes an input value and returns an output value.
Example usage
reduce
reduce assumes its input is an array, returning a single combined result using a function.
Example usage
chunk
Given a stream of values, chunk breaks it up into arrays of a given length.
For example, given a chunk size of 3:
If the stream of input values is: 1, 2, 3, 4, 5, 6
we would get chunks of [ 1, 2, 3], [ 4, 5, 6 ]
By default, if the stream ends and a chunk is not complete, it is returned regardless. Set the second parameter to chunk
to false (ie Chains.Link.chunk(3, false)
) to instead throw away the under-sized chunk.
Timing
delay
delay changes the timing of the stream, allowing you to add delay before or after a value is yielded.
Example usage
debounce
debounce ensures a minimum time between values. Values produced too quickly are dropped.
Example usage:
Stopping a chain
take
take will return the input value, but will break the chain after a certain number of values flow through it.
Example usage:
duration
duration will close a chain after a given interval has elapsed.