Substacks

StackLang substacks are essentially lists that can be operated on from one end. These substacks act like a smaller version of the main stack. It is possible to have an unquoted command in a substack, however, it will not be evaluated. Substack elements don't count towards the size limit on the main stack.

Substacks are printed and parsed using the delimiters << and >>. Elements between these delimiters are parsed as individual elements of the substack. An EBNF definition of a valid substack parse is given below.

left-delimiter-symbol = "<<" ;
right-delimiter-symbol = ">>" ;
separator-symbol = "," ;
space-symbol = " " ;

stackelement = ? Any valid stack element, including another substack ? ;
left-delimiter = left-delimiter-symbol, {space-symbol} ;
right-delimiter = {space-symbol}, right-delimiter-symbol ;
separator = {space-symbol}, separator-symbol, {space-symbol} ;

substack = left-delimiter, {stackelement, separator}, [stackelement], right-delimiter ;

Substack-related Commands

Constants

empty : -> Substack
Produces the empty substack. Equivalent to <<>>.

Type Predicates

substack? : Any -> Boolean
Produces true if element is a substack.

empty? : Any -> Boolean
Produces true if element is an empty substack.

non-empty? : Any -> Boolean
Produces true if element is a non-empty substack.

contains-type? : Type Substack -> Boolean
Produces true if substack only contains elements of type Type.

Substack Operations

push : Any Substack -> Substack
Adds element on to substack at the active (printed left) end.

top : Substack -> Any
Produces the element closest to the active end (printed left) from the substack.

pop : Substack -> Substack
Removes the active end element from the substack.

pop* : Substack -> Any Substack
Produces the top and the pop of the substack.

second : Substack -> Any
Produces the second element of the substack.

last : Substack -> Any
Produces the last element of the substack.

make-substack : Any Any -> Substack
Produces a substack from two elements.

length : Substack -> Number
Produces the number of elements in the substack.

substack-ref : Number Substack -> Any
Produces the n'th element of the substack. Fails with a RuntimeError if the given number is invalid for this substack.

sub-substack: Number Number Substack -> Substack
Produces a portion of the given substack, using the first number as the index to start from (included), and the second number as ending index (excluded). Invalid numbers will cause a RuntimeError.

append : Substack Substack -> Substack
Combines two substacks into one, with the second substack's elements coming first in the produced substack.

reverse : Substack -> Substack
Reverses the substack.

insert : Substack Number Substack -> Substack
Inserts the first substack into the second, such that the first substack's first element is the n'th element of the new substack.

Abstract Commands

map : Command Substack -> Substack
Applies the command ( Any -> Any) to each element in the given substack, forming a new substack with the results. Resulting substack is as large as the input.

filter : Command Substack -> Substack
Applies the command ( Any -> Boolean) to each element in the given substack. If the command produces true, then the element that caused the command to produce true is kept. Otherwise, the element is discarded. Resulting substack is the same size or smaller than the input.

foldr : Command Any Substack -> Any
Applies the command ( Any Any -> Any) to the accumulator (initial value is the Any element) and each element from the substack (furthest from active end first), with the result of this application becoming the new accumulator. This does not use tail recursion. The given command should expect the element first, and the accumulator second.

foldl : Command Any Substack -> Any
Applies the command ( Any Any -> Any) to the accumulator (initial value is the Any element) and each element from the substack (active end first), with the result of this application becoming the new accumulator. This does use tail recursion.

sequence : Number -> Substack(Number)
Produces a sequence of numbers from zero to n, inclusive. Number must be an integer.

index-of : Any Command Substack -> Number
Produces the index for which the application of the command ( Any Any -> Boolean) produces true when applied to the first parameter and any element from the substack. If none exists, produces an index equal to the length of the substack.