Skip to content

dropWhile() function

先頭から削除された要素を除外した Iterable/AsyncIterable を返します。f に適用された値が falsey を返すまで要素は削除されます。

Signature:

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

Example

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

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

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

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

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

see pipe, toAsync, toArray

Open Source Code

Released under the Apache-2.0 License.