// ets_tracing: off import * as core from "../../../../Effect/core.js" import type { Effect } from "../../../../Effect/effect.js" import type * as Chunk from "../core.js" import { concreteId } from "../definition.js" function loop( s: S, iterator: Iterator, any, undefined>, array: ArrayLike, i: number, length: number, pred: (s: S) => boolean, f: (s: S, a: A) => Effect ): Effect { if (i < length) { if (pred(s)) { return core.chain_(f(s, array[i]!), (s1) => loop(s1, iterator, array, i + 1, length, pred, f) ) } else { return core.succeed(s) } } else { const next = iterator.next() if (next.done) { return core.succeed(s) } else { const arr = next.value return core.suspend(() => loop(s, iterator, arr, 0, arr.length, pred, f)) } } } /** * Folds over the elements in this chunk from the left. * Stops the fold early when the condition is not fulfilled. */ export function reduceWhileEffect_( self: Chunk.Chunk, s: S, pred: (s: S) => boolean, f: (s: S, a: A) => Effect ): Effect { const iterator = concreteId(self).arrayLikeIterator() const next = iterator.next() if (next.done) { return core.succeed(s) } else { const array = next.value const length = array.length return loop(s, iterator, array, 0, length, pred, f) } } /** * Folds over the elements in this chunk from the left. * Stops the fold early when the condition is not fulfilled. * * @ets_data_first reduceWhileEffect_ */ export function reduceWhileEffect( s: S, pred: (s: S) => boolean, f: (s: S, a: A) => Effect ): (self: Chunk.Chunk) => Effect { return (self) => reduceWhileEffect_(self, s, pred, f) }