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

Function zipWithNext

packages/effect/src/internal/stream.ts:8649–8686  ·  view source on GitHub ↗
(
  self: Stream.Stream<A, E, R>
)

Source from the content-addressed store, hash-verified

8647
8648/** @internal */
8649export const zipWithNext = <A, E, R>(
8650 self: Stream.Stream<A, E, R>
8651): Stream.Stream<[A, Option.Option<A>], E, R> => {
8652 const process = (
8653 last: Option.Option<A>
8654 ): Channel.Channel<Chunk.Chunk<readonly [A, Option.Option<A>]>, Chunk.Chunk<A>, never, never, void, unknown> =>
8655 core.readWithCause({
8656 onInput: (input: Chunk.Chunk<A>) => {
8657 const [newLast, chunk] = Chunk.mapAccum(
8658 input,
8659 last,
8660 (prev, curr) => [Option.some(curr), pipe(prev, Option.map((a) => [a, curr] as const))] as const
8661 )
8662 const output = Chunk.filterMap(
8663 chunk,
8664 (option) =>
8665 Option.isSome(option) ?
8666 Option.some([option.value[0], Option.some(option.value[1])] as const) :
8667 Option.none()
8668 )
8669 return core.flatMap(
8670 core.write(output),
8671 () => process(newLast)
8672 )
8673 },
8674 onFailure: core.failCause,
8675 onDone: () =>
8676 Option.match(last, {
8677 onNone: () => core.void,
8678 onSome: (value) =>
8679 channel.zipRight(
8680 core.write(Chunk.of<readonly [A, Option.Option<A>]>([value, Option.none()])),
8681 core.void
8682 )
8683 })
8684 })
8685 return new StreamImpl(pipe(toChannel(self), channel.pipeToOrFail(process(Option.none()))))
8686}
8687
8688/** @internal */
8689export const zipWithPrevious = <A, E, R>(

Callers 1

zipWithPreviousAndNextFunction · 0.85

Calls 3

pipeFunction · 0.70
toChannelFunction · 0.70
processFunction · 0.70

Tested by

no test coverage detected