Options
All
• Public
• Public/Protected
• All

# External module "index"

## Variables

### Let \$

\$: function

#### Type declaration

• (stack: Stack, n: number): void

### Let \$n

\$n: function

#### Type declaration

• (m: number, n: number): void

### Const add

add: (Anonymous function) = op2((b, a) => a + b)

( x y -- x+y )

param

### Const and

and: (Anonymous function) = op2((b, a) => !!a && !!b)

( x y -- x&&y )

param

### Const at

at: (Anonymous function) = op2((b, a) => a[b])

Reads key/index from object/array.

( obj k -- obj[k] )

param

### Const atan2

atan2: (Anonymous function) = op2(Math.atan2)

( x y -- atan2(y,x) )

param

### Const bi

bi: function = word([[keep], dip, exec])

First applies `p` to the value `x`, then applies `q` to the same value.

( x p q -- px qx )

### Const bi2

bi2: function = word([[keep2], dip, exec])

First applies `p` to the two input values `x y`, then applies `q` to the same values.

( x y p q -- pxy qxy )

### Const bi3

bi3: function = word([[keep3], dip, exec])

First applies `p` to the three input values `x y z`, then applies `q` to the same values.

( x y z p q -- pxyz qxyz )

### Const bia

bia: function = word([dup, bis])

Applies the quotation `q` to `x`, then to `y`.

( x y q -- qx qy )

### Const bia2

bia2: function = word([dup, bis2])

Applies the quotation `q` to `x y`, then to `z w`.

( x y z w q -- qxy qzw )

### Const bis

bis: function = word([[dip], dip, exec])

Applies `p` to `x`, then applies `q` to `y`.

( x y p q -- px qy )

### Const bis2

bis2: function = word([[dip2], dip, exec])

Applies `p` to `a b`, then applies `q` to `c d`.

( a b c d p q -- pab qcd )

### Const bitand

bitand: (Anonymous function) = op2((b, a) => a & b)

( x y -- x&y )

param

### Const bitnot

bitnot: (Anonymous function) = op1((x) => ~x)

( x -- ~x )

param

### Const bitor

bitor: (Anonymous function) = op2((b, a) => a | b)

( x y -- x|y )

param

### Const bitxor

bitxor: (Anonymous function) = op2((b, a) => a ^ b)

( x y -- x^y )

param

### Const both

both: function = word([bia, and])

Applies `q` individually to both input vals `x y` and combines results with `and`. The final result will be true if both interim results were truthy.

( x y q -- qx && qy )

### Const ceil

ceil: (Anonymous function) = op1(Math.ceil)

( x -- ceil(x) )

param

### Const copy

copy: (Anonymous function) = op1((x) =>isArray(x)? [...x]: isPlainObject(x)? { ...x }: illegalArgs(`can't copy type \${typeof x}`))

Replaces TOS with its shallow copy. MUST be an array or plain object.

( x -- copy )

### Const cos

cos: (Anonymous function) = op1(Math.cos)

( x -- cos(x) )

param

### Const cpdr2

cpdr2: (Anonymous function) = cp2(0, 1)

### Const cprd2

cprd2: (Anonymous function) = cp2(1, 0)

### Const dip2

dip2: function = word([swap, [dip], dip])

Removes `x y` from d-stack, calls `q` and restores removed vals to the top of the d-stack after quotation is finished.

( x y q -- x y )

### Const dip3

dip3: function = word([swap, [dip2], dip])

Removes `x y z` from d-stack, calls `q` and restores removed vals to the top of the d-stack after quotation is finished.

( x y z q -- x y z )

### Const dip4

dip4: function = word([swap, [dip3], dip])

Removes `x y z w` from d-stack, calls `q` and restores removed vals to the top of the d-stack after quotation is finished.

( x y z w q -- x y z w )

### Const div

div: (Anonymous function) = op2((b, a) => a / b)

( x y -- x/y )

param

### Const either

either: function = word([bia, or])

Applies `q` individually to both input vals `x y` and combines results with `or`. The final result will be true if at least one of the interim results was truthy.

( x y q -- qx || qy )

### Const eq

eq: (Anonymous function) = op2((b, a) => a === b)

( x y -- x===y )

param

### Const equiv

equiv: (Anonymous function) = op2(_equiv)

( x y -- equiv(x,y) )

param

### Const even

even: (Anonymous function) = op1((x) => !(x & 1))

( x -- bool )

param

### Const exp

exp: (Anonymous function) = op1(Math.exp)

( x -- exp(x) )

param

### Const floor

floor: (Anonymous function) = op1(Math.floor)

( x -- floor(x) )

param

### Const foldl

foldl: function = word([invrot, mapl])

Convenience wrapper for `mapl` to provide an alternative stack layout for reduction purposes:

( arr q init -- reduction )

### Const gt

gt: (Anonymous function) = op2((b, a) => a > b)

( x y -- x>y )

param

### Const gteq

gteq: (Anonymous function) = op2((b, a) => a >= b)

( x y -- x>=y )

param

### Const hypot

hypot: (Anonymous function) = op2(Math.hypot)

( x y -- sqrt(xx+yy) )

param

### Const isneg

isneg: (Anonymous function) = op1((x) => x < 0)

( x -- x<0 )

param

### Const isnull

isnull: (Anonymous function) = op1((x) => x == null)

( x -- x==null )

param

### Const ispos

ispos: (Anonymous function) = op1((x) => x > 0)

( x -- x>0 )

param

### Const iszero

iszero: (Anonymous function) = op1((x) => x === 0)

( x -- x===0 )

param

### Const keep

keep: function = word([over, [exec], dip])

Calls a quotation with a value on the d-stack, restoring the value after quotation finished.

( x q -- .. x )

### Const keep2

keep2: function = word([[dup2], dip, dip2])

Call a quotation with two values on the stack, restoring the values after quotation finished.

( x y q -- .. x y )

### Const keep3

keep3: function = word([[dup3], dip, dip3])

Call a quotation with three values on the stack, restoring the values after quotation finished.

( x y z q -- .. x y z )

### Const length

length: (Anonymous function) = op1((x) => x.length)

Pushes length of TOS on d-stack.

( x -- x.length )

param

### Const log

log: (Anonymous function) = op1(Math.log)

( x -- log(x) )

param

### Const lsl

lsl: (Anonymous function) = op2((b, a) => a << b)

( x y -- x<<y )

param

### Const lsr

lsr: (Anonymous function) = op2((b, a) => a >> b)

( x y -- x>>y )

param

### Const lsru

lsru: (Anonymous function) = op2((b, a) => a >>> b)

( x y -- x>>>y )

param

### Const lt

lt: (Anonymous function) = op2((b, a) => a < b)

( x y -- x<y )

param

### Const lteq

lteq: (Anonymous function) = op2((b, a) => a <= b)

( x y -- x<=y )

param

### Const max

max: (Anonymous function) = op2(Math.max)

( x y -- max(x,y) )

param

### Const min

min: (Anonymous function) = op2(Math.min)

( x y -- min(x,y) )

param

### Const mod

mod: (Anonymous function) = op2((b, a) => a % b)

( x y -- x%y )

param

### Const movdr2

movdr2: (Anonymous function) = mov2(0, 1)

### Const movrd2

movrd2: (Anonymous function) = mov2(1, 0)

### Const mul

mul: (Anonymous function) = op2((b, a) => a * b)

( x y -- x*y )

param

### Const neg

neg: (Anonymous function) = op1((x) => -x)

( x -- -x )

param

### Const neq

neq: (Anonymous function) = op2((b, a) => a !== b)

( x y -- x!==y )

param

### Const not

not: (Anonymous function) = op1((x) => !x)

( x -- !x )

param

### Const odd

odd: (Anonymous function) = op1((x) => !!(x & 1))

( x -- bool )

param

### Const oneover

oneover: function = word([1, swap, div])

( x -- 1/x )

param

### Const or

or: (Anonymous function) = op2((b, a) => !!a || !!b)

( x y -- x||y )

param

### Const pow

pow: (Anonymous function) = op2((b, a) => Math.pow(a, b))

( x y -- pow(x,y) )

param

### Const pull

pull: function = word([popr, swap])

### Const pull2

pull2: function = word([pull, pull])

### Const pull3

pull3: function = word([pull2, pull])

### Const pull4

pull4: function = word([pull2, pull2])

### Const rswap

rswap: (Anonymous function) = _swap(1)

Swaps the two topmost r-stack items.

( x y -- y x )

param

### Const rswap2

rswap2: (Anonymous function) = _swap2(1)

Swaps the two topmost d-stack pairs.

( a b c d -- c d a b )

param

### Const sin

sin: (Anonymous function) = op1(Math.sin)

( x -- sin(x) )

param

### Const sqrt

sqrt: (Anonymous function) = op1(Math.sqrt)

( x -- sqrt(x) )

param

### Const sub

sub: (Anonymous function) = op2((b, a) => a - b)

( x y -- x-y )

param

### Const swap

swap: (Anonymous function) = _swap(0)

Swaps the two topmost d-stack items.

( x y -- y x )

param

### Const swap2

swap2: (Anonymous function) = _swap2(0)

Swaps the two topmost d-stack pairs.

( a b c d -- c d a b )

param

### Const tan

tan: (Anonymous function) = op1(Math.tan)

( x -- tan(x) )

param

### Const tanh

tanh: (Anonymous function) = op1(Math.tanh)

( x -- tanh(x) )

param

### Const tri

tri: function = word([[[keep], dip, keep], dip, exec])

Applies `p` to `x`, then `q` to `x`, and finally `r` to `x`

( x p q r -- px qx rx )

### Const tri2

tri2: function = word([[[keep2], dip, keep2], dip, exec])

Applies `p` to the two input values `x y`, then same with `q`, and finally with `r`.

( x y p q r -- pxy qxy rxy )

### Const tri3

tri3: function = word([[[keep3], dip, keep3], dip, exec])

Applies `p` to the three input values `x y z`, then same with `q`, and finally with `r`.

( x y z p q r -- pxyz qxyz rxyz )

### Const tria

tria: function = word([dup, dup, tris])

Applies the `q` to `x`, then to `y`, and finally to `z`.

( x y z q -- qx qy qz )

### Const tria2

tria2: function = word([dup, dup, tris2])

Applies the quotation to `u v`, then to `w x`, and then to `y z`.

( u v w x y z q -- quv qwx qyz )

### Const tris

tris: function = word([[[dip2], dip, dip], dip, exec])

Applies `p` to `x`, then `q` to `y`, and finally `r` to `z`.

( x y z p q r -- )

### Const tris2

tris2: function = word([[dip4], dip2, bis2])

Applies `p` to `u v`, then `q` to `w x`, and finally `r` to `y z`.

( u v w x y z p q r -- puv qwx ryz )

### Const vadd

vadd: (Anonymous function) = op2v((b, a) => a + b)

### Const vdiv

vdiv: (Anonymous function) = op2v((b, a) => a / b)

### Const vec2

vec2: function = tuple(2)

### Const vec3

vec3: function = tuple(3)

### Const vec4

vec4: function = tuple(4)

### Const vmul

vmul: (Anonymous function) = op2v((b, a) => a * b)

### Const vsub

vsub: (Anonymous function) = op2v((b, a) => a - b)

## Functions

### Const _swap

• _swap(i: number): (Anonymous function)

### Const _swap2

• _swap2(i: number): (Anonymous function)

### Const bindkeys

• Takes an array of keys and target object, then pops & binds deeper stack values to respective keys in object. Pushes result object back on stack at the end. Throws error if there're less stack values than keys in given array.

``````runU([1,2,3, ["a","b","c"], {}, bindkeys])
// { c: 3, b: 2, a: 1 }``````

(v1 v2 .. [k1 k2 ..] obj -- obj )

### Const cases

• cases(cases: IObjectOf<StackProc>): (Anonymous function)
• Higher order word. Takes an object of stack programs with keys in the object being used to check for equality with TOS. If a match is found, executes corresponding stack program. If a `default` key is specified and no other cases matched, run `default` program. In all other cases throws an error.

### Const cat

• Concatenates two arrays on d-stack:

( arr1 arr2 -- arr )

### Const collect

• Pops TOS (a number) and then forms a tuple of the top `n` remaining values and pushes it as new TOS. The original collected stack values are removed from d-stack.

( ... n --- ... [...] )

### Const compile

#### Returns function

• (...xs: any[]): T

### Const cond

• Higher order word. Takes two stack programs: truthy and falsey branches, respectively. When executed, pops TOS and runs only one of the branches depending if TOS was truthy or not.

Note: Unlike JS `if() {...} else {...}` constructs, the actual conditional is NOT part of this word.

( bool -- ? )

### Const condq

• Non-HOF version of `cond`, expects `test` result and both branches on d-stack. Executes `thenq` word/quotation if `test` is truthy, else runs `elseq`.

( test thenq elseq -- ? )

### Const cp2

• cp2(a: number, b: number): (Anonymous function)

### Const ctx

• Creates a new StackContext tuple from given d-stack and/or environment only (the r-stack is always initialized empty).

#### Parameters

• ##### Default value stack: Stack = []

initial d-stack

• ##### Default value env: StackEnv = {}

initial environment

• ( x -- x-1 )

### Const dip

• Removes `x` from d-stack, calls `q` and restores `x` to the top of the d-stack after quotation is finished.

( x q -- x )

Same behavior as: `[swap, movdr, exec, movrd]`, only the current implementation doesn't use r-stack and stashes `x` off stack.

### Const dotimes

• Executes given `body` word/quotation `n` times. In each iteration pushes current counter on d-stack prior to executing body.

``````pf.run([3, ["i=", pf.swap, pf.add, pf.print], pf.dotimes])
// i=0
// i=1
// i=2``````

With empty body acts as finite range generator 0 .. n:

``````// range gen
pf.run([3, [], pf.dotimes])
[ [ 0, 1, 2 ], [], {} ]

// range gen (collect results as array)
pf.runU([3, pf.cpdr, [], pf.dotimes, pf.movrd, pf.collect])
// [ 0, 1, 2 ]``````

( n body -- ? )

### Const drop

• Removes TOS from d-stack.

( x -- )

### Const drop2

• Removes top 2 vals from d-stack.

( x y -- )

### Const dropif

• If TOS is truthy then drop it:

( x -- )

Else, no effect:

( x -- x )

### Const dsp

• Pushes current d-stack size on d-stack.

( -- n )

### Const dup

• Duplicates TOS on d-stack.

( x -- x x )

### Const dup2

• Duplicates top 2 vals on d-stack.

( x y -- x y x y )

### Const dup3

• Duplicates top 3 vals on d-stack.

( x y -- x y x y )

### Const dupif

• If TOS is truthy then push copy of it on d-stack:

( x -- x x )

Else, no effect:

( x -- x )

### Const exec

• Executes TOS as stack function and places result back on d-stack. TOS MUST be a valid word or quotation.

( x -- x() )

### Const execjs

• Expects TOS to be a quotation with a vanilla JS function as first element. Calls fn with all remaining items in quot as arguments and pushes result back on d-stack (even if fn returned `undefined`).

( [f ...] -- f(...) )

• ( x -- x+1 )

### Const invrot

• Rotates three topmost d-stack items upwards/to the right.

( x y z -- z x y )

### Const join

• join(sep?: string): (Anonymous function)
• Higher order helper word to convert a TOS tuple/array into a string using `Array.join()` with given `sep`arator.

### Const list

• Pushes a new empty array on the d-stack. While it's easily possible to use `[]` as part of a stack program, the `list` word is intended to be used as part of re-usuable `word()` definitions to ensure a new array is being created for every single invocation of the word (else only a single instance is created due to the mutable nature of JS arrays).

Compare:

``````// using array literal within word definition
foo = pf.word([ [], 1, pf.pushl ])
pf.runU(foo)
// [ 1 ]
pf.runU(foo)
// [ 1, 1 ] // wrong!

// using `list` instead
bar = pf.word([ pf.list, 1, pf.pushl ])
pf.runU(bar)
// [ 1 ]
pf.runU(bar)
// [ 1 ] // correct!``````

( -- [] )

### Const load

• Loads value for `key` from current env and pushes it on d-stack. Throws error if var doesn't exist.

( key -- env[key] )

### Const loadkey

• loadkey(key: PropertyKey): (Anonymous function)
• Higher order word. Similar to `load`, but always uses given preconfigured `key` instead of reading it from d-stack at runtime (also slightly faster). Throws error if var doesn't exist.

( -- env[key] )

### Const loop

• Higher order word. Takes a `test` and `body` stack program. Applies test to copy of TOS and executes body. Repeats while test is truthy.

( -- ? )

``````run([loop([dup, ispos], [dup, print, dec])], [])
// 3
// 2
// 1
// [ true, [ 0 ], undefined ]``````

### Const loopq

• Non-HOF version of `loop`. Expects test result and body quotation / word on d-stack.

( testq bodyq -- ? )

### Const mapl

• Generic array transformer.

( arr q -- ? )

Pops both args from d-stack, then executes quotation for each array item (each pushed on d-stack prior to calling quotation). Can produce any number of results and therefore also be used as filter, mapcat, reduce...

``````// each item times 10
run([[1, 2, 3, 4], [10, mul], mapl])
// [ [ 10, 20, 30, 40 ], [], {} ]``````

Use for filtering:

``````// drop even numbers, duplicate odd ones
run([[1, 2, 3, 4], [dup, even, cond(drop, dup)], mapl])
// [ [ 1, 1, 3, 3 ], [], {} ]``````

Reduction:

``````// the `0` is the initial reduction result
runU([0, [1, 2, 3, 4], [add], mapl])
// 10``````

Important: `mapl` does not produce a result array. However, there're several options to collect results as array, e.g.

Use `mapll()` to transform:

``````runU([[1, 2, 3, 4], [10, mul], mapll])
// [ 10, 20, 30, 40]``````

Collecting results as array is a form of reduction, so we can use `list` to produce an initial new array and `pushr` to push each new interim value into the result:

``````runU([list, [1, 2, 3, 4], [10, mul, pushr], mapl])
// [ 10, 20, 30, 40 ]``````

If the array size is known & not changed by transformation:

``````runU([[1, 2, 3, 4], [10, mul], mapl, 4, collect])
// [ 10, 20, 30, 40 ]``````

### Const mapll

• Similar to `mapl()`, but produces new array of transformed values.

( arr q -- arr )

``````runU([[1, 2, 3, 4], [10, mul], mapll])
// [ 10, 20, 30, 40]``````

Filter / mapcat:

``````// drop even numbers, duplicate odd ones
run([[1, 2, 3, 4], [dup, even, cond(drop, dup)], mapll])
// [ [ [ 1, 1, 3, 3 ] ], [], {} ]``````

### Const mov2

• mov2(a: number, b: number): (Anonymous function)

### Const nip

• Removes second topmost item from d-stack.

( x y -- y )

### Const nop

• Utility word w/ no stack nor side effect.

### Const obj

• Pushes new empty JS object on d-stack. Same reasoning as for `list`.

( -- {} )

### Const op1

• op1(op: Fn<any, any>): (Anonymous function)
• Higher order word. Replaces TOS of d-stack with result of given op.

( x -- y )

### Const op2

• op2(op: Fn2<any, any, any>): (Anonymous function)
• Higher order word. Takes 2 values from d-stack and writes back result from given op. The arg order is (TOS, TOS-1)

( a b -- c )

### Const op2v

• op2v(f: Fn2<any, any, any>): (Anonymous function)
• Similar to `op2`, but for array operators. Either `a` or `b` can be a non-array value, but not both. Creates new array of result values. The result will have the same length as the shortest arg (if `a` and `b` have different lengths).

• ( a b -- a ), if `a` is an array
• ( a b -- b ), if `a` is not an array

### Const over

• Pushes copy of TOS-1 as new TOS on d-stack.

( x y -- x y x )

### Const pick

• Uses TOS as index to look up a deeper d-stack value, then places it as new TOS. Throws error if stack depth is < `x`.

( ... x -- ... stack[x] )

### Const popr

• Removes RHS from array as new TOS on d-stack. Throws error is `arr` is empty.

( arr -- arr arr[-1] )

### Const print

• Prints TOS to console

( x -- )

### Const push

• push(...args: any[]): (Anonymous function)
• Higher order word. Pushes given args verbatim on d-stack.

( -- ...args )

### Const pushenv

• Pushes current env onto d-stack.

( -- env )

### Const pushl

• Pushes `val` on the LHS of array.

( val arr -- arr )

### Const pushr

• Pushes `val` on the RHS of array.

( arr val -- arr )

### Const rand

• ( -- Math.random() )

### Const rdec

• Like `dec`, but applies to r-stack TOS.

### Const rdrop

• Removes TOS from r-stack.

( x -- )

### Const rdrop2

• Removes top 2 vals from r-stack.

( x y -- )

### Const rinc

• Like `inc`, but applies to r-stack TOS.

### Const rot

• Rotates three topmost d-stack items downwards/to the left.

( x y z -- y z x )

### Const rsp

• Pushes current r-stack size on d-stack.

( -- n )

### Const run

• Executes program / quotation with given stack context (initial D/R stacks and optional environment). Returns updated context.

### Const runE

• Like `run()`, but returns result environment. Syntax sugar for: `run(...)`

### Const runU

• Like `run()`, but returns unwrapped result. Syntax sugar for: `unwrap(run(...),n)`

### Const safeMode

• safeMode(state: boolean): void

### Const setat

• Writes `val` at key/index in object/array.

( val obj k -- obj )

### Const split

• Splits vector / array at given index `x`.

( arr x -- [...] [...] )

### Const store

• Stores `val` under `key` in env.

( val key -- )

### Const storekey

• storekey(key: PropertyKey): (Anonymous function)
• Higher order word. Similar to `store`, but always uses given preconfigure `key` instead of reading it from d-stack at runtime (also slightly faster).

( val -- )

### Const tuck

• Inserts copy of TOS @ TOS-2 in d-stack.

( x y -- y x y )

### Const tuple

• tuple(n: number | StackFn): function
• Higher order helper word to `collect()` tuples of pre-defined size `n`. The size can be given as number or a stack function producing a number.

( ... -- [...])

### Const unwrap

• unwrap(__namedParameters: [Array], n?: number): any
• Takes a result tuple returned by `run()` and unwraps one or more items from result stack. If no `n` is given, defaults to single value (TOS) and returns it as is. Returns an array for all other `n`.

### Const word

• Higher order word. Takes a StackProgram and returns it as StackFn to be used like any word. Unknown stack effect.

If the optional `env` is given, uses a shallow copy of that environment (one per invocation) instead of the current one passed by `run()` at runtime. If `mergeEnv` is true (default), the user provided env will be merged with the current env (also shallow copies). This is useful in conjunction with `pushenv()` and `store()` or `storekey()` to save results of sub procedures in the main env.

Note: The provided (or merged) env is only active within the execution scope of the word.

( ? -- ? )

### Const wordU

• Like `word()`, but automatically calls `unwrap()` on result context to produced unwrapped value/tuple.

Importatant: Words defined with this function CANNOT be used as part of a larger stack program, only for standalone use.

#### Returns (Anonymous function)

Generated using TypeDoc