Skip to content
This repository was archived by the owner on Oct 8, 2024. It is now read-only.
This repository was archived by the owner on Oct 8, 2024. It is now read-only.

flatMap should act like it does a yield * on each iterable #114

@jorendorff

Description

@jorendorff

Currently Iterator.prototype.flatMap is specified roughly like this:

*flatMap(mapper) {
  for (let value of {[@@Iterator]: () => this}) {
    for (let innerValue of mapper(value)) {
      yield innerValue;
    }
  }
}

Arguably it should be more like this:

*flatMap(mapper) {
  for (let value of {[@@Iterator]: () => this}) {
    yield* mapper(value);
  }
}

This changes two details of behavior:

  • It causes the flatMap iterator helper's .throw(exc) method to forward the exception along to the inner iterator (consistent with other iterator helpers). Currently the inner iterator is instead closed, and exc is then thrown again.
  • It causes the flatMap iterator helper's .next(value) method to forward the value along to the inner iterator. Currently the inner iterator's .next() method is always called with no arguments.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions