effect-io-ai

Package: effect
Module: Array

Array.takeWhile

Takes elements from the start while the predicate holds, stopping at the first element that fails.

When to use

Use to keep the leading elements of an iterable while each element satisfies a predicate, returning the retained prefix as an array.

Details

Supports refinements for type narrowing. The predicate receives (element, index).

Example (Taking while condition holds)

import { Array } from "effect"

console.log(Array.takeWhile([1, 3, 2, 4, 1, 2], (x) => x < 4)) // [1, 3, 2]

See

Signature

declare const takeWhile: { <A, B extends A>(refinement: (a: NoInfer<A>, i: number) => a is B): (self: Iterable<A>) => Array<B>; <A>(predicate: (a: NoInfer<A>, i: number) => boolean): (self: Iterable<A>) => Array<A>; <A, B extends A>(self: Iterable<A>, refinement: (a: A, i: number) => a is B): Array<B>; <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): Array<A>; }

Source

Since v2.0.0