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

Function takeWhileFilterEffect

packages/effect/src/Sink.ts:1710–1737  ·  view source on GitHub ↗
(
  filter: Filter.FilterEffect<In, Out, X, E, R>
)

Source from the content-addressed store, hash-verified

1708 * @since 4.0.0
1709 */
1710export const takeWhileFilterEffect = <In, Out, X, E, R>(
1711 filter: Filter.FilterEffect<In, Out, X, E, R>
1712): Sink<Array<Out>, In, In, E, R> =>
1713 fromTransform((upstream) => {
1714 const out = Arr.empty<Out>()
1715 let leftover: Arr.NonEmptyReadonlyArray<In> | undefined = undefined
1716 return upstream.pipe(
1717 Effect.flatMap((arr) => {
1718 let i = 0
1719 return Effect.whileLoop({
1720 while: () => i < arr.length,
1721 body: constant(Effect.flatMap(Effect.suspend(() => filter(arr[i++])), (result) => {
1722 if (Result.isFailure(result)) {
1723 if (i < arr.length) {
1724 leftover = arr.slice(i) as any
1725 }
1726 return Cause.done()
1727 }
1728 out.push(result.success)
1729 return Effect.void
1730 })),
1731 step: constVoid
1732 })
1733 }),
1734 Effect.forever({ disableYield: true }),
1735 Pull.catchDone(() => Effect.succeed([out, leftover] as const))
1736 )
1737 })
1738
1739/**
1740 * Collects input elements until the predicate returns `true`, including the

Callers

nothing calls this directly

Calls 7

constantFunction · 0.90
pushMethod · 0.80
fromTransformFunction · 0.70
filterFunction · 0.70
pipeMethod · 0.65
doneMethod · 0.45
succeedMethod · 0.45

Tested by

no test coverage detected