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

Function takeWhileEffect

packages/effect/src/Sink.ts:1662–1695  ·  view source on GitHub ↗
(
  predicate: (input: In) => Effect.Effect<boolean, E, R>
)

Source from the content-addressed store, hash-verified

1660export const takeWhileEffect: {
1661 <In, E, R>(predicate: (input: In) => Effect.Effect<boolean, E, R>): Sink<Array<In>, In, In, E, R>
1662} = <In, E, R>(
1663 predicate: (input: In) => Effect.Effect<boolean, E, R>
1664): Sink<Array<In>, In, In, E, R> =>
1665 fromTransform((upstream) => {
1666 const out = Arr.empty<In>()
1667 let leftover: Arr.NonEmptyReadonlyArray<In> | undefined = undefined
1668 return upstream.pipe(
1669 Effect.flatMap((arr) => {
1670 let i = 0
1671 return Effect.whileLoop({
1672 while: () => i < arr.length,
1673 body: constant(Effect.flatMap(
1674 Effect.suspend(() => {
1675 const input = arr[i++]
1676 return Effect.map(predicate(input), (passes) => [input, passes] as const)
1677 }),
1678 ([input, passes]) => {
1679 if (!passes) {
1680 if (i < arr.length) {
1681 leftover = arr.slice(i) as any
1682 }
1683 return Cause.done()
1684 }
1685 out.push(input)
1686 return Effect.void
1687 }
1688 )),
1689 step: constVoid
1690 })
1691 }),
1692 Effect.forever({ disableYield: true }),
1693 Pull.catchDone(() => Effect.succeed([out, leftover] as const))
1694 )
1695 })
1696
1697/**
1698 * Applies a `FilterEffect` to input elements effectfully while it succeeds,

Callers 1

takeUntilEffectFunction · 0.85

Calls 8

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

Tested by

no test coverage detected