import { concreteChunkId } from "@tsplus/stdlib/collections/Chunk"
/**
* Statefully and effectfully maps over the elements of this chunk to produce
* new elements.
*
* @tsplus static effect/core/io/Effect.Ops mapAccum
*/
export function mapAccum(
self: Collection,
s: S,
f: (s: S, a: A) => Effect
): Effect]> {
return Effect.suspendSucceed(() => {
const chunk = Chunk.from(self)
const iterator = concreteChunkId(chunk)._arrayLikeIterator()
let dest: Effect = Effect.succeed(s)
let builder = Chunk.empty()
let next
while ((next = iterator.next()) && !next.done) {
const array = next.value
const length = array.length
let i = 0
while (i < length) {
const a = array[i]!
dest = dest.flatMap((state) =>
f(state, a).map(([s, b]) => {
builder = builder.append(b)
return s
})
)
i++
}
}
return dest.map((s) => [s, builder] as const)
})
}