A sequence is a logical series of elements all of one type. The definition of a sequence has no stipulation of how its elements are stored, or linked, or whether they are stored at all. In C# sequences are called enumerable collections, they implement IEnumerable<T>
. In F# they are represented by the seq<T>
type.
The main purpose of Project Composite is to provide extensions or functions that allow building sequence-based data processing solutions in C#/F# with more convenience and practical sense.
It also introduces Composite<T>
, a brand of sequence with a native support for nesting. Each node of a Composite<T>
is either a Value
containing a payload object or a Composite
comprising a sequence of child nodes.
The functions are listed below grouped by their C#-friendly representations followed by their F#-friendly signatures.
Splits the source sequence into an array of sequences.
The function uses deferred execution. The resulting sequences will not be generated until they are enumerated as shown in the diagram below.
F# (available in standard FSharp.Core since version 4.4.0.0): Seq.chunkBySize: (chunkSize: int) -> (source: seq<'T>) -> seq<'T[]>
Divides the input sequence into a sequence of pages each containing at most the given number of elements.
The function uses deferred execution. The resulting sequence will not be generated until it is enumerated as shown in the diagram below.
IEnumerable<T[]> EnumerableExtensions.AsBatched<T>(IEnumerable<T> source, int batchSize, Func<T, int> getElementSize)
Divides the input sequence into a sequence of batches of either one element or as many elements as possible if their total size does not exceed the given number. The size of each element is obtained through the use of the given function.
The function uses deferred execution. The resulting sequence will not be generated until it is enumerated as shown in the diagram below.
IEnumerable<TResult> EnumerableExtensions.AccumulateSelectMany<TSource>(IEnumerable<TSource> source, AccumulateTransformRule<TSource, TResult>[] rules)
F#: Seq.accumulateCollect (rules: (('TIn -> bool)[] * ('TIn[] -> seq<'TOut>))[]) -> (source: seq<'TIn>) -> seq<'TOut>
Finds elements in the input sequence to accumulate them in arrays, applies the respective transform functions to populated arrays, and concatenates the results. The accumulation and transformation of objects found in the source sequence is performed according to the given rules.
AccumulateTransformRule
has the following constructor signature:
public AccumulateTransformRule(Func<TSource, bool>[] findPredicates, Func<TSource[], IEnumerable<TResult>> transformFunction)
The findPredicates
are used to find objects to be put in the accumulator cells with the respective indices.
The transformFunction
is used to transform the array of accumulated objects into an enumerable result.
The function uses deferred execution. The resulting sequence will not be generated until it is enumerated as shown in the diagram below if we assume the rules
are defined as in the following listing.
var rules = new[] { new AccumulateTransformRule<int, int>(
new Func<int, bool>[] {
x => x > 2,
x => x > 3 },
x => new[] { x[0] + x[1] + 1, 2 * x[0] + x[1] / 2 } ),
new AccumulateTransformRule<int, int>(
new Func<int, bool>[] {
x => x > 1,
x => x > 2 },
x => new[] { 2 * x[0] + x[1], 3 * x[0] + x[1] } )
};
Composite<T> CompositeExtensions.Fork<T>(Composite<T> source, Func<T, bool> predicate, Func<T, T[]> mapping)
F#: Comp.fork (predicate: 'T -> bool) -> (mapping: 'T -> seq<'T>) -> (source: 'T Composite) -> 'T Composite
Builds a new sequence composite based on the source in which the values containing objects for which the given predicate returns true
are substituted for sequence composites wrapping the sequences produced from them through the use of the mapping function.
The function uses deferred execution. The resulting sequence composite will not be generated until it is enumerated as shown in the diagram below.
Composite<TOut> CompositeExtensions.Select<TIn,TOut>(Composite<TIn> source, Func<TIn, TOut> mapping)
Builds a new sequence composite based on the source in which the values contain objects substituted through the use of the given function.
The function uses deferred execution. The resulting sequence composite will not be generated until it is enumerated as shown in the diagram below.
Returns the sequence of components of the input sequence composite.
The function uses deferred execution. The resulting sequence will not be generated until it is enumerated as shown in the diagram below.
Views the given sequence composite as a sequence.
The function uses deferred execution. The resulting sequence will not be generated until it is enumerated as shown in the diagram below.