Kenpali Core Specification
The Core functions must be available to any Kenpali program. Reference implementations for some of them in terms of the others are provided by core.kpc
, enabling a complete implementation with fewer builtins.
Arithmetic
plus
Returns the sum of its arguments
Parameters:
*numbers
(array of number): The numbers to add up.
Returns:
- (number): The sum.
sum
Returns the sum of the values in the specified sequence.
Parameters:
numbers
(sequence of number): The numbers to add up.
Returns:
- (number): The sum.
minus
Returns its first argument minus its second argument.
Parameters:
a
(number): The amount to subtract from.b
(number): The amount to take away.
Returns:
- (number): The difference.
negative
Returns its argument with the sign flipped.
Parameters:
n
(number): The value to negate.
Returns:
- (number): The negated value.
absolute
Returns its argument with the sign set to positive.
Parameters:
n
(number): The value to make positive.
Returns:
- (number): The absolute value.
up
Returns its argument plus one.
Parameters:
n
(number): The value to increase.
Returns:
- (number): The incremented value.
down
Returns its argument minus one.
Parameters:
n
(number): The value to decrease.
Returns:
- (number): The decremented value.
times
Returns the product of its arguments.
Parameters:
*numbers
(array of number): The values to multiply.
Returns:
- (number): The product.
dividedBy
Returns its first argument divided by its second argument.
Parameters:
a
(number): The dividend.b
(number): The divisor.
Returns:
- (number): The quotient.
oneOver
Returns the reciprocal of its argument.
Parameters:
x
(number): The value to invert.
Returns:
- (number): The reciprocal.
quotientBy
Returns the integer quotient of its first argument divided by its second argument.
Parameters:
a
(number): The dividend.b
(number): The divisor.
Returns:
- (number): The integer quotient.
remainderBy
Returns the remainder of its first argument divided by its second argument.
A non-zero remainder always has the same sign as the divisor.
Parameters:
a
(number): The dividend.b
(number): The divisor.
Returns:
- (number): The remainder.
isDivisibleBy
Returns whether its first argument is divisible by its second argument.
Parameters:
a
(number): The dividend.b
(number): The divisor.
Returns:
- (boolean): Whether
a
divided byb
is an integer.
Strings
toCodePoints
Returns an array of Unicode code points representing the characters in the given string.
Parameters:
string
(string): The text to convert.
Returns:
- (array of number): The code points.
fromCodePoints
Returns a string created from the given array of Unicode code points.
Parameters:
codePoints
(array of number): The code points to convert.
Returns:
- (string): The resulting text.
join
Returns a string created by joining the given values with a separator.
Parameters:
strings
(array or stream of string): The values to join.on:
(string, default""
): The separator to insert between values.
Returns:
- (string): The joined string.
joinLines
Returns a string created by joining the given values with newlines.
Parameters:
strings
(array or stream of string): The values to join.
Returns:
- (string): The joined string.
split
Returns an array of substrings by splitting the given string at occurrences of a separator.
Parameters:
string
(string): The text to split.on:
(string): The separator to split on.
Returns:
- (array of string): The split substrings.
splitLines
Returns an array of the newline-separated lines in the specified string.
Parameters:
string
(string): The text to split.
Returns:
- (array of string): The lines.
Comparison
Comparison Rules
Kenpali can perform equality and ordering comparisons on various types.
All comparisons are strict about types: testing values of different types for equality always returns false
, while using ordering comparisons on different types always throws a wrongArgumentType
error.
Any two values can be tested for equality. To be considered equal, the values must have the same type, and:
- If both are booleans, they must both be true or both be false.
- If both are numbers, they must have the same numerical value.
- If both are strings, they must have the same sequence of Unicode code points.
- If both are arrays, they must have the same length, and corresponding elements must be equal according to the
equals
function. - If both are objects, they must have the same property names, and corresponding property values must be equal according to the
equals
function. The order in which the properties are written does not affect equality. - Otherwise, they must be aliases referring to the same data in memory.
Ordering comparisons work on booleans, numbers, strings, and arrays, as follows:
- For booleans,
false
is less thantrue
. - Numbers are compared by numerical value.
- Strings are compared lexicographically by their Unicode code points.
- Arrays are compared lexicographically by their elements.
equals
Returns whether its arguments have equal values, according to the Comparison Rules.
Parameters:
a
(any): The first value.b
(any): The second value.
Returns:
- (boolean): Whether the values are equal.
isLessThan
Returns whether its first argument is less than its second argument, according to the Comparison Rules.
Parameters:
a
(number, string, boolean, or array): The first value.b
(number, string, boolean, or array): The second value.
Returns:
- (boolean): Whether
a
is less thanb
.
isAtMost
Returns whether its first argument is less than or equal to its second argument, according to the Comparison Rules.
Parameters:
a
(number, string, boolean, or array): The first value.b
(number, string, boolean, or array): The second value.
Returns:
- (boolean): Whether
a
is less than or equal tob
.
isMoreThan
Returns whether its first argument is greater than its second argument, according to the Comparison Rules.
Parameters:
a
(number, string, boolean, or array): The first value.b
(number, string, boolean, or array): The second value.
Returns:
- (boolean): Whether
a
is greater thanb
.
isAtLeast
Returns whether its first argument is greater than or equal to its second argument, according to the Comparison Rules.
Parameters:
a
(number, string, boolean, or array): The first value.b
(number, string, boolean, or array): The second value.
Returns:
- (boolean): Whether
a
is greater than or equal tob
.
isBetween
Returns whether its first argument is between the specified lower and upper bounds, according to the Comparison Rules.
Both bounds are inclusive.
Parameters:
n
(number, string, boolean, or array): The value to test.lower
(number, string, boolean, or array): The lower bound.upper
(number, string, boolean, or array): The upper bound.
Returns:
- (boolean): Whether
n
is betweenlower
andupper
.
least
Returns the least element in the specified sequence, i.e. the element that is less than or equal to all other elements, according to the Comparison Rules.
Parameters:
sequence
(sequence): The values to find the least element of.
Returns:
- (number, string, boolean, or array): The least element.
most
Returns the greatest element in the specified sequence, i.e. the element that is greater than or equal to all other elements, according to the Comparison Rules.
Parameters:
sequence
(sequence): The values to find the greatest element of.
Returns:
- (number, string, boolean, or array): The greatest element.
Logic
and
Returns whether all given conditions evaluate to true
. If any condition evaluates to false
, the remaining conditions are not evaluated.
Parameters:
first
(boolean): The first condition.*rest
(array of function returning boolean): The remaining conditions.
Returns:
- (boolean): Whether all conditions are
true
.
or
Returns whether at least one of the given conditions evaluates to true
. If any condition evaluates to true
, the remaining conditions are not evaluated.
Parameters:
first
(boolean): The first condition.*rest
(array of function returning boolean): The remaining conditions.
Returns:
- (boolean): Whether any condition is
true
.
not
Returns the logical negation of its argument.
Parameters:
x
(boolean): The value to negate.
Returns:
- (boolean): The negated value.
Types and Type Conversion
typeOf
Returns the Kenpali type of its argument.
Parameters:
value
(any): The value whose type to get.
Returns:
- (string): The Kenpali type.
isNull
Returns whether its argument is null
.
Parameters:
value
(any): The value to check.
Returns:
- (boolean): Whether
value
isnull
.
isBoolean
Returns whether its argument is a boolean.
Parameters:
value
(any): The value to check.
Returns:
- (boolean): Whether
value
is a boolean.
isNumber
Returns whether its argument is a number.
Parameters:
value
(any): The value to check.
Returns:
- (boolean): Whether
value
is a number.
toNumber
Returns the numeric representation of its argument.
Parameters:
value
(string or number): The value to convert.
Returns:
- (number): The numeric value.
Throws:
notNumeric
: Ifvalue
is a string that cannot be parsed as a number.
isString
Returns whether its argument is a string.
Parameters:
value
(any): The value to check.
Returns:
- (boolean): Whether
value
is a string.
toString
Returns the string representation of its argument.
Calling toString
on a stream reports already evaluated elements but never forces further evaluation.
Parameters:
value
(any): The value to convert.
Returns:
- (string): The string value.
isArray
Returns whether its argument is an array.
Parameters:
value
(any): The value to check.
Returns:
- (boolean): Whether
value
is an array.
toArray
Collects the elements of the specified sequence into an array.
Parameters:
value
(sequence): The value to convert.
Returns:
- (array): The array of elements.
isStream
Returns whether its argument is a stream.
Parameters:
value
(any): The value to check.
Returns:
- (boolean): Whether
value
is a stream.
toStream
Returns a stream that iterates over the elements of the specified sequence.
If the argument is already a stream, the same stream is returned, rather than a wrapper, i.e. x | toStream | equals(x)
is true if x
is a stream.
Parameters:
value
(sequence): The value to convert.
Returns:
- (stream): The stream of elements.
isObject
Returns whether its argument is an object.
Parameters:
value
(any): The value to check.
Returns:
- (boolean): Whether
value
is an object.
isBuiltin
Returns whether its argument is a builtin function, written in the platform language.
Parameters:
value
(any): The value to check.
Returns:
- (boolean): Whether
value
is a builtin function.
isGiven
Returns whether its argument is a pure-Kenpali function.
Parameters:
value
(any): The value to check.
Returns:
- (boolean): Whether
value
is a pure-Kenpali function.
isError
Returns whether its argument is an error.
Note that merely calling this function doesn’t catch a thrown error: if x
throws, x | isError
propagates the error rather than returning true
. The error must be explicitly caught first, i.e. x ! | isError
.
Parameters:
value
(any): The value to check.
Returns:
- (boolean): Whether
value
is an error.
isFunction
Returns whether its argument is a function.
Parameters:
value
(any): The value to check.
Returns:
- (boolean): Whether
value
is a function.
toFunction
Converts the specified value to a function:
- If the argument is already a function, the same function is returned.
- Otherwise, a constant function that returns the specified value is returned.
Parameters:
value
(any): The value to convert.
Returns:
- (function): The function.
isSequence
Returns whether its argument is a sequence—a string, array, or stream.
Parameters:
value
(any): The value to check.
Returns:
- (boolean): Whether
value
is a sequence.
Control Flow
if
Evaluates and returns the result of one of two functions based on a condition.
Parameters:
condition
(boolean): The condition to check.then:
(function): The function to call ifcondition
istrue
.else:
(function): The function to call ifcondition
isfalse
.
Returns:
- (any): The result of the evaluated function.
butIf
Substitutes a different value if a condition is true.
Parameters:
value
(any): The original value.condition
(boolean or function): The condition to check. If this is a function, it is called withvalue
as its argument to get the condition’s truth value.ifTrue
(function): A function that returns the value to substitue ifcondition
is true. This is called withvalue
as its argument.
Returns:
- (any):
value
ifcondition
is false, the result ofifTrue
otherwise.
ifs
Evaluates a list of conditions and returns the result of the first matching case.
Parameters:
conditions
(array of tuple like [function, function]): A list of condition-result pairs. Each condition function is called with no arguments, and if it returnstrue
, the corresponding result function is called with no arguments and its result is returned.else:
(function): The function to call if no conditions match.
Returns:
- (any): The result of the first matching case, or the
else
function if no conditions match.
switch
Evaluates a list of conditions on an input value and returns the result of the first matching case.
Parameters:
value
(any): The value to pass to conditions and results.conditions
(array of tuple like [function, function]): A list of condition-result pairs. Each condition function is called withvalue
, and if it returnstrue
, the corresponding result function is called withvalue
and its result is returned.else:
(function): The function to call if no conditions match.
Returns:
- (any): The result of the first matching case, or the
else
function if no conditions match.
Stream Builders
These functions build up new streams from scalar inputs.
build
Generates a stream by repeatedly applying a function to a start value.
Parameters:
start
(any): The start value.next
(function): A function that computes the next state from the current state.
Returns:
- (stream): A stream containing
start
and all values produced by thenext
function.
to
Generates a stream of numbers covering a range.
This is often useful for situations where other languages would use for
loops.
Parameters:
start
(number): The number to start counting from. This is always the first element of the stream.end
(number): The number to stop at or before.by:
(number, default1
): The number to count by. Each element of the stream will beby
more than the previous one. Ifby
is negative, the stream will count down. If the sign ofby
is opposite the sign ofend - start
(i.e. counting the “wrong way”), the resulting stream is empty.
Returns:
- (stream): A stream of numbers ranging from
start
toend
, incrementing byby
.
toSize
Generates a stream of incrementing numbers with a given size.
Parameters:
start
(number): The number to start counting from.size
(number): The number of elements to include in the stream.
Returns:
- (stream): A stream of numbers counting up from
start
, withsize
elements in total.
repeat
Creates a stream that repeats the same value forever.
Parameters:
value
(any): The value to repeat.
Returns:
- (stream): An infinite stream all of whose elements are
value
.
newStream
Explicitly creates a stream with the specified head value and tail stream.
Parameters:
value:
(function): A function that returns the first value in the stream.next:
(function): A function that returns the remaining values in the stream, as a stream. This normally makes a recursive call to the containing function, allowing the stream to continue indefinitely.
Returns:
- (stream): The new stream.
emptyStream
Creates a stream with no elements. Together with newStream
, this allows explicitly creating finite streams.
Returns:
- (stream): An empty stream.
Stream Collapsers
These functions exhaust an input stream to produce a scalar output or side effect. They loop forever if given an infinite stream.
last
Returns the last element of the specified sequence. Equivalent to indexing with -1
.
Parameters:
sequence
(sequence): The sequence to get the last element of.
Returns:
- (any): The last element.
length
Returns the number of elements in the specified sequence.
For strings, this is the number of characters.
Parameters:
sequence
(sequence): The sequence to find the length of.
Returns:
- (number): The length.
keepLast
Retains only the last n
elements of the input.
Parameters:
sequence
(sequence): The sequence to take values from.n
(number): The number of elements to keep.
Returns:
- (array or string): An array of at most
n
elements, or a string containing the lastn
characters ifsequence
is a string.
dropLast
Drops the last n
elements of the input.
Parameters:
sequence
(sequence): The sequence to drop values from.n
(number, default:1
): The number of elements to drop.
Returns:
- (array or string): An array with the last
n
elements dropped, or a string with the lastn
characters dropped ifsequence
is a string.
count
Returns the number of elements in the sequence matching the specified condition.
Parameters:
sequence
(sequence): The sequence to count from.condition
(function): A function that returnstrue
for elements to count.
Returns:
- (number): The number of elements for which
condition
returnstrue
.
forAll
Tests whether the specified condition is true for all elements in the sequence.
Parameters:
sequence
(sequence): The sequence to test.condition
(function): A function that returnstrue
for acceptable elements.
Returns:
- (boolean): Whether all elements match the condition.
forSome
Tests whether the specified condition is true for at least one element in the sequence.
Parameters:
sequence
(sequence): The sequence to test.condition
(function): A function that returnstrue
for acceptable elements.
Returns:
- (boolean): Whether any elements match the condition.
reverse
Returns an array containing all the elements of the specified sequence in reverse order.
Parameters:
sequence
(sequence): The sequence to reverse.
Returns:
- (array): The reversed array.
sort
Returns an array with the same elements as the specified sequence, but in ascending order.
If by
is null
, the elements are sorted in their natural order, following the Comparison Rules.
Otherwise, by
is called on each element to obtain a sort key, and the elements are sorted by that sort key, again following the Comparison Rules. The sort is stable: two elements with equal sort keys will appear in the same relative order in the sorted array as they were in the original sequence.
Parameters:
sequence
(sequence): The sequence to sort.by:
(function or null, defaultnull
): A function to produce a sort key from each element, ornull
to sort the elements in their natural order.
Returns:
- (array): The sorted array.
group
Groups a sequence of pairs by their first element.
Parameters:
pairs
(sequence): A sequence of pairs whose first element is the grouping key.onGroup:
(function, default(x) => x
): A function to call on each group after assembling it.
Returns:
- (array): A sequence of pairs whose first element is the grouping key, and whose second element is the result of calling
onGroup
on an array of the second elements of the input pairs with that grouping key.
groupBy
Groups a sequence by a key function.
Parameters:
sequence
(sequence): The sequence to group.by
(function): A function that returns a grouping key for a given value.onGroup:
(function, default(x) => x
): A function to call on each group after assembling it.
Returns:
- (array): A sequence of pairs whose first element is the grouping key, and whose second element is the result of calling
onGroup
on an array of the elements with that grouping key.
forEach
Applies a function to each element in a sequence for its side effects.
Parameters:
sequence
(sequence): The sequence of values to iterate over.action
(function): The function to apply to each element.
Returns:
- (array): The input sequence as an array.
Stream Accessors
These functions calculate a scalar value from a stream, but only access a finite number of elements to do so. Therefore, they are safe to call even on infinite streams.
isEmpty
Tests whether the specified sequence has no elements.
Parameters:
sequence
(sequence): The sequence to check for emptiness.
Returns:
- (boolean): Whether the sequence is empty.
first
Returns the first element of the specified sequence. Equivalent to indexing with 1
.
Parameters:
sequence
(sequence): The sequence to get the first element of.
Returns:
- (any): The first element.
Stream Rebuilders
These functions create new streams that depend on existing ones, preserving stream laziness.
transform
Creates a stream from applying a function to each element in an input sequence.
Parameters:
sequence
(sequence): The sequence of values to transform.f
(function): The function to apply to each element.
Returns:
- (stream): A stream of transformed values.
running
Maintains state while processing a sequence, producing a stream of intermediate states.
Parameters:
sequence
(sequence): The sequence of values to process.start:
(any): The initial state.next:
(function): A function that computes the next state from the current element and state. The state is passed as a named argumentstate:
.
Returns:
- (stream): A stream containing
start
and all values produced by thenext
function.
keepFirst
Retains only the first n
elements of the input.
Parameters:
sequence
(sequence): The sequence to take values from.n
(number): The number of elements to keep.
Returns:
- (stream or string): A stream of at most
n
elements, or a string containing the firstn
characters ifsequence
is a string.
dropFirst
Skips the first n
elements of the input.
Parameters:
sequence
(sequence): The sequence to skip values from.n
(number, default:1
): The number of elements to drop.
Returns:
- (stream or string): A stream with the first
n
elements skipped, or a string with the firstn
characters skipped ifsequence
is a string.
slice
Extracts a sub-sequence of the specified sequence.
Parameters:
sequence
(sequence): The sequence to take values from.from:
(number): The index of the first element to take.to:
(number): The index of the last element to take.
Returns:
- (stream or string): A stream containing only the elements from index
from
to indexto
, or a string with the characters at those indices ifsequence
is a string.
while
Creates a stream of elements from the input sequence while a condition holds.
Parameters:
sequence
(sequence): The sequence to process.condition
(function returning boolean): A function that returnsfalse
when the stream should stop.
Returns:
- (stream): A stream containing elements for which
condition
returnstrue
, stopping at the firstfalse
.
continueIf
Creates a stream of elements from the input sequence, continuing to the next element if a condition holds. The resulting stream has one extra element compared to while
—the first element that doesn’t satisfy the condition—which makes some stopping conditions easier to express.
Parameters:
sequence
(sequence): The sequence to process.condition
(function returning boolean): A function that returnsfalse
when the stream should stop.
Returns:
- (stream): A stream containing the first element for which the condition returns
false
, and all elements before it.
thenRepeat
Adds endless copies of a constant value to the end of the sequence.
This is useful if subsequent steps need to continue past the end of the stream.
Parameters:
sequence
(sequence): The initial sequence.value
(value): The value to repeat aftersequence
is exhausted.
sliding
Returns a stream containing arrays of adjacent elements in the input sequence.
Parameters:
sequence
(sequence): The input sequence.size
(number): The number of adjacent elements in each output array.
Returns:
- (stream): A stream containing arrays with
size
consecutive elements each. The first array starts with the first element of the input sequence, the second array with the second element, and so on, with the last array ending on the last element of the input sequence.
where
Filters a sequence, keeping only elements that satisfy a condition.
Parameters:
sequence
(sequence): The sequence to filter.condition
(function): A function that returnstrue
for elements to keep.
Returns:
- (stream): A stream of elements where
condition
returnstrue
.
zip
Combines multiple sequences into a stream of tuples, stopping when the shortest sequence is exhausted.
Parameters:
*sequences
(array of sequence): The sequences to combine.
Returns:
- (stream): A stream of tuples, where each tuple contains corresponding elements from the input sequences.
unzip
Splits a sequence of tuples into multiple streams.
Parameters:
sequence
(sequence of array): A sequence where each element is a tuple.numStreams:
(number, default:2
): The number of streams to produce.
Returns:
- (array of stream): An array of
numStreams
streams, where thei
-th stream contains thei
-th element from each tuple.
flatten
Flattens a sequence of sequences into a single stream.
Parameters:
sequences
(sequence of sequence): The sequence to flatten.
Returns:
- (stream): A stream containing all elements from the nested sequences.
dissect
Splits a sequence into chunks at elements that satisfy a condition.
Parameters:
sequence
(sequence): The sequence to process.condition
(function): A function that determines split points.
Returns:
- (stream of array): A stream of arrays, where each array contains a group of elements up to and including a split point.
chunk
Splits a sequence into arrays of a fixed size.
Parameters:
sequence
(sequence): The sequence to split.size
(number): The number of elements in each output array.
Returns:
- (stream): A stream containing arrays with
size
elements each, except that the last array may have fewer elements.
Objects
keys
Returns an array of the keys in an object.
Parameters:
object
(object): The object whose keys to retrieve.
Returns:
- (array): An array of the object’s keys.
toObject
Converts the specified value into an object.
If the value is an array or stream, its elements are treated as key-value pairs. Any duplicate keys are assigned the value from the last pair with that key in the sequence.
If the value is an error, an object containing the error details is returned.
Parameters:
value
(array or stream or error): The value to convert. If an array or stream, it must contain tuples like [string, any].
Returns:
- (object): An object constructed from
value
.
properties
Returns an array of the key-value pairs in the object.
Parameters:
object
(object): The object whose properties to retrieve.
Returns:
- (array): An array of the object’s properties.
merge
Combines the properties of the specified objects into a new object.
If the same key appears in more than one input object, the resulting object takes the property value from the last such object.
Parameters:
objects
(array of object): The objects to combine.
Returns:
- (object): The combined object.
Indexing
at
Returns the value at the specified index in the specified collection, or a default value if the index is invalid.
Without the default value, a | at(b)
is equivalent to a @ b
.
Parameters:
collection
(sequence or object): The collection to index into.index
(number or string): The index value.default:
(function or null, defaultnull
): A function to call and return the result of if the index is invalid, ornull
to throw an error if the index is invalid.
Utilities
debug
Writes the specified value to a diagnostic log. Exactly where this goes depends on the implementation and configuration; writing to standard error is a common default.
This function returns its argument, allowing it to be dropped into pipelines temporarily without disrupting their structure. For example, 42 | foo | bar | debug | baz
has the same result as 42 | foo | bar | baz
, but it writes bar
’s return value to the diagnostic log.
Parameters:
value
(any): The value to write.name
(string or null, defaultnull
): If provided, the output value will be tagged with this, making it clearer which debug values came from where.
Returns:
- (any): The
value
argument.
itself
Returns its argument. Useful when a function must be provided, but no transformation is needed.
Parameters:
x
(any): The argument.
Returns:
- (any): The argument.
Sets and Maps
newSet
Creates an immutable set.
A set is like an array, but it can’t have duplicate elements, and checking whether an element is in the set is fast regardless of the size of the set.
Parameters:
elements
(array, default[]
): The set’s elements. Duplicate elements are silently discarded.
Returns:
- (object): The new set.
newSet/size
Returns:
- (number): The number of elements in the set.
newSet/elements
Returns:
- (array): An array containing all the elements in the set.
newSet/has
Checks whether the specified element is in the set.
Parameters:
element
(any): The value to look for.
Returns:
- (boolean): Whether the value is in the set.
newMap
Creates an immutable map.
A map is a collection of key-value pairs, where keys are unique. Looking up a value by key is fast regardless of the size of the map.
Parameters:
entries
(array, default[]
): An array of[key, value]
pairs. If duplicate keys exist, only the last one is kept.
Returns:
- (object): The new map.
newMap/size
Returns:
- (number): The number of key-value pairs in the map.
newMap/keys
Returns:
- (array): An array containing all keys in the map.
newMap/values
Returns:
- (array): An array containing all values in the map.
newMap/entries
Returns:
- (array): An array of
[key, value]
pairs representing the map’s contents.
newMap/has
Checks whether the specified key exists in the map.
Parameters:
key
(any): The key to check.
Returns:
- (boolean): Whether the key is in the map.
newMap/at
Retrieves the value associated with the specified key.
Parameters:
key
(any): The key whose value to retrieve.default
(function or null, defaultnull
): A function that provides a default value if the key is not found, ornull
to throw an error if the key is missing.
Returns:
- (any): The value associated with the key, or the result of calling
default
if the key is not found.
Mutable Objects
variable
Creates a mutable variable.
A variable holds a value that can be retrieved or updated.
Parameters:
initialValue
(any): The initial value of the variable.
Returns:
- (object): The new variable.
variable/get
Returns the current value of the variable.
Returns:
- (any): The current value.
variable/set
Updates the variable’s value.
Parameters:
newValue
(any): The new value to assign.
Returns:
- (any): The updated value.
mutableArray
Creates a mutable array.
A mutable array allows updating arbitrary elements, adding new elements to the end, and removing elements from the end.
Parameters:
elements
(array, default[]
): The initial elements of the array.
Returns:
- (object): The new mutable array.
mutableArray/size
Returns:
- (number): The number of elements in the array.
mutableArray/elements
Returns:
- (array): An array containing all elements in the mutable array.
mutableArray/append
Adds an element to the end of the array.
Parameters:
element
(any): The value to append.
Returns:
- (object): The mutable array itself, for chaining.
mutableArray/set
Replaces the element at the specified index.
Parameters:
index
(number): The position to update (1-based, negative values count from the end).element
(any): The new value.
Returns:
- (object): The mutable array itself, for chaining.
Throws:
indexOutOfBounds
: If the index is out of range.
mutableArray/storeAt
Alias for set
, but with parameters in reversed order. Useful as the final step in a pipeline that computes the new element value.
Parameters:
element
(any): The new value.index
(number): The position to update (1-based, negative values count from the end).
Returns:
- (object): The mutable array itself, for chaining.
Throws:
indexOutOfBounds
: If the index is out of range.
mutableArray/at
Retrieves the element at the specified index.
Parameters:
index
(number): The position to retrieve (1-based, negative values count from the end).default:
(function or null, defaultnull
): A function returning a default value if the index is out of range, ornull
to throw an error.
Returns:
- (any): The element at the index, or the result of
default
if out of range.
mutableArray/pop
Removes and returns the last element.
Parameters:
default:
(function or null, defaultnull
): A function returning a default value if the array is empty, ornull
to throw an error.
Returns:
- (any): The last element, or the result of
default
if the array is empty.
mutableArray/clear
Removes all elements from the array.
Returns:
- (object): The mutable array itself, for chaining.
mutableSet
Creates a mutable set.
A mutable set is like a regular set, but it allows adding and removing elements dynamically.
Parameters:
elements
(array, default[]
): The initial elements of the set. Duplicate elements are silently discarded.
Returns:
- (object): The new mutable set.
mutableSet/size
Returns:
- (number): The number of elements in the set.
mutableSet/elements
Returns:
- (array): An array containing all elements in the mutable set.
mutableSet/add
Adds an element to the set.
Parameters:
element
(any): The value to add.
Returns:
- (object): The mutable set itself, for chaining.
mutableSet/remove
Removes an element from the set.
Parameters:
element
(any): The value to remove.
Returns:
- (object): The mutable set itself, for chaining.
mutableSet/has
Checks whether the specified element is in the set.
Parameters:
element
(any): The value to look for.
Returns:
- (boolean): Whether the value is in the set.
mutableSet/clear
Removes all elements from the set.
Returns:
- (object): The mutable set itself, for chaining.
mutableMap
Creates a mutable map.
A mutable map is a collection of key-value pairs where keys are unique, and entries can be added, updated, and removed dynamically.
Parameters:
entries
(array, default[]
): An array of[key, value]
pairs to initialize the map.
Returns:
- (object): The new mutable map.
mutableMap/size
Returns:
- (number): The number of entries in the map.
mutableMap/keys
Returns:
- (array): An array containing all keys in the map.
mutableMap/values
Returns:
- (array): An array containing all values in the map.
mutableMap/entries
Returns:
- (array): An array of
[key, value]
pairs representing the map’s entries.
mutableMap/set
Sets the value for a given key.
Parameters:
key
(any): The key to set.value
(any): The value to associate with the key.
Returns:
- (object): The mutable map itself, for chaining.
mutableMap/storeAt
Alias for set
, but with parameters in reversed order. Useful as the final step in a pipeline that computes the new element value.
Parameters:
value
(any): The value to store.key
(any): The key to associate with the value.
Returns:
- (object): The mutable map itself, for chaining.
mutableMap/remove
Removes a key and its associated value from the map.
Parameters:
key
(any): The key to remove.
Returns:
- (object): The mutable map itself, for chaining.
mutableMap/has
Checks whether the map contains a specific key.
Parameters:
key
(any): The key to check.
Returns:
- (boolean): Whether the key exists in the map.
mutableMap/at
Retrieves the value associated with a given key.
Parameters:
key
(any): The key to look up.default:
(function or null, defaultnull
): A function returning a default value if the key is not found, ornull
to throw an error.
Returns:
- (any): The value associated with the key, or the default value if the key is not found.
mutableMap/clear
Removes all entries from the map.
Returns:
- (object): The mutable map itself, for chaining.
also
Performs an action on a value, then returns it for further processing.
Parameters:
value
(any): The value to perform the action on.action
(function): The function to apply.
Returns:
- (any): The
value
argument.
Errors
error
Creates an error value with the specified error type and details. This in itself doesn’t throw the error.
Parameters:
type
(string): The kind of error condition that this error represents.**details
(object): Any relevant information about the cause of the error.
Validation
Kenpali is a dynamically typed language, but it does strict type checks at runtime. The core module exposes several functions to interact with the type checking system.
Values are checked against a schema. Valid schemas are:
- A string representing the expected Kenpali type: one of
"null"
,"boolean"
,"number"
,"string"
,"array"
,"stream"
,"sequence"
,"object",
,"builtin"
,"given"
,"function"
,"error"
,"any"
. - An object of the form
{oneOf: [<values>]}
, which matches only the specified values. - An object of the form
{either: [<schemas>]}
, which matches a value if at least one of the specified schemas matches it. - An object of the form
{type: <type>, ...}
. On its own, this is equivalent to a string schema, e.g.{type: "number"}
matches the same values as"number"
. But this format allows additional properties to narrow the range of accepted values:- For arrays, the
elements
property specifies the schema that all the array’s elements must match, e.g.{type: "array", elements: "number"}
matches only array of numbers. - For arrays, the
shape
property specifies an array of individual schemas for each element, e.g.{type: "array", shape: ["number", "string"]}
matches only arrays whose first element is a number and whose second element is a string. - For objects, the
values
property specifies the schemas that all the object’s property values must match, e.g.{type: "object", values: "number"}
matches only objects whose properties are all numbers. - For objects, the
shape
property specifies an object whose properties are schemas that the corresponding properties must match, e.g.{type: "object", shape: {foo: "number", bar: "string"}}
matches only objects with a numericfoo
property and a stringbar
property. - The
where
property specifies an arbitrary function that must returntrue
when called on the value, e.g.{type: "number", where: | isLessThan(10)}
matches only numbers less than 10.
- For arrays, the
validate
Ensures that the specified value matches a schema. Throws a validation error if the value doesn’t match.
Parameters:
value
(any): The value to check.schema
(string or object): The schema to check against.
Returns:
- (boolean): Always
true
.
matches
Checks whether the specified value matches a schema.
Parameters:
value
(any): The value to check.schema
(string or object): The schema to check against.
Returns:
- (boolean): Whether the value matches the schema.
is
Creates a schema that checks the type of the value, and optionally that it satisfies a predicate.
Parameters:
type
(string): The expected Kenpali type.where:
(function or null, defaultnull
): A predicate that the value must satisfy.
Returns:
- (object): A schema of the form
{type, where}
.
oneOf
Creates a schema that checks if the value is in the specified list.
Parametersː
*values
(array of any): The allowed values.
Returns:
- (object): A schema of the form
{oneOf: values}
.
arrayOf
Creates a schema that checks the elements of an array against a single schema, and optionally that the array satisfies a predicate.
Parameters:
elementSchema
(string or object): The schema that all elements must match.where:
(function or null, defaultnull
): A predicate that the array must satisfy.
Returns:
- (object): A schema of the form
{type: "array", elements: elementSchema, where}
.
tupleLike
Creates a schema that checks each element of an array against a separate schema.
Parameters:
shape
(array of string or object): A schema that each element of the array must match.
Returns:
- (object): A schema of the form
{type: "array", shape}
.
objectOf
Creates a schema that checks the property values of an object against a single schema, and optionally that the keys match a schema and/or that the entire object satisfies a predicate.
Parameters:
keys:
(string or object, default: “string”): The schema that all keys must match.values:
(string or object): The schema that all values must match.where:
(function or null, defaultnull
): A predicate that the object must satisfy.
Returns:
- (object): An object of the form
{type: "object", keys, values, where}
.
recordLike
Creates a schema that checks each property of an object against a separate schema.
Parameters:
shape
(object of string or object): A schema that each property of the object must match.
Returns:
- (object): A schema of the form
{type: "object", shape}
.
optional
Creates a schema that marks an array element or object property as optional.
The returned schema is only valid inside the shape
property of an array or object schema.
Parameters:
schema
(string or object): The schema that the element or property must match if present.
Returns:
- (object): A schema of the form
{optional: schema}
.
either
Creates a schema that allows anything matching any of the specified schemas.
Parameters:
*schemas
(array of string or object): The allowed schemas.
Returns:
- (object): A schema of the form
{either: schemas}
.