Skip to content

takeWhile #27

Description

@jmakeig

Early exit after a condition fails to be met.

function* takeWhile(iterable /* Sequence, Array, etc. */, predicate = () => true) {
  // TODO: Test that iterable is, well, iterable and not something else
  for (const item of iterable) {
    if (predicate(item)) {
      yield item;
    } else {
      break;
    }
  }
}

console.log(Array.from(takeWhile([1, 2, 3, 4, 5, 6, 7], x => x < 3)));

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions