Skip to content

Access previous items with Linq #817

Closed

Description

@mdmoura

I have the following for loop:

for (Int32 i = 0; i < inputs.Count(); i++)
  outputs.Add(inputs[index] * outputs[index - 1]);  

Is there any extension in MoreLinq that would allow me to do this with Linq?

var outputs = inputs.Select(input => input * Previous Output);

Activity

  1. viceroypenguin commented on May 13, 2021

    @viceroypenguin
    Contributor

    The .Scan extension will do what you're looking for. It returns the first value, and then the result of the operation when provided the previous result and the current value. See this example for your case.

    var outputs = inputs.Scan((a, b) => a * b);
  2. locked and limited conversation to collaborators on May 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions