// ets_tracing: off import * as CK from "../../../../Collections/Immutable/Chunk/index.js" import type * as Tp from "../../../../Collections/Immutable/Tuple/index.js" import * as CH from "../../Channel/index.js" import * as C from "../core.js" /** * Statefully maps over the elements of this stream to produce new elements. */ export function mapAccum_( self: C.Stream, s: S, f: (s: S, a: A) => Tp.Tuple<[S, A1]> ): C.Stream { const accumulator = ( currS: S ): CH.Channel, unknown, E, CK.Chunk, void> => CH.readWith( (in_) => { const { tuple: [nextS, a2s] } = CK.mapAccum_(in_, currS, f) return CH.zipRight_(CH.write(a2s), accumulator(nextS)) }, (err) => CH.fail(err), (_) => CH.unit ) return new C.Stream(self.channel[">>>"](accumulator(s))) } /** * Statefully maps over the elements of this stream to produce new elements. * * @ets_data_first mapAccum_ */ export function mapAccum(s: S, f: (s: S, a: A) => Tp.Tuple<[S, A1]>) { return (self: C.Stream) => mapAccum_(self, s, f) }