Type Functions
Every value in Menter has functions that can be called on it using the .
dot syntax. The following will show most of these functions, most importantly on object
values.
General information
List all functions
All functions on any type can be listed using the functions
function.
Bind functions to values
Bound functions can be stored in variables and be called later. This is useful for having quick access to functions that you use often on a specific value.
Chaining function calls
For chaining function calls, depending on the situation, you can use the:
.
operator for chaining calls on the value|>
and>|
operators for piping the value into the next function call
Functions for all types
type
Returns the type of the value as a string.
isNull
Returns whether the value is null
.
Object functions (object
type)
As you can see, there are quite a few functions on objects. Most of them are self-explanatory, but some are not. The following will explain the most important ones.
foldl
, foldr
, reduce
These functions are used to reduce a list of values to a single value. The difference between foldl
and foldr
is that foldl
starts aggregating values from the left, while foldr
starts from the right. reduce
is the same as foldl
.
map
, mapKeys
The map
function is used to transform a list of values or object into another list or object of values. The function passed to map
will be called for each value in the list and the result of that function will be added to the new list. The type of the result does not have to be the same as the type of the original list.
Just as always when looping over a list/object, the function can take one or two arguments: Only the value or the key and value.
filter
The filter
function is used to filter a list of values or object into another list or object of values. The function passed to filter
will be called for each value in the list and the value will be added to the new list if the function returns true
.
sort
, sortKey
The sort
function is used to sort the items in a list or object. If no function is passed to sort
, the items will be sorted using their identity. If a function is passed, the items will be sorted using the result of that function.
The same goes for sortKey
, but it will sort the items using the key.
join
The join
function is used to join a list of values into a string. The function takes a separator string which will be used to separate the values in the string.
contains
, containsKey
, containsValue
The contains
and containsValue
functions are used to check whether a list or object contains a value. The function takes a value and returns whether the list or object contains that value.
Similar to that, the containsKey
function is used to check whether an object contains a key.
sum
, avg
, min
, max
These functions are used to calculate the sum, average, minimum and maximum of a list of values. The sum
function takes no arguments, while the avg
, min
and max
functions optionally take a function that serves as the comparator for the values.
distinct
The distinct
function is used to remove duplicate values from a list. The function takes no arguments.
frequency
The frequency
function is used to count the number of occurrences of each value in a list. The function takes no arguments and returns an object with the values as keys and the number of occurrences as values, ordered by the number of occurrences.
Here's a small example:
cross
The cross
function is used to create a list of all possible combinations of two lists. The function takes a list of values and returns a list of lists of values.
It optionally takes a filter function that will be called for each combination and only the combinations for which the function returns true
will be added to the result. The result can also be chained into another call. In this case, the previous result will be stored in an array that has to be accessed first.
A cool application for this can be found in the examples section.
head
, tail
The head
function is used to get the first element of a list. The tail
function is used to get all elements of a list except the first one. Both functions take no arguments.
rename
The rename
function is used to rename the keys of an object. The function takes an old key and a new key. Note that the object is directly modified and not copied.
keys
, values
These functions are used to get the keys or values of an object.