Functions
February 21, 2019 ยท View on GitHub
These system functions are defined within the Ketos interpreter. They are available, by default, in any scope.
Arithmetic Functions
Basic arithmetic functions include +, -, *, /, //, ^, and rem.
They perform the same operation as their Rust counterparts. The only exception
being that, if the given values of different numeric types, the values will
be coerced according to these rules:
- For all operations:
integerandfloat=>floatintegerandratio=>ratioratioandfloat=>float
- For division:
integerandintegerwill result inratioif the two values cannot be evenly divided.
- For exponentation:
ratioandratioresults in afloat- If the right-hand side is a negative
integer, the result is alwaysfloat
The // function, which does not exist in Rust, is the "floor division"
function. It will divide its arguments as normal and return the floor
of the value.
Bitwise Functions
Bitwise functions <<, >>, bit&, bit|, bit^, and bit! are supported.
Comparison Functions
The equality function =, inequality function /= and ordered comparison
functions, <, <=, >, and >= are supported. With the exception of above
numeric coercion rules, these functions will produce an error if two values of
different types are received.
The zero function tests whether given values are equal to zero.
Numeric Functions
absreturns the absolute value of a numeric value.ceilrounds a numeric value toward positive infinity.floorrounds a numeric value toward negative infinity.truncrounds a numeric value toward zero.introunds a numeric value toward zero and returns aninteger.floatreturns a numeric value converted into afloat.infreturns whether a givenfloatvalue is infinite; given no arguments, it will instead return a value of positive infinity.nanreturns whether a givenfloatvalue isNaN; given no arguments, it will instead return a value ofNaN.denomreturns the denominator of a rational value.numerreturns the numerator of a rational value.fractreturns the fractional portion of aratioorfloat.ratwill convert a value to aratioor compose aratiofrom twointegervalues.recipreturns the reciprocal of a numeric value.
List Functions
appendappends a value to a list, e.g.(append list value).eltreturns the nth element of a list, e.g.(elt list n).concatconcatenates each given list value.joinjoins together a series of lists using the first argument as separator.lenreturns the length of a list.slicereturns a subslice of a list value, e.g.(slice list begin end).firstreturns the first element of a list.secondreturns the second element of a list.lastreturns the last element of a list.initreturns all elements until the last element of a list.tailreturns all elements after the first element of a list.listevaluates each of its arguments and return them as a list.reversereturns a list with elements in reverse order.
String Functions
concatconcatenates a series of string or char values.joinjoins together a series of strings using the first argument as separator.lenreturns the length, in bytes, of a string.charsreturns a list of char values for each successive char in a string.stringreturns a char value as a string.
Struct Functions
newreturns a new struct value with named field values, e.g.(new Foo :a 1 :b "foo").is-instancereturns whether a given struct value is an instance of a given struct-def, e.g.(is-instance Foo foo-value)..returns a named field of a struct value, e.g.(. struct :foo)..=returns a struct value with the named field assigned to a new value, e.g.(.= struct :foo bar). Accepts many keyword-value pairs.
Other Functions
bytes, converts a string or list of integers into a byte string.id, the identity function, returns its argument as-is.type-ofreturns a name value indicating the type of its argument.isreturns whether the type of value matches the given type, e.g.(is 'integer 0).
Additionally, the type'numberwill match any numeric type.nullreturns whether the given value is().formatreturns a formatted string; see string_formatting.mdprintprints a formatted string to stdout; see string_formatting.mdprintlnprints a formatted string to stdout, followed by a newline; see string_formatting.mdeprintprints a formatted string to stderr; see string_formatting.mdeprintlnprints a formatted string to stderr, followed by a newline; see string_formatting.mdpaniccauses a panic; similar in concept to a Rust panic.xorreturns the logical XOR of twoboolvaluesnotreturns the logical NOT of aboolvalue