import type * as P from "@tsplus/stdlib/prelude/ChainRec" /** * @tsplus static ImmutableArray.Ops depthFirstChainRec */ export function depthFirstChainRec( a: A, f: (a: A) => ImmutableArray> ): ImmutableArray { const todo: Array> = [...f(a)] const result: Array = [] while (todo.length > 0) { const e = todo.shift()! if (e._tag === "Left") { todo.unshift(...f(e.left)) } else { result.push(e.right) } } return new ImmutableArray(result) } /** * @tsplus static ImmutableArray.Ops DepthFirstChainRec */ export const DepthFirstChainRec = HKT.instance>({ chainRec: (f) => (a) => depthFirstChainRec(a, f) }) /** * @tsplus static ImmutableArray.Ops breadthFirstChainRec */ export function breadthFirstChainRec( a: A, f: (a: A) => ImmutableArray> ): ImmutableArray { const initial = f(a) const todo: Array> = [] const result: Array = [] for (const e of initial) { if (e._tag === "Left") { f(e.left).array.forEach((v) => todo.push(v)) } else { result.push(e.right) } } while (todo.length > 0) { const e = todo.shift()! if (e._tag === "Left") { f(e.left).array.forEach((v) => todo.push(v)) } else { result.push(e.right) } } return new ImmutableArray(result) } /** * @tsplus static ImmutableArray.Ops BreadthFirstChainRec */ export const BreadthFirstChainRec = HKT.instance>({ chainRec: (f) => (a) => breadthFirstChainRec(a, f) })