Table of Contents

Method FoldByIdentity

Namespace
SodaFlow.Collections
Assembly
SodaFlow.Collections.dll

FoldByIdentity<TKey, TIdentity, TState, TAccumulate>(ReactiveCollection<TKey, TIdentity, TState>, Func<TIdentity, TAccumulate>, TAccumulate, Func<TAccumulate, TAccumulate, TAccumulate>, Func<TAccumulate, TAccumulate, TAccumulate>)

Folds the identity of each item into one cell, and keeps that cell current as the collection adds and removes items.

public static Cell<TAccumulate> FoldByIdentity<TKey, TIdentity, TState, TAccumulate>(this ReactiveCollection<TKey, TIdentity, TState> collection, Func<TIdentity, TAccumulate> select, TAccumulate zero, Func<TAccumulate, TAccumulate, TAccumulate> add, Func<TAccumulate, TAccumulate, TAccumulate> subtract) where TKey : notnull where TIdentity : notnull

Parameters

collection ReactiveCollection<TKey, TIdentity, TState>

The collection or view to fold.

select Func<TIdentity, TAccumulate>

The part of an identity that this fold adds.

zero TAccumulate

The value for no items, which must be the identity of add.

add Func<TAccumulate, TAccumulate, TAccumulate>

Adds the value of one item to the accumulated value.

subtract Func<TAccumulate, TAccumulate, TAccumulate>

Removes the value of one item from the accumulated value. It must remove each value that add includes.

Returns

Cell<TAccumulate>

A cell with the folded value of the items that the collection holds.

Type Parameters

TKey

The type of the keys.

TIdentity

The type of the immutable part of an item.

TState

The type of the mutable part of an item.

TAccumulate

The type of the value that the fold gives.

Remarks

This gives the same value as Fold<TKey, TIdentity, TState, TAccumulate>(ReactiveCollection<TKey, TIdentity, TState>, Func<TIdentity, TState, TAccumulate>, TAccumulate, Func<TAccumulate, TAccumulate, TAccumulate>, Func<TAccumulate, TAccumulate, TAccumulate>) with a select that reads only the identity, and it costs less to keep. An edit of a state cannot change an identity, thus this fold reads only the changes that add or remove an item. The cell sends no value at an edit of a state, and the cost of that edit here is one test. The select receives the identity and not the state, thus it cannot read the state that it says it does not read.

A count of the items, and a count for each group of an identity, are the usual examples. The rules of add, subtract, and zero are the rules of Fold.

A slice can name a key that stays in its window as a removal and an add. An edit of a state that moves the keys of the window does this. The cell then sends a value that is equal to its previous value.