@thi.ng/pointfree
Version:
Pointfree functional composition / Forth style stack execution engine
65 lines • 1.65 kB
TypeScript
import type { StackContext, StackProc } from "./api.js";
/**
* Higher order word. Takes a `test` and `body` stack program. Applies
* test to copy of TOS and executes body. Repeats while test is truthy.
*
* Stack effect: `( -- ? )`
*
* @example
* ```js
* import { dec, dup, ispos, print, run } from "@thi.ng/pointfree";
*
* run([loop([dup, ispos], [dup, print, dec])], [[3]])
* // 3
* // 2
* // 1
* // [ true, [ 0 ], undefined ]
* ```
*
* @param test -
* @param body -
*/
export declare const defLoop: (test: StackProc, body: StackProc) => (ctx: StackContext) => StackContext;
/**
* Non-HOF version of {@link defLoop}. Expects test result and body quotation /
* word on d-stack.
*
* Stack effect: `( testq bodyq -- ? )`
*
* @param ctx -
*/
export declare const loopq: (ctx: StackContext) => StackContext;
/**
* Executes given `body` word/quotation `n` times. In each iteration
* pushes current counter on d-stack prior to executing body.
*
* @example
* ```js
* import { add, print, swap, runU } from "@thi.ng/pointfree";
*
* run([3, ["i=", swap, add, print], dotimes])
* // i=0
* // i=1
* // i=2
* ```
*
* With empty body acts as finite range generator 0 .. n:
*
* ```js
* import { collect, cpdr, dotimes, movrd, run, runU } from "@thi.ng/pointfree";
*
* // range gen
* run([3, [], dotimes])
* [ [ 0, 1, 2 ], [], {} ]
*
* // range gen (collect results as array)
* runU([3, cpdr, [], dotimes, movrd, collect])
* // [ 0, 1, 2 ]
* ```
*
* Stack effect: `( n body -- ? )`
*
* @param body -
*/
export declare const dotimes: (ctx: StackContext) => StackContext;
//# sourceMappingURL=loop.d.ts.map