Options
All
  • Public
  • Public/Protected
  • All
Menu

External module "index"

Index

Variables

Let $

$: function

Type declaration

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

      Returns void

Let $n

$n: function

Type declaration

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

      • m: number
      • n: number

      Returns 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 )

Type declaration

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 )

Type declaration

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 )

Type declaration

Const bia

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

Applies the quotation q to x, then to y.

( x y q -- qx qy )

Type declaration

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 )

Type declaration

Const bis

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

Applies p to x, then applies q to y.

( x y p q -- px qy )

Type declaration

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 )

Type declaration

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 )

Type declaration

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 )

Type declaration

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 )

Type declaration

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 )

Type declaration

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 )

Type declaration

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 )

Type declaration

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 )

Type declaration

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 )

Type declaration

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 )

Type declaration

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

Type declaration

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])

Type declaration

Const pull2

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

Type declaration

Const pull3

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

Type declaration

Const pull4

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

Type declaration

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 )

Type declaration

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 )

Type declaration

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 )

Type declaration

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 )

Type declaration

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 )

Type declaration

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 -- )

Type declaration

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 )

Type declaration

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)

Type declaration

Const vec3

vec3: function = tuple(3)

Type declaration

Const vec4

vec4: function = tuple(4)

Type declaration

Const vmul

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

Const vsub

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

Functions

Const $stackFn

Const _swap

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

    • i: number

    Returns (Anonymous function)

Const _swap2

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

    • i: number

    Returns (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 )

    Parameters

    Returns StackContext

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.

    Parameters

    Returns (Anonymous function)

Const casesq

Const cat

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 --- ... [...] )

    Parameters

    Returns StackContext

Const compile

  • Parameters

    Returns function

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

        • Rest ...xs: any[]

        Returns 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 -- ? )

    Parameters

    Returns (Anonymous function)

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 -- ? )

    Parameters

    Returns StackContext

Const cp2

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

    • a: number
    • b: number

    Returns (Anonymous function)

Const cpdr

Const cprd

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

    Returns StackContext

Const dec

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.

    Parameters

    Returns StackContext

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 -- ? )

    Parameters

    Returns StackContext

Const drop

Const drop2

Const dropif

Const dsp

Const dup

Const dup2

Const dup3

Const dupif

Const exec

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

    ( x -- x() )

    Parameters

    Returns StackContext

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(...) )

    Parameters

    Returns StackContext

Const inc

Const invrot

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 separator.

    Parameters

    • Default value sep: string = ""

    Returns (Anonymous function)

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!

    ( -- [] )

    Parameters

    Returns StackContext

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] )

    Parameters

    Returns StackContext

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] )

    Parameters

    • key: PropertyKey

    Returns (Anonymous function)

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]])
    // 3
    // 2
    // 1
    // [ true, [ 0 ], undefined ]

    Parameters

    Returns (Anonymous function)

Const loopq

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 ]

    Parameters

    Returns StackContext

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 ] ], [], {} ]

    Parameters

    Returns StackContext

Const mov2

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

    • a: number
    • b: number

    Returns (Anonymous function)

Const movdr

Const movrd

Const nip

Const nop

Const obj

Const op1

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

    ( x -- y )

    Parameters

    • op: Fn<any, any>

    Returns (Anonymous function)

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 )

    Parameters

    • op: Fn2<any, any, any>

    Returns (Anonymous function)

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

    Parameters

    • f: Fn2<any, any, any>

    Returns (Anonymous function)

Const over

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] )

    Parameters

    Returns StackContext

Const popr

Const print

Const printds

Const printrs

Const push

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

    ( -- ...args )

    Parameters

    • Rest ...args: any[]

    Returns (Anonymous function)

Const pushenv

Const pushl

Const pushr

Const rand

Const rdec

Const rdrop

Const rdrop2

Const rinc

Const rot

Const rsp

Const run

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

    Parameters

    Returns StackContext

Const runE

  • Like run(), but returns result environment. Syntax sugar for: run(...)[2]

    Parameters

    Returns any

Const runU

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

    Parameters

    Returns any

Const safeMode

  • safeMode(state: boolean): void
  • Parameters

    • state: boolean

    Returns void

Const setat

Const split

Const store

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 -- )

    Parameters

    • key: PropertyKey

    Returns (Anonymous function)

Const tos

Const tuck

Const tuple

  • tuple(n: number | StackFn): function

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.

    Parameters

    • __namedParameters: [Array]
    • Default value n: number = 1

    Returns any

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.

    ( ? -- ? )

    Parameters

    Returns function

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.

    Parameters

    • prog: StackProgram
    • Default value n: number = 1
    • Optional env: StackEnv
    • Default value mergeEnv: boolean = true

    Returns (Anonymous function)

Generated using TypeDoc