MCPcopy Create free account
hub / github.com/Effect-TS/effect / takeWhile

Function takeWhile

packages/effect/src/Sink.ts:1590–1609  ·  view source on GitHub ↗
(predicate: Predicate<In>)

Source from the content-addressed store, hash-verified

1588 <In, Out extends In>(refinement: Refinement<In, Out>): Sink<Array<Out>, In, In>
1589 <In>(predicate: Predicate<In>): Sink<Array<In>, In, In>
1590} = <In>(predicate: Predicate<In>): Sink<Array<In>, In, In> =>
1591 fromTransform((upstream) => {
1592 const out = Arr.empty<In>()
1593 return upstream.pipe(
1594 Effect.flatMap((arr) => {
1595 for (let i = 0; i < arr.length; i++) {
1596 if (!predicate(arr[i])) {
1597 const leftover: Arr.NonEmptyReadonlyArray<In> | undefined = (i + 1) < arr.length
1598 ? arr.slice(i + 1) as any
1599 : undefined
1600 return Cause.done([out, leftover] as const)
1601 }
1602 out.push(arr[i])
1603 }
1604 return Effect.void
1605 }),
1606 Effect.forever({ disableYield: true }),
1607 Pull.catchDone((end) => Effect.succeed<End<Array<In>, In>>(end ?? [out]))
1608 )
1609 })
1610
1611/**
1612 * Applies a `Filter` to input elements while it succeeds, collecting each

Callers 2

takeUntilFunction · 0.85
Stream.tsFile · 0.85

Calls 6

predicateFunction · 0.85
pushMethod · 0.80
fromTransformFunction · 0.70
pipeMethod · 0.65
doneMethod · 0.45
succeedMethod · 0.45

Tested by

no test coverage detected