// ets_tracing: off import * as CK from "../../../../Collections/Immutable/Chunk/index.js" import * as T from "../../../../Effect/index.js" import * as O from "../../../../Option/index.js" import type * as C from "../core.js" import * as LoopOnPartialChunks from "./loopOnPartialChunks.js" /** * Effectfully transforms all elements of the stream for as long as the specified partial function is defined. */ export function collectWhileEffect_( self: C.Stream, pf: (a: A) => O.Option> ): C.Stream { return LoopOnPartialChunks.loopOnPartialChunks_(self, (chunk, emit) => { const pfSome = (a: A) => O.fold_( pf(a), () => T.succeed(false), (_) => T.as_(T.chain_(_, emit), true) ) const loop = (chunk: CK.Chunk): T.Effect => { if (CK.isEmpty(chunk)) { return T.succeed(true) } else { return T.chain_(pfSome(CK.unsafeHead(chunk)), (cont) => { if (cont) { return loop(CK.unsafeTail(chunk)) } else { return T.succeed(false) } }) } } return loop(chunk) }) } /** * Effectfully transforms all elements of the stream for as long as the specified partial function is defined. * * @ets_data_first collectWhileEffect_ */ export function collectWhileEffect( pf: (a: A) => O.Option> ) { return (self: C.Stream) => collectWhileEffect_(self, pf) }