Skip to content

Add an optional argument for "each" to use on empty input #1912

@hanche

Description

@hanche

What new feature should Elvish have?

Sometimes, I want each to do something different if it sees no inputs at all, as in each $fn [] or put | each $fn. I suggest an optional argument named else, or perhaps if-none, for this.

To make the proposal clear, here is an elvish implementation.

fn each {|&else=$nil f @inputs|
  if $else {
    var none = $true
    builtin:each {|item| set none = $false; $f $item } $@inputs
    if $none { $else }
  } else {
    builtin:each $f $@inputs
} }

Oh, I almost forgot (since I hardly ever use it): Do the same for for.

Output of "elvish -version"

0.21.0-dev.0.20240809001233-bc247bb5e17c-dirty

Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    ❓Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions