SuperLinq SuperLinq
SuperLinq SuperLinq
DocFX + Singulink = ♥

Search Results for

    Method CountDown

    | Edit this page

    CountDown<TSource>(IEnumerable<TSource>, int)

    Provides a countdown counter for a given count of elements at the tail of the sequence where zero always represents the last element, one represents the second-last element, two represents the third-last element and so on.

    Declaration
    public static IEnumerable<(TSource item, int? count)> CountDown<TSource>(this IEnumerable<TSource> source, int count)
    Parameters
    Type Name Description
    IEnumerable<TSource> source

    The source sequence.

    int count

    Count of tail elements of source to count down.

    Returns
    Type Description
    IEnumerable<(TSource item, int? count)>

    A sequence of tuples containing the element and it's count from the end of the sequence, or null.

    Type Parameters
    Name Description
    TSource

    The type of elements of source

    Remarks

    At most, count elements of the source sequence may be buffered at any one time unless source is a collection or a list.

    This method uses deferred execution semantics and streams its results.

    Examples

    The following code example demonstrates how to get a count down timer for a sequence, using the CountDown operator:

    var sequence = Enumerable.Range(1, 5);
                
    // Get a countdown counter for the the sequence
    var result = sequence.CountDown(2);
    
    Console.WriteLine(
        "[" +
        string.Join(", ", result) +
        "]");
    
    // This code produces the following output:
    // [(1, ), (2, ), (3, ), (4, 1), (5, 0)]
    
    Exceptions
    Type Condition
    ArgumentNullException

    source is null.

    ArgumentOutOfRangeException

    count is 0 or negative.

    | Edit this page

    CountDown<TSource, TResult>(IEnumerable<TSource>, int, Func<TSource, int?, TResult>)

    Provides a countdown counter for a given count of elements at the tail of the sequence where zero always represents the last element, one represents the second-last element, two represents the third-last element and so on.

    Declaration
    public static IEnumerable<TResult> CountDown<TSource, TResult>(this IEnumerable<TSource> source, int count, Func<TSource, int?, TResult> resultSelector)
    Parameters
    Type Name Description
    IEnumerable<TSource> source

    The source sequence.

    int count

    Count of tail elements of source to count down.

    Func<TSource, int?, TResult> resultSelector

    A function that receives the element and the current countdown value for the element and which returns those mapped to a result returned in the resulting sequence. For elements before the last count, the countdown value is null.

    Returns
    Type Description
    IEnumerable<TResult>

    A sequence of results returned by resultSelector.

    Type Parameters
    Name Description
    TSource

    The type of elements of source

    TResult

    The type of elements of the resulting sequence.

    Remarks

    At most, count elements of the source sequence may be buffered at any one time unless source is a collection or a list.

    This method uses deferred execution semantics and streams its results.

    Examples

    The following code example demonstrates how to get a count down timer for a sequence, using the CountDown operator:

    var sequence = Enumerable.Range(1, 5);
    
    // Get a countdown counter for the the sequence
    var result = sequence.CountDown(2, (item, cd) => new { Item = item, CountDown = cd?.ToString() ?? "null", });
    
    Console.WriteLine(
        "[" +
        string.Join(", ", result) +
        "]");
    
    // This code produces the following output:
    // [{ Item = 1, CountDown = null }, { Item = 2, CountDown = null }, { Item = 3, CountDown = null }, { Item = 4, CountDown = 1 }, { Item = 5, CountDown = 0 }]
    
    Exceptions
    Type Condition
    ArgumentNullException

    source or resultSelector is null.

    ArgumentOutOfRangeException

    count is 0 or negative.

    © SuperLinq Authors. All rights reserved.