EagerCollection<K, V>
A reactive collection eagerly kept up-to-date.
An EagerCollection
is a reactive collection of entries that associate keys to values where the entries are computed eagerly and kept up to date whenever inputs change.
Extends
Type Parameters
Type Parameter | Description |
---|---|
K extends Json | Type of keys. |
V extends Json | Type of values. |
Methods
getArray()
getArray(key): V & DepSafe[]
Get all values associated to a key.
Parameters
Parameter | Type | Description |
---|---|---|
key | K | The key to query. |
Returns
V
& DepSafe
[]
The values associated to key
.
getUnique()
getUnique(key): V & DepSafe
Get the single value associated to a key.
For collections that do not use the generality of associating multiple values to a key, getUnique
saves some boilerplate over getArray
.
Parameters
Parameter | Type | Description |
---|---|---|
key | K | The key to query. |
Returns
V
& DepSafe
The value associated to key
.
Throws
SkipNonUniqueValueError if key
is associated to either zero or multiple values.
map()
map<K2, V2, Params>(mapper, ...params): EagerCollection<K2, V2>
Create a new eager collection by mapping a function over the values in this one.
For collection.map(MapperClass, params)
, the MapperClass
parameter should be the constructor function of a top-level class that implements the Mapper
interface.
This MapperClass
is instantiated to obtain a mapper object MapperClass(params)
.
For each entry [key, values]
in collection
, the result of MapperClass(params).mapEntry(key, values)
is some key
-value
pairs.
These key
-value
pairs, from calling mapEntry
on each of the entries in collection
, are combined to form the contents of the new collection.
If there are multiple pairs with the same key, the values are collected so that the result collection associates the key to all such values.
This not only produces the output collection, but also records a dependency edge in the computation graph, so that future updates to the input collection will eagerly trigger recomputation of the affected part of the output collection.
Type Parameters
Type Parameter | Description |
---|---|
K2 extends Json | Type of keys of the new collection. |
V2 extends Json | Type of values of the new collection. |
Params extends DepSafe [] | Types of additional parameters passed to mapper . |
Parameters
Parameter | Type | Description |
---|---|---|
mapper | (...params ) => Mapper <K , V , K2 , V2 > | Constructor of Mapper class to transform each entry of this collection. |
...params | Params | Additional parameters to mapper . |
Returns
EagerCollection
<K2
, V2
>
The resulting eager collection.
Remarks
The reason map
accepts the Mapper
class constructor and params
separately and instantiates the class internally is to avoid the resulting mapEntry
functions from being able to access other objects that the Skip Runtime is not aware of, and hence cannot track updates to.
This is also the reason the params
need to have type DepSafe[]
, as this requires them to be either constant or tracked by the Skip Runtime.
It is a bad idea to attempt to circumvent the spirit of the constraints this interface imposes.
mapReduce()
mapReduce<K2, V2, MapperParams>(mapper, ...mapperParams): <Accum, ReducerParams>(reducer, ...reducerParams) => EagerCollection<K2, Accum>
Composition of map
and reduce
.
The result of collection.mapReduce(MapperClass, mapperParams)(ReducerClass, reducerParams)
is equivalent to collection.map(MapperClass, mapperParams).reduce(ReducerClass, reducerParams)
but more efficient due to avoiding the intermediate collection.
Note that this function is curried so that mapper and reducer rest parameters can be provided separately, e.g. as mapReduce(MyMapper, foo, bar)(MyReducer, baz)
Type Parameters
Type Parameter | Description |
---|---|
K2 extends Json | Type of keys of the new collection. |
V2 extends Json | Type of values of the new collection. |
MapperParams extends DepSafe [] | Types of additional parameters passed to mapper . |
Parameters
Parameter | Type | Description |
---|---|---|
mapper | (...params ) => Mapper <K , V , K2 , V2 > | Constructor of Mapper class to transform each entry of this collection. |
...mapperParams | MapperParams | Additional parameters to mapper . |
Returns
Function
Type Parameters
Type Parameter | Description |
---|---|
Accum extends Json | Type of accumulated values. |
ReducerParams extends DepSafe [] | Types of additional parameters passed to reducer |
Parameters
Parameter | Type | Description |
---|---|---|
reducer | (...params ) => Reducer <V2 , Accum > | Constructor of Reducer class to accumulate values of this collection. |
...reducerParams | ReducerParams | Additional parameters to reducer |
Returns
EagerCollection
<K2
, Accum
>
The resulting eager collection.
merge()
merge(...others): EagerCollection<K, V>
Combine some eager collections into one, associating with each key all values associated with that key in any of the input collections.
Parameters
Parameter | Type | Description |
---|---|---|
...others | EagerCollection <K , V >[] | Eager collections to merge with this one. |
Returns
EagerCollection
<K
, V
>
The resulting combination of all input key-value pairs.
reduce()
reduce<Accum, Params>(reducer, ...params): EagerCollection<K, Accum>
Create a new eager collection from this one that accumulates each key's values.
For collection.reduce(ReducerClass, reducerParams)
, the ReducerClass
parameter should be the constructor function of a top-level class that implements the Reducer
interface.
This ReducerClass
is instantiated to obtain a reducer object ReducerClass(reducerParams)
.
For each entry [key, values]
in collection
, the result collection associates each key
to the accumulated result of calling ReducerClass(reducerParams).add
on each value in values
, starting from ReducerClass(reducerParams).initial
.
That is, if values
is [v1,...,vN]
, then key
will be associated with add(...add(add(initial, v1), v2),...,vN)
in the output collection.
This not only produces the output collection, but also records a dependency edge in the computation graph, so that future updates to the input collection will eagerly trigger recomputation of the affected part of the output collection.
During such recomputations, if an update is made to the input collection that results in a value that used to be associated to a key no longer being associated, then the reducer object's remove
function is used to update the accumulated value.
The remove
function may return null
, in which case the accumulated value is recomputed using add
and initial
.
Type Parameters
Type Parameter | Description |
---|---|
Accum extends Json | Type of accumulated values. |
Params extends DepSafe [] | Types of additional parameters passed to reducer |
Parameters
Parameter | Type | Description |
---|---|---|
reducer | (...params ) => Reducer <V , Accum > | Constructor of Reducer class to accumulate values of this collection. |
...params | Params | Additional parameters to reducer |
Returns
EagerCollection
<K
, Accum
>
The resulting eager collection.
Remarks
The reason reduce
accepts the Reducer
class constructor and params
separately and instantiates the class internally is to avoid the resulting add
and remove
functions from being able to access other objects that the Skip Runtime is not aware of, and hence cannot track updates to.
This is also the reason the params
need to have type DepSafe[]
, as this requires them to be either constant values or tracked by the Skip Runtime.
It is a bad idea to attempt to circumvent the spirit of the constraints this interface imposes.
size()
size(): number
The current number of entries in the collection.
Returns
number
The number of entries.
slice()
slice(start, end): EagerCollection<K, V>
Create a new eager collection by keeping only the elements whose keys are in the given range.
Parameters
Parameter | Type | Description |
---|---|---|
start | K & DepSafe | The least key whose entry will be kept in the result. |
end | K & DepSafe | The greatest key whose entry will be kept in the result. |
Returns
EagerCollection
<K
, V
>
The restricted eager collection.
slices()
slices(...ranges): EagerCollection<K, V>
Create a new eager collection by keeping only the elements whose keys are in at least one of the given ranges.
Parameters
Parameter | Type | Description |
---|---|---|
...ranges | [K & DepSafe , K & DepSafe ][] | The pairs of lower and upper bounds on keys to keep in the result. |
Returns
EagerCollection
<K
, V
>
The restricted eager collection.
take()
take(limit): EagerCollection<K, V>
Create a new eager collection by keeping the first entries.
Parameters
Parameter | Type | Description |
---|---|---|
limit | number | The number of entries to keep. |
Returns
EagerCollection
<K
, V
>
The restricted eager collection.