namespace Reducer
A Reducer is a stand-alone calculation that takes input values of type I, and, when requested, produces an output value of type O.
Companion type: Reducer<I,O>
Interfaces
| Name | Description |
|---|---|
Reducer.Impl<I,O,S> | The Implementation interface for a Reducer, which also exposes the internal state type. |
Reducer.Instance<I,O> | A reducer instance that manages its own state based on the reducer definition that was used to create this instance. |
Classes
| Name | Description |
|---|---|
Base | A base class that can be used to easily create Reducer instances. |
InstanceImpl | The default Reducer.Impl implementation. |
InvalidCombineShapeError | undocumented |
ReducerHaltedError | undocumented |
Functions
combine
Returns a Reducer that combines multiple input reducers according to the given "shape" by providing input values to all of them and collecting the outputs in the shape.
combineReducer that combines multiple input reducers according to the given "shape" by providing input values to all of them and collecting the outputs in the shape.Definition
function combine<T, const S extends Reducer.CombineShape<T>>(shape: S & Reducer.CombineShape<T>): Reducer<T, Reducer.CombineResult<S>>;
Type parameters
| Name | Description |
|---|---|
| T | the input value type for all the reducers |
| S | the desired result shape type |
Parameters
| Name | Type | Description |
|---|---|---|
shape | S & Reducer.CombineShape<T> | a shape defining where reducer outputs will be located in the result. It can consist of a single reducer, an array of shapes, or an object with string keys and shapes as values. |
const red = Reducer.combine([Reducer.sum, { av: [Reducer.average] }])
console.log(Stream.range({amount: 9 }).reduce(red))
// => [36, { av: [4] }]
constant
Returns a Reducer that always outputs the given value, and does not accept input values.
constantReducer that always outputs the given value, and does not accept input values.contains
Returns a Reducer that outputs false as long as the given elem has not been encountered the given amount of times in the input values, true otherwise.
containsReducer that outputs false as long as the given elem has not been encountered the given amount of times in the input values, true otherwise.Definition
function contains<T, T2 extends T = T>(elem: T2, options?: {
amount?: number;
eq?: Eq<T | T2>;
negate?: boolean;
}): Reducer<T, boolean>;
Type parameters
| Name | Description |
|---|---|
| T | the element type |
| T2 |
Parameters
| Name | Type | Description |
|---|---|---|
elem | T2 | the element to search for |
options | {amount?: number;eq?: Eq<T | T2>;negate?: boolean;} | (optional) an object containing the following properties: - amount: (detaulf: 1) the amount of elements to find - eq: (default: Eq.objectIs) the Eq instance to use to compare elements - negate: (default: false) when true will invert the given predicate |
console.log(Stream.range({ amount: 10 }).reduce(Reducer.contains(5)))
// => true
containsSlice
Returns a Reducer that returns true if the input values contain the given slice sequence amount times. Otherwise, returns false.
containsSliceReducer that returns true if the input values contain the given slice sequence amount times. Otherwise, returns false.Definition
function containsSlice<T>(slice: StreamSource<T>, options?: {
eq?: Eq<T> | undefined;
amount?: number;
}): Reducer<T, boolean>;
Type parameters
| Name | Description |
|---|---|
| T |
Parameters
| Name | Type | Description |
|---|---|---|
slice | StreamSource<T> | a sequence of elements to match against |
options | {eq?: Eq<T> | undefined;amount?: number;} | (optional) an object containing the following properties: - amount: (detaulf: 1) the amount of elements to find - eq: (default: Eq.objectIs) the Eq instance to use to compare elements |
create
Returns a Reducer with the given options:
createReducer with the given options:Definition
function create<I, O = I, S = O>(init: (initHalt: () => void) => S, next: (current: S, next: I, index: number, halt: () => void) => S, stateToResult: (state: S, index: number, halted: boolean) => O): Reducer<I, O>;
Type parameters
| Name | Description |
|---|---|
| I | the input value type |
| O | the output value type |
| S | the internal state type |
Parameters
| Name | Type | Description |
|---|---|---|
init | (initHalt: () => void) => S | the initial state value |
next | (current: S, next: I, index: number, halt: () => void) => S | returns the next state value based on the given inputs: - current: the current state - next: the current input value - index: the input index value - halt: function that, when called, ensures no more elements are passed to the reducer |
stateToResult | (state: S, index: number, halted: boolean) => O | a function that converts the current state to an output value |
const evenNumberOfOnes = Reducer
.create(
true,
(current, value: number) => value === 1 ? !current : current,
state => state ? 'even' : 'not even')
const result = Stream.of(1, 2, 3, 2, 1)).reduce(evenNumberOfOnes)
console.log+(result)
// => 'even'
createMono
Returns a Reducer of which the input, state, and output types are the same.
createMonoReducer of which the input, state, and output types are the same.Definition
function createMono<T>(init: (initHalt: () => void) => T, next: (current: T, next: T, index: number, halt: () => void) => T, stateToResult?: (state: T, index: number, halted: boolean) => T): Reducer<T>;
Type parameters
| Name | Description |
|---|---|
| T | the overall value type |
Parameters
| Name | Type | Description |
|---|---|---|
init | (initHalt: () => void) => T | the initial state value |
next | (current: T, next: T, index: number, halt: () => void) => T | returns the next state value based on the given inputs: - current: the current state - next: the current input value - index: the input index value - halt: function that, when called, ensures no more elements are passed to the reducer |
stateToResult | (state: T, index: number, halted: boolean) => T | (optional) a function that converts the current state to an output value |
const sum = Reducer
.createMono(
0,
(current, value) => current + value
)
const result = Stream.of(1, 2, 3, 2, 1)).reduce(sum)
console.log+(result)
// => 9
createOutput
Returns a Reducer of which the state and output types are the same.
createOutputReducer of which the state and output types are the same.Definition
function createOutput<I, O = I>(init: (initHalt: () => void) => O, next: (current: O, next: I, index: number, halt: () => void) => O, stateToResult?: (state: O, index: number, halted: boolean) => O): Reducer<I, O>;
Type parameters
| Name | Description |
|---|---|
| I | the input value type |
| O | the output value type |
Parameters
| Name | Type | Description |
|---|---|---|
init | (initHalt: () => void) => O | the initial state value |
next | (current: O, next: I, index: number, halt: () => void) => O | returns the next state value based on the given inputs: - current: the current state - next: the current input value - index: the input index value - halt: function that, when called, ensures no more elements are passed to the reducer |
stateToResult | (state: O, index: number, halted: boolean) => O | (optional) a function that converts the current state to an output value |
const boolToString = Reducer
.createOutput(
'',
(current, value: boolean) => current + (value ? 'T' : 'F')
)
const result = Stream.of(true, false, true)).reduce(boolToString)
console.log+(result)
// => 'TFT'
endsWithSlice
Returns a Reducer that returns true if the last input values match the given slice values repeated amount times. Otherwise, returns false.
endsWithSliceReducer that returns true if the last input values match the given slice values repeated amount times. Otherwise, returns false.Definition
function endsWithSlice<T>(slice: StreamSource<T>, options?: {
eq?: Eq<T> | undefined;
amount?: number;
}): Reducer<T, boolean>;
Type parameters
| Name | Description |
|---|---|
| T |
Parameters
| Name | Type | Description |
|---|---|---|
slice | StreamSource<T> | a sequence of elements to match against |
options | {eq?: Eq<T> | undefined;amount?: number;} | (optional) an object containing the following properties: - amount: (detaulf: 1) the amount of elements to find - eq: (default: Eq.objectIs) the Eq instance to use to compare elements |
equals
Returns a Reducer that ouputs true when the received elements match the given other stream source according to the eq instance, false otherwise.
equalsReducer that ouputs true when the received elements match the given other stream source according to the eq instance, false otherwise.Definition
function equals<T>(other: StreamSource<T>, options?: {
eq?: Eq<T>;
negate?: boolean;
}): Reducer<T, boolean>;
Type parameters
| Name | Description |
|---|---|
| T | the element type |
Parameters
| Name | Type | Description |
|---|---|---|
other | StreamSource<T> | a stream source containg elements to match against |
options | {eq?: Eq<T>;negate?: boolean;} | (optional) an object containing the following properties: - eq: (default: Eq.objectIs) the Eq instance to use to compare elements - negate: (default: false) when true will invert the given predicate |
every
Returns a Reducer that ouputs true as long as all input values satisfy the given pred, false otherwise.
everyReducer that ouputs true as long as all input values satisfy the given pred, false otherwise.Definition
function every<T>(pred: (value: T, index: number) => boolean, options?: {
negate?: boolean;
}): Reducer<T, boolean>;
Type parameters
| Name | Description |
|---|---|
| T | the element type |
Parameters
| Name | Type | Description |
|---|---|---|
pred | (value: T, index: number) => boolean | a function taking an input value and its index, and returning true if the value satisfies the predicate |
options | {negate?: boolean;} | (optional) an object containing the following properties: - negate: (default: false) when true will invert the given predicate |
console.log(Stream.range{ amount: 10 }).reduce(Reducer.every(v => v < 5))
// => false
fold
Returns a Reducer that uses the given init and next values to fold the input values into result values.
foldReducer that uses the given init and next values to fold the input values into result values.Definition
function fold<T, R>(init: OptLazy<R>, next: (current: R, value: T, index: number, halt: () => void) => R): Reducer<T, R>;
Type parameters
| Name | Description |
|---|---|
| T | the input type |
| R | the output type |
Parameters
| Name | Type | Description |
|---|---|---|
init | OptLazy<R> | an (optionally lazy) initial result value |
next | (current: R, value: T, index: number, halt: () => void) => R | a function taking the following arguments: - current - the current result value - value - the next input value - index: the input index value - halt: function that, when called, ensures no more elements are passed to the reducer |
join
Returns a Reducer that joins the given input values into a string using the given options.
joinReducer that joins the given input values into a string using the given options.Definition
function join<T>({ sep, start, end, valueToString, }?: {
sep?: string | undefined;
start?: string | undefined;
end?: string | undefined;
valueToString?: ((value: T) => string) | undefined;
}): Reducer<T, string>;
Type parameters
| Name | Description |
|---|---|
| T | the input element type |
Parameters
| Name | Type | Description |
|---|---|---|
{ sep, start, end, valueToString, } | {sep?: string | undefined;start?: string | undefined;end?: string | undefined;valueToString?: ((value: T) => string) | undefined;} |
console.log(Stream.of(1, 2, 3).reduce(Reducer.join({ sep: '-' })))
// => '1-2-3'
some
Returns a Reducer that ouputs false as long as no input value satisfies given pred, true otherwise.
someReducer that ouputs false as long as no input value satisfies given pred, true otherwise.Definition
function some<T>(pred: (value: T, index: number) => boolean, options?: {
negate?: boolean;
}): Reducer<T, boolean>;
Type parameters
| Name | Description |
|---|---|
| T | the element type |
Parameters
| Name | Type | Description |
|---|---|---|
pred | (value: T, index: number) => boolean | a function taking an input value and its index, and returning true if the value satisfies the predicate |
options | {negate?: boolean;} | (optional) an object containing the following properties: - negate: (default: false) when true will invert the given predicate |
console.log(Stream.range{ amount: 10 }).reduce(Reducer.some(v => v > 5))
// => true
startsWithSlice
Returns a Reducer that returns true if the first input values match the given slice values repeated amount times. Otherwise, returns false.
startsWithSliceReducer that returns true if the first input values match the given slice values repeated amount times. Otherwise, returns false.Definition
function startsWithSlice<T>(slice: StreamSource<T>, options?: {
eq?: Eq<T> | undefined;
amount?: number;
}): Reducer<T, boolean>;
Type parameters
| Name | Description |
|---|---|
| T |
Parameters
| Name | Type | Description |
|---|---|---|
slice | StreamSource<T> | a sequence of elements to match against |
options | {eq?: Eq<T> | undefined;amount?: number;} | (optional) an object containing the following properties: - amount: (detaulf: 1) the amount of elements to find - eq: (default: Eq.objectIs) the Eq instance to use to compare elements |
toArray
Returns a Reducer that collects received input values in an array, and returns a copy of that array as an output value when requested.
toArrayReducer that collects received input values in an array, and returns a copy of that array as an output value when requested.Definition
function toArray<T>(options?: {
reversed?: boolean;
}): Reducer<T, T[]>;
Type parameters
| Name | Description |
|---|---|
| T | the element type |
Parameters
| Name | Type | Description |
|---|---|---|
options | {reversed?: boolean;} | (optional) object specifying the following properties - reversed: (optional) when true will create a reversed array |
console.log(Stream.of(1, 2, 3).reduce(Reducer.toArray()))
// => [1, 2, 3]
console.log(Stream.of(1, 2, 3).reduce(Reducer.toArray({ reversed: true })))
// => [3, 2, 1]
toJSMap
Returns a Reducer that collects received input tuples into a mutable JS Map, and returns a copy of that map when output is requested.
toJSMapReducer that collects received input tuples into a mutable JS Map, and returns a copy of that map when output is requested.toJSMultiMap
Returns a Reducer that collects received input tuples into a mutable JS multimap, and returns a copy of that map when output is requested.
toJSMultiMapReducer that collects received input tuples into a mutable JS multimap, and returns a copy of that map when output is requested.toJSObject
Returns a Reducer that collects 2-tuples containing keys and values into a plain JS object, and returns a copy of that object when output is requested.
toJSObjectReducer that collects 2-tuples containing keys and values into a plain JS object, and returns a copy of that object when output is requested.toJSSet
Returns a Reducer that collects received input values into a mutable JS Set, and returns a copy of that map when output is requested.
toJSSetReducer that collects received input values into a mutable JS Set, and returns a copy of that map when output is requested.Constants
| Name | Description |
|---|---|
| and | A Reducer that takes boolean values and outputs true if all input values are true, and false otherwise. |
| average | A Reducer that calculates the average of all given numberic input values. |
| count | A Reducer that remembers the amount of input items provided. |
| first | Returns a Reducer that remembers the first input value. |
| groupBy | Returns a Reducer that uses the valueToKey function to calculate a key for each value, and feeds the tuple of the key and the value to the collector reducer. Finally, it returns the output of the collector. If no collector is given, the default collector will return a JS multimap of the type Map<K, V[]>. |
| isEmpty | A Reducer that outputs true if no input values are received, false otherwise. |
| last | Returns a Reducer that remembers the last input value. |
| max | Returns a Reducer that remembers the maximum value of the numberic inputs. |
| maxBy | Returns a Reducer that remembers the maximum value of the inputs using the given compFun to compare input values |
| min | Returns a Reducer that remembers the minimum value of the numberic inputs. |
| minBy | Returns a Reducer that remembers the minimum value of the inputs using the given compFun to compare input values |
| nonEmpty | A Reducer that outputs true if one or more input values are received, false otherwise. |
| or | A Reducer that takes boolean values and outputs true if one or more input values are true, and false otherwise. |
| partition | Returns a Reducer that splits the incoming values into two separate outputs based on the given pred predicate. Values for which the predicate is true are fed into the collectorTrue reducer, and other values are fed into the collectorFalse instance. If no collectors are provided the values are collected into arrays. |
| pipe | Returns a Reducer instance that first applies this reducer, and then applies the given next reducer to each output produced by the previous reducer. |
| product | A Reducer that calculates the product of all given numeric input values. |
| race | Returns a Reducer that feeds incoming values to all reducers in the provided reducers source, and halts when the first reducer in the array is halted and returns the output of that reducer. Returns the otherwise value if no reducer is yet halted. |
| single | Returns a Reducer that only produces an output value when having receives exactly one input value, otherwise will return the otherwise value or undefined. |
| sum | A Reducer that sums all given numeric input values. |