SuperLinq SuperLinq
SuperLinq SuperLinq
DocFX + Singulink = ♥

Search Results for

    Method PreScan

    | Edit this page

    PreScan<TSource>(IEnumerable<TSource>, Func<TSource, TSource, TSource>, TSource)

    Performs a pre-scan (exclusive prefix sum) on a sequence of elements.

    Declaration
    public static IEnumerable<TSource> PreScan<TSource>(this IEnumerable<TSource> source, Func<TSource, TSource, TSource> transformation, TSource identity)
    Parameters
    Type Name Description
    IEnumerable<TSource> source

    Source sequence

    Func<TSource, TSource, TSource> transformation

    An accumulator function to be invoked on each element.

    TSource identity

    The initial accumulator value.

    Returns
    Type Description
    IEnumerable<TSource>

    The scanned sequence

    Type Parameters
    Name Description
    TSource

    Type of elements in source sequence

    Remarks

    An exclusive prefix scan returns an equal-length sequence where the N-th element is the aggregation of the first N-1 input elements, where the first element is simply the identity value.

    The inclusive version of PreScan is Scan<TSource>(IEnumerable<TSource>, Func<TSource, TSource, TSource>).

    This operator uses deferred execution and streams its result.

    Examples

    The following code example demonstrates how to perform an exclusive pre-fix scan on a sequence using PreScan.

    var sequence = Enumerable.Range(1, 4);
    
    // execute a scan of the sequence, returning the aggregation before processing the element
    var result = sequence.PreScan((a, b) => a + b, 0);
    
    Console.WriteLine(
        "[" +
        string.Join(", ", result) +
        "]");
    
    // This code produces the following output:
    // [0, 1, 3, 6]
    
    Exceptions
    Type Condition
    ArgumentNullException

    source or transformation is null.

    © SuperLinq Authors. All rights reserved.