// ets_tracing: off import { _A, _E } from "../Effect/commons.js" import { NoSuchElementException } from "../GlobalExceptions/index.js" import type { Option } from "../Option/index.js" import * as Utils from "../Utils/index.js" import type { Either } from "./core.js" import { chain_, left, right } from "./core.js" export class GenEither { readonly [_E]!: () => E; readonly [_A]!: () => A constructor(readonly effect: Either) {} *[Symbol.iterator](): Generator, A, any> { return yield this } } function adapter(_: any, __?: any) { return Utils.isOption(_) ? new GenEither( _._tag === "Some" ? right(_.value) : left(__ ? __() : new NoSuchElementException()) ) : new GenEither(_) } export function gen, AEff>( f: (i: { (_: Option, onNone: () => E): GenEither (_: Option): GenEither (_: Either): GenEither }) => Generator ): Either, AEff> { const iterator = f(adapter as any) const state = iterator.next() function run( state: IteratorYieldResult | IteratorReturnResult ): Either { if (state.done) { return right(state.value) } return chain_(state.value["effect"], (val) => { const next = iterator.next(val) return run(next) }) } return run(state) }