import { rawRecursive } from "../../algorithms/recursive.mjs"; import { none, Option, some } from "../common/option.mjs"; import type { Comparator } from "../interfaces/schema.mjs"; import type { BinaryNode } from "./binary-tree-schema.mjs"; export const typedBinarySearchFactory = >() => rawRecursive< [node: N | null, entry: N["entry"], comparator: Comparator], Option >(function* (node, entry, comparator) { if (node === null) { return none(); } const result = comparator(node.entry, entry); if (result > 0) { return yield this.call(node.left, entry, comparator); } if (result < 0) { return yield this.call(node.right, entry, comparator); } return some(node.entry); });