Skip to content

Bad use of Seq.item in sumSeq example code #35428

@ghost

Description

[Enter feedback here]
sumSeq uses Seq.item to get to the item of interest in sequence -- this means it has to calculate all the intermediate items afresh every time. This makes it very slow and a bad example for someone just learning seqs/IEnumerables. A better implementation of sumSeq would be something like:

    let sumSeq length sequence =
        sequence
        |> Seq.skip 1                      // sumSeq uses Seq.item (index+1) -- so it always skips first item
        |> Seq.truncate length      // don't take more than length items
        |> Seq.scan (+) 0.0            // generate running sums
        |> Seq.skip 1                     // skip the initial 0.0 from sequence of running sums

Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions