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.


Associated WorkItem - 499150

Metadata

Metadata

Labels

📌 seQUESTeredIdentifies that an issue has been imported into Quest.dotnet-fsharp/svchelp wantedGood for community contributors to help [up-for-grabs]in-prThis issue will be closed (fixed) by an active pull request.okr-qualityContent-quality KR: Concerns article defects (bugs), freshness, or build warnings.

Type

No type

Projects

Status

👀 In review

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions