Skip to content

reduce supply accumulator as pipeline input to the closure #13459

Closed

Description

@Bahex

Related problem

The parameter order of reduce can be confusing, as it is a left associative operation yet the parameter order is |item, accumulator| unlike some other languages (e.g. Rust) where it's the other way around.

Describe the solution you'd like

Nushell already has an extra way of passing data into a function, the pipeline input $in. This can be utilized to differentiate between the two parameters.

Some commands that are basically binary operators accept their left operand in this manner

$left | merge $right

Supplying data to a closure as both an argument and as the pipeline input is a common pattern in other commands like each and filter so this has some precedent.

It makes applying commands like merge a little cleaner:

[{a:1} {b:2} {c:3} {b:0}] | reduce {|it| merge $it}

compared to the current

[{a:1} {b:2} {c:3} {b:0}] | reduce {|it,acc| $acc | merge $it}

This does not suggest the accumulator should not be available as the second argument. Nushell closures ignore extra arguments which makes both of these possible

  • ... | each {...}
  • ... | each {|it| ...}

Keeping the current order of parameters would make this a (mostly) backwards compatible change as it is unlikely that existing usages depend on the closures pipeline input being empty.

Describe alternatives you've considered

Changing the order of parameters would cause additional confusion and be a breaking change for no technical benefit as it does not allow any new patterns.

Additional context and details

No response

Activity

  1. added
    category:enhancement
    status:needs-triage
    on Jul 26, 2024
  2. added a commit that references this issue on Jul 30, 2024
    0576794
  3. added this to the v0.97.0 milestone on Jul 30, 2024
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

    category:enhancementstatus:needs-triage

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions