import type { IterableArrayLike } from "@tsplus/stdlib/collections/Chunk" import { concreteChunkId } from "@tsplus/stdlib/collections/Chunk" /** * Returns the first element that satisfies the effectful predicate. * * @tsplus static effect/core/io/Effect.Ops find */ export function find( self: Collection, f: (a: A) => Effect ): Effect> { return Effect.suspendSucceed(() => { const chunk = Chunk.from(self) const iterator = concreteChunkId(chunk)._arrayLikeIterator() let next: IteratorResult, any> const loop = ( iterator: Iterator>, array: IterableArrayLike, i: number, length: number ): Effect> => { if (i < length) { const a = array[i]! return f(a).flatMap((r) => r ? Effect.succeed(Maybe.some(a)) : loop(iterator, array, i + 1, length) ) } else if (!(next = iterator.next()).done) { return loop(iterator, next.value, 0, next.value.length) } else { return Effect.succeed(Maybe.none) } } next = iterator.next() if (!next.done) { return loop(iterator, next.value, 0, next.value.length) } else { return Effect.succeed(Maybe.none) } }) }