Functions

Below you can find all functions included with Manglang. They are either built-in or implemented in the standard library.

Time Complexity

For all container functions we specify how the time complexity depends on the number of elements N in the container. So an operation that is marked as O(1) takes the same time for small and large containers. An operation that is marked as O(N) takes twice as long for a container that is twice as big.

Container Interface

Manglang has containers like: stacks, strings, tables. They share an interface of basic operations:
take
take!container returns a single item from the container. O(1).
drop
drop!container returns the container with a single item dropped from it. O(1).
put
put!(item container) returns a new container with item added to the old container. For tables we have that put!((key value) table) sets the value corresponding to the key in the table and returns the table. For stacks and strings the original container is not mutated, but it is mutated for tables so that other references to it are effected as well! O(1).
clear
clear!container returns an empty container of the same type as the input. O(1).
indexing
container!index can be used to get the item at the specified index. The container is interpreted as a function which takes an index as input and outputs an item. O(N).
We check if a container is not empty by if container then ... else ... O(1).

These operations can be used as a base for building more container functions. The standard library does that and adds all the functions below.

Generic Container Functions

These functions work on both stacks, strings, tables, and the type of the output container is the same as the input. We use the word container to represent either a stack, string or table.
fold
fold!(binary_operation container init) is mainly used as an algorithmic building block to implement the other container functions. O(N).
reverse
reverse!container returns a container with the items in the reverse order for stacks and string. For tables we just get a copy, since a table is always sorted by its keys. O(N).
take
take!container returns a single item from the container. O(1).
take_many
take_many!(n container) returns a new containers with n items taken from the input container. O(n).
take_while
take_while!(predicate container) returns a new containers with items taken from the input container as long as the predicate says yes. O(N).
take_until_item
take_until_item!(item container) returns a new containers with items taken from the input container until the query item is found. O(N).
drop
drop!container returns the container with a single item dropped from it. O(1).
drop_many
drop_many!(n container) returns the container with n items dropped from it. O(n).
drop_while
drop_while!(predicate container) returns the container with items dropped from it as long as the predicate says yes. O(N).
drop_until_item
drop_until_item!(item container) returns a container with items dropped until the query item is found. O(N).
clear
clear!container returns an empty container of the same type as the input. O(1).
clear_item
clear_item!(item container) returns a container with all occurances of the specified item removed. O(N).
clear_if
clear_if!(predicate container) returns a container without the items for which the predicate says yes. O(N).
replace
replace!(new_item container) returns a container where each item is replaced with new_item. O(N).
replace_item
replace_item!(old_item new_item container) returns a container with occurances of old_item replaced with new_item. O(N).
replace_if
replace_if!(predicate new_item container) returns a container where each item is replaced if the predicate says yes. O(N).
count
count!container returns the number of items in the container. O(N).
count_item
count!(item container) counts the number of occurances of an item in the container. O(N).
count_if
count_if!(predicate container) counts the number of items for which the predicate says yes. O(N).

Generic Container Functions - Accessing Items

You can access the items of a container via take!container and drop!container and container!index, or using the following functions:
get0
get0!container returns the item with index 0 in the container. O(1).
get1
get1!container returns the item with index 1 in the container. O(1).
get2
get2!container returns the item with index 2 in the container. O(1).
get3
get3!container returns the item with index 3 in the container. O(1).
get4
get4!container returns the item with index 4 in the container. O(1).
get5
get5!container returns the item with index 5 in the container. O(1).
get6
get6!container returns the item with index 6 in the container. O(1).
get7
get7!container returns the item with index 7 in the container. O(1).
get8
get8!container returns the item with index 8 in the container. O(1).
get9
get9!container returns the item with index 9 in the container. O(1).
These fixed getters are useful in two situations. Firstly, for passing to a higher order function like xs=map!(get0 points). Secondly, when using multiple indices to index containers of containers like scalar=get3!get2!matrix. Otherwise, the easiest option is container!index, which works well in most other situations.

Specialized Container Functions

For the following functions you need to know what kind of container you want to use:
make_stack
make_stack!container copies the items in the container to a new stack. O(N).
make_string
make_string!container copies the items in the container to a new string. O(N).
make_table
make_table!container copies the items in the container to a new table. The container should contain tuples of (key value). O(N).
merge_stack
merge_stack![c1 ... cn] concatenates a stack of containers into a single stack. O(N).
merge_string
merge_string![c1 ... cn] concatenates a stack of containers into a single string. O(N).
merge_table
merge_table![c1 ... cn] merges a stack of containers into a single table. O(N).
map
map!(f container) returns a new container with the function f applied to each item in the input container. The type of the output container is the same as the type of the input container. O(N).
map_stack
map_stack!(f container) returns a stack with the function f applied to each item in the container. O(N).
map_string
map_string!(f container) returns a string with the function f applied to each item in the container. O(N).
map_table
map_table!(f container) returns a table with the function f applied to each item in the container. The function f should return a tuple (key value). O(N).
range
range!n returns the stack [0 1 2 ... n-1]. O(n).
enumerate
enumerate![7 9 4] returns the stack of tuples [(0 7) (1 9) (2 4)], where the first item in each tuple is its index. O(N).
zip2
zip2!([1 2 3] [4 5 6]) returns the stack of tuples [(1 4) (2 5) (3 6)], where each inner tuple combines the corresponding items from the input tuple of stacks. O(N).
zip3
zip3!([1 2 3] [4 5 6] [7 8 9]) returns the stack of tuples [(1 4 7) (2 5 8) (3 6 9)], where each inner tuple combines the corresponding items from the input tuple of stacks. O(N).
zip4
zip4!([1 2] [3 4] [5 6] [7 8]) returns the stack of tuples [(1 3 5 7) (2 4 6 8)], where each inner tuple combines the corresponding items from the input tuple of stacks. O(N).
split
split!(' ' "hey hey, ok") returns ["hey" "hey," "ok"] and split!(1 [1 2 3 1 1 4]) returns [[] [2 3] [] [4]]. O(N).
cartesian_product2
cartesian_product2!([0 1] "ab") returns the stack of tuples [(1 'b') (0 'b') (1 'a') (0 'a')]. O(MN).
put_column
put_column!([1 2] [[3] [4]]) returns [[1 3] [2 4]]. O(N).
transpose
transpose![[1 2] [3 4]] returns [[1 3] [2 4]]. O(NM).

Table Functions

get
get!(key table default_value) returns the value corresponding to the key in the table, if it exists, otherwise default_value is returned. O(log N).
put
put!((key value) table) set the value corresponding to the key in the table and returns the table. Note that the table is also mutated so that other references to it are effected as well. O(log N).
get_keys
get_keys!table returns a stack of all keys in the table. O(N).
get_values
get_values!table returns a stack of all values in the table. O(N).
get_items
get_items!table returns a stack of tuples, of all pairwise keys and values in the table. O(N).
count_elements
count_elements!container returns a table where the keys are elements from the input container and the values are the number of times they occur in the input container. O(N log N).
unique
unique!container returns a stack with the unique elements of the input container. O(N log N).

Boolean Functions

boolean
Convert a value to a boolean. All values are converted to yes, except empty stack [] and empty string "" and number zero 0 and the boolean no which are all converted to no.
not
Convert a value to a boolean. All values are converted to no, except empty stack [] and empty string "" and number zero 0 and the boolean no which are all converted to yes.
all / and
Takes a stack of booleans and returns yes if all items are yes and otherwise no.
any / or
Takes a stack of booleans and returns yes if any item is yes and otherwise no.
none
Takes a stack of booleans and returns yes if all item are no and otherwise yes.
equal
Takes two values and returns yes if they are equal and otherwise no.
unequal
Takes two values and returns no if they are equal and otherwise yes.

Functions on Single Numbers

inc
Increments a number by adding 1 to it.
dec
Decrements a number by subtracting 1 from it.
neg
Negates a number.
abs
Absolute value of a number.
round
Round a number to closest integer.
round_up
Round a number up to closest integer.
round_down
Round a number down to closest integer.
sqrt
Square root of a number.

Functions on Pairs of Numbers

add
Add a tuple of two numbers.
mul
Multiply a tuple of two numbers.
sub
Subtract a tuple of two numbers.
div
Divide a tuple of two numbers.
mod
The remainder of the division of a tuple of two numbers.
min
Minimum of a tuple of two numbers.
max
Maximum of a tuple of two numbers.
less
Takes a tuple of two numbers and returns yes if the first is smaller than the second, otherwise no.
less_or_equal
Takes a tuple of two numbers and returns yes if the first is smaller or equal to the second, otherwise no.
greater
Takes a tuple of two numbers and returns yes if the first is larger than the second, otherwise no.

Functions on Stacks of Numbers

sum
Adds a stack of numbers to a single number. Returns 0 if the stack is empty.
product
Multiplies a stack of numbers to a single number. Returns 1 if the stack is empty.
min_item
Minimum of a stack of numbers. Returns inf if the stack is empty.
max_item
Maximum of a stack of numbers. Returns -inf if the stack is empty.
min_key
min_key!(key stack) returns the item in the stack for which the the result of the function application key!item is smallest. Requires the stack to be non-empty.
max_key
max_key!(key stack) returns the item in the stack for which the result of the function application key!item is largest. Requires the stack to be non-empty..
min_predicate
min_predicate!(predicate stack) compares all items in the stack and returns the item that is smallest according to the binary predicate. predicate!(left right) checks if left is smaller than right and returns a boolean. Requires the stack to be non-empty.
max_predicate
max_predicate!(predicate stack) compares all items in the stack and returns the item that is largest according to the binary predicate. predicate!(left right) checks if left is smaller than right and returns a boolean. Requires the stack to be non-empty.

Character Functions

number
Takes a character and returns its ascii number.
character
Takes a number and returns the character with that ascii number.
parse_digit
Takes a character and returns the corresponding number.
parse_natural_number
Takes a string and returns the corresponding non-negative integer.
is_digit
Is a character a digit? Returns yes or no.
is_letter
Is a character a letter? Returns yes or no.
is_upper
Is a character upper case? Returns yes or no.
is_lower
Is a character lower case? Returns yes or no.
to_upper
Takes a character and converts it to upper case. Only has an effect it the character is a lower case letter.
to_lower
Takes a character and converts it to lower case. Only has an effect it the character is an upper case letter.