// ets_tracing: off import * as T from "../../../../Effect/index.js" import * as O from "../../../../Option/index.js" import type * as Chunk from "../core.js" import { concreteId } from "../definition.js" /** * Returns the first element that satisfies the effectful predicate. */ export function findEffect_( self: Chunk.Chunk, f: (a: A) => T.Effect ): T.Effect> { return T.suspend(() => { const iterator = concreteId(self).arrayLikeIterator() let next: IteratorResult, any> const loop = ( iterator: Iterator>, array: Chunk.IterableArrayLike, i: number, length: number ): T.Effect> => { if (i < length) { const a = array[i]! return T.chain_(f(a), (r) => r ? T.succeed(O.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 T.succeed(O.none) } } next = iterator.next() if (!next.done) { return loop(iterator, next.value, 0, next.value.length) } else { return T.succeed(O.none) } }) } /** * Returns the first element that satisfies the effectful predicate. * * @ets_data_first findEffect_ */ export function findEffect(f: (a: A) => T.Effect) { return (self: Chunk.Chunk) => findEffect_(self, f) }