import type { AnyParams } from "../../../types/array.mjs"; import type { Creater, Func, Method } from "../../../types/concepts.mjs"; import type { CacheMap } from "../data-structure/interfaces/schema.mjs"; declare type ProtectedRecursiveParam = [param: T]; declare type ProtectedRecursiveCaller = Func, ProtectedRecursiveCallRequest>; export declare type RecursiveCallGenerator = Generator; export declare type ProtectedRecursiveCallGenerator = RecursiveCallGenerator, R>; export declare type RawRecursiveCallGenerator

= RecursiveCallGenerator; interface ProtectedRecursiveCallMemoConfig { /** * Whether to use cache by parameter. * default `false` */ cacheParam: boolean; /** * The cache factory function. * default `() => new Map()` */ cacheFactory: Creater>; } interface RecursiveConfig { /** * Max recursive stack size. `Infinity` by default. */ maxStack: number; } export interface RecursiveCallConfig extends RecursiveConfig { memo: ProtectedRecursiveCallMemoConfig; } interface ProtectedRecursiveCallRequest { readonly payload: T; } interface RawRecursiveCallThisContext

{ call: Func; /** * The internal iterator stack. * Don't try to invoke `generator.next()` or modify it, or the stack might be corrupted. */ stack: RawRecursiveCallGenerator[]; } interface ProtectedRecursiveCallThisContext { call: ProtectedRecursiveCaller; } interface GeneralRecursiveCallThisContext

{ call: Func; } export declare type RawRecursiveCallFactory

= Method, P, RawRecursiveCallGenerator>; export declare type ProtectedRecursiveCallFactory = Method, ProtectedRecursiveParam, ProtectedRecursiveCallGenerator>; /** * To make factory functions with only one parameter to work with both {@link protectedRecursive} and {@link rawRecursive}, use this type for annotation. * It's not completely type safe, so make sure to use `yield this.call(param)` to perform a recursive call. */ export declare type GeneralRecursiveFactory = Method>, ProtectedRecursiveParam, Generator>; /** * Perform a recursive computation without increasing call stack frames as the recursion grow. * A Fibonacci sequence example with this API: * ```ts * // 1. Define a recursive function by call this API like this: * const fibo = rawRecursive<[n: number], number>( * // 2. Pass a generator function as the parameter * function* (n) { * if (n === 0 || n === 1) { * return 1; * } * // 3. Use `yield this.call(...params)` to perform a recursive call * const fn2 = yield this.call(n - 2); * const fn1 = yield this.call(n - 1); * return fn2 + fn1; * }); * // 4. Use the defined recursive function to compute: * console.log(fibo(1)) // 1 * console.log(fibo(2)) // 2 * console.log(fibo(8)) // 34 * ``` * @param factory the factory function which returns a generator * @returns sync recursive result */ export declare const rawRecursive:

(factory: RawRecursiveCallFactory) => Func; /** * Same core logic with {@link rawRecursive} but safer than {@link rawRecursive} with extra config. * This API calls {@link rawRecursive} internally. * The parameter generator function should have **exactly one parameter**. * A Fibonacci sequence example with this API: * ```ts * // 1. Define a recursive function by call this API like this: * const fibo = protectedRecursive( * // 2. Pass a generator function as the parameter. * function* (n) { * if (n === 0 || n === 1) { * return 1; * } * // 3. Use `yield this.call(params)` to perform a recursive call * const fn2 = yield this.call(n - 2); * const fn1 = yield this.call(n - 1); * return fn2 + fn1; * }); * // 4. Use the defined recursive function to compute: * console.log(fibo(1)) // 1 * console.log(fibo(2)) // 2 * console.log(fibo(8)) // 34 * ``` * @param factory the factory function which returns a generator * @param config the config * @returns sync recursive result */ export declare const protectedRecursive: (factory: ProtectedRecursiveCallFactory, config?: { memo?: { cacheParam?: boolean | undefined; cacheFactory?: Creater> | undefined; } | undefined; maxStack?: number | undefined; } | undefined) => Func, R>; export declare const recursive: (factory: ProtectedRecursiveCallFactory, config?: { memo?: { cacheParam?: boolean | undefined; cacheFactory?: Creater> | undefined; } | undefined; maxStack?: number | undefined; } | undefined) => Func, R>; interface RecursiveGeneratorValueRequest { readonly type: "value"; readonly payload: T; } interface RecursiveGeneratorSequenceRequest

{ readonly type: "seq"; readonly payload: P; } declare type RecursiveGeneratorRequest

= RecursiveGeneratorSequenceRequest

| RecursiveGeneratorValueRequest; interface RecursiveGeneratorValueResponse { type: "next"; payload: N; } interface RecursiveGeneratorSequenceResponse { type: "return"; payload: R; } declare type RecursiveGeneratorResponse = RecursiveGeneratorSequenceResponse | RecursiveGeneratorValueResponse; interface RawRecursiveGeneratorThisContext

extends ProtectedRecursiveGeneratorThisContext { stack: RecursiveGenerator[]; } interface ProtectedRecursiveGeneratorThisContext

{ value: Func<[value: T], RecursiveGeneratorValueRequest>; sequence: Func>; } declare type RecursiveGenerator

= Generator, R, RecursiveGeneratorResponse>; export declare type RawRecursiveGeneratorFactory

= Method, P, RecursiveGenerator>; export declare type ProtectedRecursiveGeneratorFactory

= Method, P, RecursiveGenerator>; export declare const rawRecursiveGenerator:

(factory: RawRecursiveGeneratorFactory) => (...args: P) => Generator; export declare const protectedRecursiveGenerator:

(factory: ProtectedRecursiveGeneratorFactory, config?: Partial | undefined) => (...args: P) => Generator; export declare const recursiveGenerator:

(factory: ProtectedRecursiveGeneratorFactory, config?: Partial | undefined) => (...args: P) => Generator; export {}; //# sourceMappingURL=recursive.d.mts.map