Skip to content

takeWhile() function

각 값이 주어진 f를 만족하는 한 값을 가져온 Iterable/AsyncIterable을 반환합니다.

Signature:

typescript
declare function takeWhile<A, B>(
  f: (a: A) => B,
  iterable: Iterable<A>,
): IterableIterator<A>;

Example

ts
const iter = takeWhile((a) => a < 3, [1, 2, 3, 4, 5, 6]);
iter.next(); // {done:false, value: 1}
iter.next(); // {done:false, value: 2}
iter.next(); // {done:true, value: undefined}

// with pipe
pipe(
  [1, 2, 3, 4, 5, 6],
  takeWhile((a) => a < 3),
  toArray,
); // [1, 2]

await pipe(
  Promise.resolve([1, 2, 3, 4, 5, 6]),
  takeWhile((a) => a < 3),
  toArray,
); // [1, 2]

// if you want to use asynchronous callback
await pipe(
  Promise.resolve([1, 2, 3, 4, 5, 6]),
  toAsync,
  takeWhile(async (a) => a < 3),
  toArray,
); // [1, 2]

// with toAsync
await pipe(
  [
    Promise.resolve(1),
    Promise.resolve(2),
    Promise.resolve(3),
    Promise.resolve(4),
    Promise.resolve(5),
    Promise.resolve(6),
  ],
  toAsync,
  takeWhile((a) => a < 3),
  toArray,
); // [1, 2]

Try It

see pipe, toAsync, toArray

Open Source Code

Released under the Apache-2.0 License.