Skip to content

Behavior of reduce2 when ind argument is empty vs docstringΒ #1545

@sogaiu

Description

@sogaiu

The current docstring for reduce2 states:

The 2-argument version of reduce that does not take an initialization value. Instead, the first element of the array is used for initialization.

My reading of this is that it leaves unspecified what to do when ind is empty.

According to this commit, the current behavior of returning nil is intentional:

(reduce2 + [])
# =>
nil

It seems like there are at least two other reasonable ways to handle an empty ind argument:

  1. Raise an error
  2. Try to call the f argument with zero arguments

It seems that approach 2 is what Common Lisp went for [1].

If the current behavior is the most desirable, may be it would make sense to update the docstring?


[1] See this comment for more details.

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