SuperLinq SuperLinq
SuperLinq SuperLinq
DocFX + Singulink = ♥

Search Results for

    Method FillForward

    | Edit this page

    FillForward<T>(IEnumerable<T?>)

    Returns a sequence with each null reference or value in the source replaced with the previous non-null reference or value seen in that sequence.

    Declaration
    public static IEnumerable<T?> FillForward<T>(this IEnumerable<T?> source)
    Parameters
    Type Name Description
    IEnumerable<T> source

    The source sequence.

    Returns
    Type Description
    IEnumerable<T>

    An IEnumerable<T> with null references or values replaced.

    Type Parameters
    Name Description
    T

    Type of the elements in the source sequence.

    Remarks

    This method uses deferred execution semantics and streams its results. If references or values are null at the start of the sequence then they remain null.

    Examples

    The following code example demonstrates how to fill in missing elements with previous elements using FillForward.

    var sequence = new int?[] { null, null, 1, 2, null, null, null, 3, 4, null, null, };
                
    // Fill in missing elements from previous elements
    var result = sequence.FillForward();
    
    Console.WriteLine(
        "[" +
        string.Join(", ", result) +
        "]");
    
    // This code produces the following output:
    // [, , 1, 2, 2, 2, 2, 3, 4, 4, 4]
    
    Exceptions
    Type Condition
    ArgumentNullException

    source is null.

    | Edit this page

    FillForward<T>(IEnumerable<T>, Func<T, bool>)

    Returns a sequence with each missing element in the source replaced with the previous non-missing element seen in that sequence. An additional parameter specifies a function used to determine if an element is considered missing or not.

    Declaration
    public static IEnumerable<T> FillForward<T>(this IEnumerable<T> source, Func<T, bool> predicate)
    Parameters
    Type Name Description
    IEnumerable<T> source

    The source sequence.

    Func<T, bool> predicate

    The function used to determine if an element in the sequence is considered missing.

    Returns
    Type Description
    IEnumerable<T>

    An IEnumerable<T> with missing values replaced.

    Type Parameters
    Name Description
    T

    Type of the elements in the source sequence.

    Remarks

    This method uses deferred execution semantics and streams its results. If elements are missing at the start of the sequence then they remain missing.

    Examples

    The following code example demonstrates how to fill in missing elements with previous elements using FillForward.

    var sequence = Enumerable.Range(1, 10);
    
    // Fill in missing elements from previous elements
    var result = sequence
        .FillForward(
            i => i % 3 < 2);
    
    Console.WriteLine(
        "[" +
        string.Join(", ", result) +
        "]");
    
    // This code produces the following output:
    // [1, 2, 2, 2, 5, 5, 5, 8, 8, 8]
    
    Exceptions
    Type Condition
    ArgumentNullException

    source or predicate is null.

    | Edit this page

    FillForward<T>(IEnumerable<T>, Func<T, bool>, Func<T, T, T>)

    Returns a sequence with each missing element in the source replaced with one based on the previous non-missing element seen in that sequence. Additional parameters specify two functions, one used to determine if an element is considered missing or not and another to provide the replacement for the missing element.

    Declaration
    public static IEnumerable<T> FillForward<T>(this IEnumerable<T> source, Func<T, bool> predicate, Func<T, T, T> fillSelector)
    Parameters
    Type Name Description
    IEnumerable<T> source

    The source sequence.

    Func<T, bool> predicate

    The function used to determine if an element in the sequence is considered missing.

    Func<T, T, T> fillSelector

    The function used to produce the element that will replace the missing one. Its first argument receives the current element considered missing while the second argument receives the previous non-missing element.

    Returns
    Type Description
    IEnumerable<T>

    An IEnumerable<T> with missing values replaced.

    Type Parameters
    Name Description
    T

    Type of the elements in the source sequence.

    Remarks

    This method uses deferred execution semantics and streams its results. If elements are missing at the start of the sequence then they remain missing.

    Examples

    The following code example demonstrates how to fill in missing elements with previous elements using FillForward.

    var sequence = Enumerable.Range(1, 10);
    
    // Fill in missing elements from previous elements
    var result = sequence
        .FillForward(
            i => i % 3 < 2,
            (cur, nxt) => cur * nxt * 100);
    
    Console.WriteLine(
        "[" +
        string.Join(", ", result) +
        "]");
    
    // This code produces the following output:
    // [1, 2, 600, 800, 5, 3000, 3500, 8, 7200, 8000]
    
    Exceptions
    Type Condition
    ArgumentNullException

    source, predicate, or fillSelector is null.

    © SuperLinq Authors. All rights reserved.