$ bach -q '"Hello world!" out'
Hello world!
Welcome to Bach, an experimental compositional programming language. Bach is a
tool that enhances your command-line superpowers and helps you write concise,
readable, and safe one-liners for text processing and other tasks. This book
serves as an introduction and reference.
In this introductory chapter, you can read about why I created Bach, and about
its general design: Why Bach?
Alternatively, you can jump straight into trying Bach out with the sections on
Installation and First Steps.
Bach is a general-purpose programming language designed in particular for
writing “one-liners” for text processing on the command line. It is designed to
have the following properties:
- Concise. One-liners are quick to type, there is not much boiler plate.
- Readable. Bach programs consist mostly of English words. Bach makes
minimal use of special characters. You can read a Bach program and get an
idea of what it does even without being familiar with the language.
- Flowing. Bach programs are evaluated from left to right, and can be read
and understood thus.
- Orthogonal. Bach’s language elements are designed so that there
is one – and preferably only one – obvious way to do any given task. This
makes life easier for both writers and readers of Bach programs. When in
doubt, orthogonality trumps conciseness.
A Bach program is a series of functions. They are just written next to each
other with spaces in between. Each function’s output is the next function’s
input. In technical terms, a Bach program is a composition of its component
functions. Schematically, this Bach program
f g h
corresponds to this expression in a traditional applicative programming
language,
h(g(f))
and to this Unix pipeline:
f | g | h
In Bach, the concept of function composition is so central that there is no
operator for it (like | in Unix shells or . in Haskell). It’s just what you
get when you put two or more functions next to each other.
To install (or upgrade) Bach, install Go first. Then run:
go install github.com/texttheater/bach@latest
This will place the bach executable in $GOROOT/bin, thus in $HOME/go/bin
by default. You may want to place it on your $PATH.
TBD
Call Bach and pass a Bach program to it as an argument on the command line. In
our first example, our program consists of the string "Hello world!". Bach
string literals use double quotes, so surround your program with single quotes.
$ bach '"Hello world!"'
"Hello world!"
Our program creates the string "Hello world!", and Bach shows this result to
us, formatted as a string literal. If we want to print the message without the
quotes, we can compose our pogram with the function out to print it out:
$ bach '"Hello world!" out'
Hello world!
"Hello world!"
out returns its input value unchanged, so we are still shown the string with the quotes at the end.
To suppress this, we can call Bach with the -q flag:
$ bach -q '"Hello world!" out'
Hello world!
Let us now use Bach in interactive mode by using its read-eval-print loop
(REPL). To start it, we call Bach without an argument. Now we are shown the
Bach prompt:
$ bach
bach>
Let us again create the string "Hello world!":
bach> "Hello world!"
"Hello world!"
Now let us compose this program with the function codePoints, which gives us
the list of Unicode code points in the string:
bach> "Hello world!" codePoints
[72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33]
Let us add another function, len, to compute the length of the string (in
terms of code points):
bach> "Hello world!" codePoints len
12
As a final example in this introduction, let us add 1 and 2:
bach> 1 +(2)
3
Bach does not have infix operators. + is just a function that takes an input
value (1) and an argument (2). Arguments are given in parentheses. However,
for the mathematical operators, you can leave the parentheses out. Note
however, that you can't have a space between the + and the 2:
bach> 1 +2
3
To exit the REPL, press Ctrl+D.
Bach has null, boolean, number, string, array, object, and union types.
Additionally, there is the Void type (there are no values of this type) and
the Any type (all values are of this type). If you are wondering what the
output type of your program is, you can always compose it with the type
function, and you will get a string representation of the output type.
The following table demonstrates this: in the Program column, there are a
number of Bach programs, each consisting of some expression followed by the
type function. The Type column indicates the output type of the whole
program. Here, it’s always Str because that is the output type of type.
Finally, the Value column contains the strings representing the types of the
various expressions.
| Program | Type | Value | Error |
null type | Str | "Null" | |
false type | Str | "Bool" | |
true type | Str | "Bool" | |
42 type | Str | "Num" | |
0.3 type | Str | "Num" | |
"Hello world!" type | Str | "Str" | |
[] type | Str | "Arr<>" | |
{} type | Str | "Obj<Void>" | |
for Any def f Num|Str as 0 ok f type | Str | "Num|Str" | |
for Any def f Any as null ok f type | Str | "Any" | |
The simple types in Bach are Null, Bool, Num, and Str. The following
table shows some examples of programs that evaluate to a value of each type.
| Program | Type | Value | Error |
null | Null | null | |
false | Bool | false | |
true | Bool | true | |
42 | Num | 42 | |
0.3 | Num | 0.3 | |
"Hello world!" | Str | "Hello world!" | |
The Null type has a single value, null.
The are two Bool values: true and false.
Num values are IEEE754 double-precision floating-point numbers.
Str values are sequences of bytes.
The type of an array is written as Arr< … >. Between the angle brackets,
the types of the array's elements are listed. If the last element type is
followed by an ellipsis, this means that the length of the array is unknown and
that there can be 0, 1, or more elements of this type here. Array functions
such as each(...) usually return arrays with an unknown number of elements.
| Program | Type | Value | Error |
[] | Arr<> | [] | |
[1] | Arr<Num> | [1] | |
[1, 2, 3] | Arr<Num, Num, Num> | [1, 2, 3] | |
[1, 2, 3] each(+1) | Arr<Num...> | [2, 3, 4] | |
[1, "a"] | Arr<Num, Str> | [1, "a"] | |
[[1, 2], ["a", "b"]] | Arr<Arr<Num, Num>, Arr<Str, Str>> | [[1, 2], ["a", "b"]] | |
[1;[]] | Arr<Num> | [1] | |
[1, 2;[3, 4]] | Arr<Num, Num, Num, Num> | [1, 2, 3, 4] | |
[3, 4] =rest [1, 2;rest] | Arr<Num, Num, Num, Num> | [1, 2, 3, 4] | |
[1, 2;[1, 2] each(+2)] | Arr<Num, Num, Num...> | [1, 2, 3, 4] | |
for Arr<Any...> def f Arr<Any...> as id ok [] f | Arr<Any...> | [] | |
An object is a unique mapping from strings (“properties”) to values. The most
general object type is Obj<Any>. A more restrictive type can be specified for
all values, e.g., Obj<Str>. If specific properties and their types are known,
this can be part of the type too, e.g.: Obj<a: Num, b: Num, Str>. The type at
the end then describes all other values.
| Program | Type | Value | Error |
{} | Obj<Void> | {} | |
{a: 1} | Obj<a: Num, Void> | {a: 1} | |
{a: 1, b: "c"} | Obj<a: Num, b: Str, Void> | {a: 1, b: "c"} | |
for Any def f Obj<Num> as {a: 1, b: 2} ok f | Obj<Num> | {a: 1, b: 2} | |
for Any def f Obj<Any> as {a: 1, b: "c"} ok f | Obj<Any> | {a: 1, b: "c"} | |
for Any def f Obj<a: Num, b: Str, Any> as {a: 1, b: "c", d: false} ok f | Obj<a: Num, b: Str, Any> | {a: 1, b: "c", d: false} | |
A union type indicates that a value could be of any of two or more types.
| Program | Type | Value | Error |
[1] +["a"] | Arr<Num|Str...> | [1, "a"] | |
[1] +["a"] get(0) | Num|Str | 1 | |
In Bach, every expression denotes a function. For example:
2 denotes a function that takes any input, ignores it, and returns the
number 2.
join(",") denotes a function that takes a list of strings and concatenates
them, using the comma as a separator.
+(2) denotes a function that takes a number as input, adds 2 to it, and
returns the result.
+2 is the same, with syntactic sugar applied.
if %3 ==0 then "multiple of 3" else "not a multiple of 3" ok denotes a
function that takes a number and returns different strings depending on
whether the number is divisible by 3.
+2 *3 denotes a composition of two functions, which is, again, a function
(one that takes a number, adds 2 to it, then multiplies the result by 3).
This chapter describes the different kinds of expressions in more detail.
Nonnegative real numbers can be written in the decimal system: as integers,
with a decimal point, or in exponential notation.
| Program | Type | Value | Error |
123 | Num | 123 | |
1.23 | Num | 1.23 | |
01.23 | Num | 1.23 | |
.23 | Num | 0.23 | |
1. | Num | 1 | |
1.23e2 | Num | 123 | |
123E2 | Num | 12300 | |
123E+2 | Num | 12300 | |
1e-1 | Num | 0.1 | |
.1e0 | Num | 0.1 | |
0010e-2 | Num | 0.1 | |
0e+5 | Num | 0 | |
There are no literals for negative, infinity, or NaN values. But they can be created using the builtin -, inf, and nan funcers. For details, see Math Funcers.
| Program | Type | Value | Error |
-1 | Num | -1 | |
-0010e-2 | Num | -0.1 | |
-0 | Num | -0 | |
inf | Num | inf | |
-inf | Num | -inf | |
nan | Num | nan | |
Strings are sequences of bytes. String literals are delimited by double quotes. Characters inside generally represent the byte sequence that is their UTF-8 encoding. For example:
a represents the byte sequence 61 (hexadecimal notation; UTF-8 encoding of Latin Small Letter A)
~ represents the byte sequence 7E (UTF-8 encoding of Tilde)
abc represents the byte sequence 61 62 63
日本語 represents the byte sequence E6 97 A5 E6 9C AC E8 AA 9E
There are, however, the following exceptions:
\a represents the byte sequence 07 (UTF-8 encoding of Bell character)
\b represents the byte sequence 08 (UTF-8 encoding of Backspace)
\f represents the byte sequence 0C (UTF-8 encoding of Form feed)
\n represents the byte sequence 0A (UTF-8 encoding of Line feed)
\r represents the byte sequence 0D (UTF-8 encoding of Carriage return)
\t represents the byte sequence 09 (UTF-8 encoding of Horizontal tab)
\v represents the byte sequence 09 (UTF-8 encoding of Vertical tab)
\\ represents the byte sequence 5C (UTF-8 encoding of Backslash)
\" represents the byte sequence 22 (UTF-8 encoding of Quotation mark)
\ followed by three octal digits in the range from 000 to 3ff (inclusive) represents the corresponding byte
\x followed by two hexadecimal digits represents the corresponding byte
\u followed by four hexadecimal digits represents the UTF-8 encoding of the corresponding code point, if defined
\U followed by eight hexadecimal digits represents the UTF-8 encoding of the corresponding code point, if defined
{{ represents the byte sequence 7B (UTF-8 encoding of Left curly bracket)
}} represents the byte sequence 7D (UTF-8 encoding of Right curly bracket)
{, followed by a Bach expression, followed by }, represents the UTF-8 encoding of what the expression evaluates to
- Other uses of
\, ", {, or } inside the delimiting quotes are invalid, as is the line feed character
| Program | Type | Value | Error |
"a" | Str | "a" | |
"\a" | Str | "\a" | |
"\"\\a\"" | Str | "\"\\a\"" | |
"\141" | Str | "a" | |
"\x61" | Str | "a" | |
"\u65e5\u672c\u8a9e" | Str | "日本語" | |
"\U000065e5\U0000672c\U00008a9e" | Str | "日本語" | |
"{{}}" | Str | "{{}}" | |
"1 + 1 = {1 +1}" | Str | "1 + 1 = 2" | |
"{ {a: 1 +1} }" | Str | "{{a: 2}}" | |
Array literals are delimited by square brackets. Inside, a comma-separated
sequence of Bach expressions represents the elements. An expression
representing an array can be appended as a suffix using a semicolon.
| Program | Type | Value | Error |
[] | Arr<> | [] | |
[1] | Arr<Num> | [1] | |
[1, 2, 3] | Arr<Num, Num, Num> | [1, 2, 3] | |
[1, "a"] | Arr<Num, Str> | [1, "a"] | |
[[1, 2], ["a", "b"]] | Arr<Arr<Num, Num>, Arr<Str, Str>> | [[1, 2], ["a", "b"]] | |
[1 +1] | Arr<Num> | [2] | |
[1;[]] | Arr<Num> | [1] | |
[1, 2;[3, 4]] | Arr<Num, Num, Num, Num> | [1, 2, 3, 4] | |
[3, 4] =rest [1, 2;rest] | Arr<Num, Num, Num, Num> | [1, 2, 3, 4] | |
[1, 2;[1, 2] each(+2)] | Arr<Num, Num, Num...> | [1, 2, 3, 4] | |
Obj literals are delimited by curly braces. Inside, each elements consists of a
key, followed by a colon, followed by a value. Elements are separated by
commas. The order of elements does not matter. Keys are always strings, but
they can be written as identifiers or number literals; they are converted to
strings automatically.
| Program | Type | Value | Error |
{} | Obj<Void> | {} | |
{"a": 1} | Obj<a: Num, Void> | {a: 1} | |
{a: 1} | Obj<a: Num, Void> | {a: 1} | |
{1: "a"} | Obj<1: Str, Void> | {1: "a"} | |
{"1": "a"} | Obj<1: Str, Void> | {1: "a"} | |
{a: 1, b: "c"} | Obj<a: Num, b: Str, Void> | {a: 1, b: "c"} | |
{b: 1 +1} | Obj<b: Num, Void> | {b: 2} | |
Every Bach expression denotes a function that maps the input to the output.
Funcers are generalizations of functions: in addition to the input, they can
have arguments on which their output depends. But there are also 0-argument
funcers. A number of funcers are built-in to Bach (see Builtin Funcer
Reference), and you can define your own (see
Funcer Definition Expressions). Arguments
go between parentheses. No space is allowed between the funcer name and the
opening parenthesis. For example, len returns the length of an array, join
joins a list of strings with a given glue string, and +(1) adds 1 to a
number.
| Program | Type | Value | Error |
[1, 2, 3] len | Num | 3 | |
["a", "b", "c"] join("+") | Str | "a+b+c" | |
1 +(1) | Num | 2 | |
To make arithmetic expressions etc. less noisy, when calling the funcers +,
-, *, /, %, <, >, ==, <=, >=, ** with one argument that is
a single literal or funcer call, the parentheses can be omitted. Note that
there are no operator precedence rules in Bach – evaluation is from left to
right. To change precedence, put arguments in parentheses by not omitting the
parentheses.
| Program | Type | Value | Error |
1 +1 | Num | 2 | |
2 +3 *5 | Num | 25 | |
2 +(3 *5) | Num | 17 | |
With a single argument that is a string, regexp, array, or object literal,
parentheses may also be omitted.
| Program | Type | Value | Error |
["a", "b", "c"] join"+" | Str | "a+b+c" | |
Funcers are selected according to the input type, the name, and the number of
arguments. For example, there are two builtin 1-arg funcers named +: one for
Num inputs (which expects the first argument to evaluate to a Num and
returns the sum) and one for Str inputs (which expectes the first argument to
evaluate to a Str and returns the concatenation).
| Program | Type | Value | Error |
1 +2 | Num | 3 | |
"a" +"b" | Str | "ab" | |
"a" +2 | | | Type error Code: ArgHasWrongOutputType Message: An argument has the wrong output type. Want type: Str Got type: Num Arg #: 1
|
In most cases, funcers are defined so that their argument can be any type of
expression as long as it has the right input and output type. But it is also
possible for funcer parameters to require a partially applied funcer call with
n remaining arguments to be filled in by the funcer. To call a funcer with
m+n arguments to fill a parameter that requires n open arguments, pass only
the first m arguments. As an example with m=0 and n=1, the builtin funcer
sort takes a comparison funcer with an open argument as its sole argument.
Here, we use the number comparison funcer >:
| Program | Type | Value | Error |
[7, 3, 2, 5] sort(>) | Arr<Num...> | [7, 5, 3, 2] | |
To define a funcer, use a funcer definition expression. It has the form for I def N(P1, P2, ..., Pn) O as B ok, where I is the funcer’s input type,
N is its name (must start with a lower-case letter), P1, P2, ..., Pn are
n or more parameters (if n=0, leave out the parentheses), O is the
funcer’s output type, and B is its body. When called, the funcer will
evaluate like B, with parameters bound to the arguments passed at call time.
Here are some examples of defining and then calling funcers without parameters:
| Program | Type | Value | Error |
for Num def plusOne Num as +1 ok 1 plusOne | Num | 2 | |
for Num def plusOne Num as +1 ok 1 plusOne plusOne | Num | 3 | |
In addition to the input, funcers can take arguments, which are functions
that can be called in the funcer body to use their outputs. Parameters
specify what kind of arguments are expected. In the simplest case, a parameter
is simply a name and a type. This means that inside the funcer body, the given
function can be called with any input, by the given name, and will return a
value of the given type.
| Program | Type | Value | Error |
for Any def f(x Num) Num as x ok f(1) | Num | 1 | |
for Num def plus(b Num) Num as +b ok 1 plus(1) | Num | 2 | |
for Any def f(a Num, b Num) Arr<Num, Num> as [a, b] ok f(2, 3) | Arr<Num, Num> | [2, 3] | |
In more complex cases, parameters can restrict arguments to be partially
applied funcer calls. In such cases, the syntax for a parameter is for I N(P1, P2, ..., Pn) O. This means that inside the funcer body, for inputs of type
I, the given funcer will be available to call by the name N with arguments
as specified by the parameters P1, P2, ..., Pn (as before, drop the
parentheses if n=0) and will return a value of type O. Here, P1, P2, ..., Pn do not contain names.
| Program | Type | Value | Error |
for Num def apply(for Num f Num) Num as f ok 1 apply(+1) | Num | 2 | |
for Num def apply(for Num f Num) Num as f ok 2 =n apply(+n) | Num | 4 | |
for Num def connectSelf(for Num f(for Any Num) Num) Num as =x f(x) ok 1 connectSelf(+) | Num | 2 | |
for Num def connectSelf(for Num f(for Any Num) Num) Num as =x f(x) ok 3 connectSelf(*) | Num | 9 | |
for Num def connectSelf(for Num f(Num) Num) Num as =x f(x) ok 1 connectSelf(+) | Num | 2 | |
Funcer definition expressions can use type variables. They are written between
angle brackets and start with an upper-case letter. When applying a funcer to
an input expression, type variables are bound from left to right.
| Program | Type | Value | Error |
for <A> def apply(for <A> f <B>) <B> as f ok 1 apply(+1) | Num | 2 | |
for <A>|Null def myMust <A> as is Null then fatal else id ok ok 123 myMust | Num | 123 | |
for <A>|Null def myMust <A> as is Null then fatal else id ok ok "abc" myMust | Str | "abc" | |
for <A>|Null def myMust <A> as is Null then fatal else id ok ok null myMust | <A> | | Value error Code: UnexpectedValue Message: Component got an unexpected input value. Got value: null
|
for <A>|Null def myMust <A> as is <A> then id else fatal ok ok null myMust | Null | null | |
for <A Obj<a: Num, Any>> def f <A> as id ok {a: 1} f | Obj<a: Num, Void> | {a: 1} | |
Type variables can be given upper bounds, they are then constrained to match
subtypes of the given upper bound type.
| Program | Type | Value | Error |
for Any def f(for Any g <A Arr<Any...>>) <A> as g ok f([1, "a"]) | Arr<Num, Str> | [1, "a"] | |
for Any def f(for Any g <A Arr<Any...>>) <A> as g ok f("a") | | | Type error Code: ArgHasWrongOutputType Message: An argument has the wrong output type. Want type: <A Arr<Any...>> Got type: Str Arg #: 1
|
Funcers are looked up by input type, name, and number of parameters. So you
can, for example, define two funcers with the same name but different input
types, and use both:
| Program | Type | Value | Error |
for Num def f Num as +2 ok for Str def f Str as +"2" ok | Null | null | |
for Num def f Num as +2 ok for Str def f Str as +"2" ok 2 f | Num | 4 | |
for Num def f Num as +2 ok for Str def f Str as +"2" ok "a" f | Str | "a2" | |
Funcers cannot be overloaded with respect to the parameters themselves. Thus,
calling a funcer on the wrong input or with the wrong number of arguments
results in a NoSuchFuncer error. Calling it with the wrong kinds of
arguments, by contrast, leads to an ArgHasWrongOutputType error.
| Program | Type | Value | Error |
for Num def f Num as *2 ok f | | | Type error Code: NoSuchFuncer Message: no such funcer Input type: Null Name: f # params: 0
|
for <A Obj<a: Num>> def f <A> as id ok {} f | | | Type error Code: NoSuchFuncer Message: no such funcer Input type: Obj<Void> Name: f # params: 0
|
for Num def f Num as *2 ok 2 f(2) | | | Type error Code: NoSuchFuncer Message: no such funcer Input type: Num Name: f # params: 1
|
for Str def f Obj<> as {} ok "abc" reFindAll(f) | | | Type error Code: ArgHasWrongOutputType Message: An argument has the wrong output type. Want type: <A Null|Obj<0: Str, start: Num, Any>> Got type: Obj<Any> Arg #: 1
|
Funcers have access to the variables defined before, but not after the definition.
| Program | Type | Value | Error |
1 =a for Any def f Num as a ok f | Num | 1 | |
1 =a for Any def f Num as a ok 2 =a f | Num | 1 | |
1 =a for Any def f Num as 2 =a a ok f | Num | 2 | |
TBD
An assignment expressions consists of the character = immediately followed by
a pattern. It binds the names in the pattern to the corresponding parts of its
input value so you can reuse them later in the program. A pattern can be a
name, an array literal where the elements are patterns, or an object literal
where the values are patterns.
| Program | Type | Value | Error |
1 +1 =a 3 *2 +a | Num | 8 | |
1 +1 ==2 =p 1 +1 ==1 =q p ==q not | Bool | true | |
[1, 2, 3] =[a, b, c] a | Num | 1 | |
[1, 2, 3] =[a;r] r | Arr<Num, Num> | [2, 3] | |
{a: 1, b: 2, c: 3} ={a: d, b: e, c: f} d | Num | 1 | |
{a: 1, b: 2, c: 3} ={a: d, b: e, c: f} e | Num | 2 | |
{a: 1, b: 2, c: 3} ={a: d, b: e, c: f} f | Num | 3 | |
for Num def cube Num as =n *n *n ok 3 cube | Num | 27 | |
An “impossible match” error occurs if the pattern cannot match any values of
the input type, e.g., if an array pattern has a different length from the
input, or matching objects with array patterns, or vice versa, or if an object
pattern contains keys that the input doesn’t.
| Program | Type | Value | Error |
[1, 2, 3] =[a, b] | | | Type error Code: ImpossibleMatch Message: Impossible match. The pattern will never match the input type.
|
{a: 1, b: 2, c: 3} =[a, b] | | | Type error Code: ImpossibleMatch Message: Impossible match. The pattern will never match the input type.
|
{a: 1, b: 2, c: 3} ={g: h} | | | Type error Code: ImpossibleMatch Message: Impossible match. The pattern will never match the input type.
|
A “nonexhaustive match” error occurs if the pattern can match some but not all
values of the input type, e.g., if a variable-length array type is matched as
fixed-length, or if an object type is matched against a key it might or might
not contain.
| Program | Type | Value | Error |
for Arr<Num...> def f Num as =[a, b] a ok | | | Type error Code: NonExhaustiveMatch Message: Match is not exhaustive. Consider adding elis clauses and/or an else clause.
|
for Obj<a: Num, Num> def f Num as ={a: a, b: b} a +b ok | | | Type error Code: NonExhaustiveMatch Message: Match is not exhaustive. Consider adding elis clauses and/or an else clause.
|
for Obj<a: Num, Num> def f Num as ={b: b} b ok | | | Type error Code: NonExhaustiveMatch Message: Match is not exhaustive. Consider adding elis clauses and/or an else clause.
|
Conditional expressions come in three flavors:
if A then B else C ok (checks a boolean condition A)
is A then B else C ok (matches the input against the pattern A)
is A with D then B else C ok (matches the input against the pattern A and checks the boolean condition D)
A pattern can be a name, a type optionally combined with a name, an array
literal where the elements are patterns, or an object literal where the values
are patterns.
To check for additional alternative conditions, add alternative clauses before
the final else clause. Alternative clauses, too, come in three flavors. All
can be freely combined:
elis A then B (checks a boolean condition A)
elis A then B (matches the input against the pattern A)
elis A with D then B (matches the input against the pattern A and checks the boolean condition D)
Names bound by a pattern can be used inside the following then clause.
| Program | Type | Value | Error |
if true then 2 else 3 ok | Num | 2 | |
for Num def heart Bool as if <3 then true else false ok ok 2 heart | Bool | true | |
for Num def heart Bool as if <3 then true else false ok ok 4 heart | Bool | false | |
for Num def expand Num as if <0 then -1 elif >0 then +1 else 0 ok ok 0 -1 expand | Num | -2 | |
for Num def expand Num as if <0 then -1 elif >0 then +1 else 0 ok ok 1 expand | Num | 2 | |
for Num def expand Num as if <0 then -1 elif >0 then +1 else 0 ok ok 0 expand | Num | 0 | |
In Bach, a predicate is a function that for a given input I returns either
{yes: I} or {no: I}. This is often useful, e.g., for checking the members
of an array for some condition. Thus, several builtin array
funcers accept them as arguments.
Bach provides a special syntax for making such conditions easy to write. A
predicate expression is like a conditional expression, except it consists
only of the first part, that is, the if ... clause, or the is ... clause,
or the is ... with ... clause. The then {yes: id} else {no: id} part is
filled in automatically.
| Program | Type | Value | Error |
is Null | | | Type error Code: UnreachableElseClause Message: The else clause is unreachable because the match is already exhaustive.
|
2 is Num with >3 | Obj<yes: Num>|Obj<no: Num> | {no: 2} | |
4 is Num with >3 | Obj<yes: Num>|Obj<no: Num> | {yes: 4} | |
2 if >3 | Obj<yes: Num>|Obj<no: Num> | {no: 2} | |
4 if >3 | Obj<yes: Num>|Obj<no: Num> | {yes: 4} | |
for Any def f Num|Str as 2 ok f is Num _ | Obj<yes: Num>|Obj<no: Str> | {yes: 2} | |
[1, 2, 3] each(if ==1 then "a" elif ==2 then "b" else "c" ok) | Arr<Str...> | ["a", "b", "c"] | |
[1, 2, 3] each(if ==1 then "a" elif ==2 then "b" else "c") | | | Syntax error Code: Syntax Message: syntax error
|
A getter expressions consists of the @ character followed by an
object property name or an array index. It
retrieves the value of the property or the element at the index for an input
object or array. The input type must guarantee the existence of the
property/index, otherwise a type error is raised. If the input type cannot
guarantee the existence of the property/index, use the get funcer for
objects or for arrays.
| Program | Type | Value | Error |
{a: 1, b: 2} @a | Num | 1 | |
{a: 1, b: 2} @b | Num | 2 | |
{a: 1, b: 2} @c | | | Type error Code: NoSuchProperty Message: The object does not have this property. Want type: Obj<c: Any, Any> Got type: Obj<a: Num, b: Num, Void>
|
{0: "a"} @0 | Str | "a" | |
["a", "b", "c"] @0 | Str | "a" | |
["a", "b", "c"] @1 | Str | "b" | |
["a", "b", "c"] @2 | Str | "c" | |
["a", "b", "c"] @3 | | | Type error Code: NoSuchIndex Message: Array is not long enough.
|
["a", "b", "c"] @-1 | | | Type error Code: BadIndex Message: Index must be a nonnegative integer.
|
["a", "b", "c"] @1.5 | | | Type error Code: BadIndex Message: Index must be a nonnegative integer.
|
"abc" @1 | | | Type error Code: NoGetterAllowed Message: A getter expression cannot be applied to this type.
|
24 @1 | | | Type error Code: NoGetterAllowed Message: A getter expression cannot be applied to this type.
|
for Any def f Arr<Any...> as [] ok f @1 | | | Type error Code: NoGetterAllowed Message: A getter expression cannot be applied to this type.
|
In Bach, a regexp is a function that takes a Str and returns either null
or an object with at least a property start with type Num, and 0 with
type Str.
Regexp expressions denote such functions. They have the syntax of re2 regular
expressions, except for \C,
delimited by ~. They search the input string for a substring that matches the
regular expression. If none can be found, null is returned. Otherwise, an
object is returned with start indicating the offset (in codepoints) of the
first occurrence, and 0 containing the matched substring. If the regexp
contains capturing groups, the substrings matched by the groups are included as
additional properties – by group number and, in the case of named groups,
additionally by name.
| Program | Type | Value | Error |
"abccd" ~b(?P<cs>c*)d~ | Null|Obj<start: Num, 0: Str, 1: Null|Str, cs: Null|Str, Void> | {start: 1, 0: "bccd", 1: "cc", cs: "cc"} | |
"def" ~^b(?P<cs>c*)d~ | Null|Obj<start: Num, 0: Str, 1: Null|Str, cs: Null|Str, Void> | null | |
"abccd" ~^b(?P<cs>*)d~ | | | Syntax error Code: BadRegexp Message: error parsing regexp
|
Regexp expressions can be used with regexp funcers for
greater effect.
A composition expression consists of two other expressions, separated by
whitespace. The output of the first expression is then used as the input to the
other. By adding another expression at the end, you again create a larger
composition expression, and so on.
| Program | Type | Value | Error |
"abc" | Str | "abc" | |
"abc" codePoints | Arr<Num...> | [97, 98, 99] | |
"abc" codePoints len | Num | 3 | |
"abc" codePoints len *3 | Num | 9 | |
Note that when you compose with expressions that ignore their input, such as
literals, only the last one has any effect on the output.
| Program | Type | Value | Error |
1 | Num | 1 | |
1 2 | Num | 2 | |
1 2 3.5 | Num | 3.5 | |
Returns the null value.
| Type | Value |
| Input | Any | any value (is ignored) |
| Output | Null | the null value (the only value of this type) |
| Program | Type | Value | Error |
null | Null | null | |
1 null | Null | null | |
Groups lines into blocks separated by empty lines.
| Type | Value |
| Input | Arr<Str...> | an array of consecutive lines |
| Output | Arr<Arr<Str...>...> | an array of arrays of lines, each representing a block |
Each empty line in the input marks the end of a block. Blocks can be empty. The empty lines themselves are not included.
| Program | Type | Value | Error |
["a", "b", "", "c", "d", "e", "f", ""] blocks | Arr<Arr<Str...>...> | [["a", "b"], ["c", "d", "e", "f"]] | |
["a", ""] blocks | Arr<Arr<Str...>...> | [["a"]] | |
["a"] blocks | Arr<Arr<Str...>...> | [["a"]] | |
["", "a"] blocks | Arr<Arr<Str...>...> | [[], ["a"]] | |
["a", "", "", "", "b"] blocks | Arr<Arr<Str...>...> | [["a"], [], [], ["b"]] | |
Writes to STDERR.
| Type | Value |
| Input | <A Any> | any value |
| Output | <A Any> | the same value |
Identity function with the side effect of writing a string representation of the value to STDERR, followed by a line break.
Writes to STDERR with a custom line end.
| Type | Value |
| Input | <A Any> | any value |
| end (param #1) | Str | the line end to use |
| Output | <A Any> | the same value |
Identity function with the side effect of writing a string representation of the value to STDERR, followed by a the specified line end.
Reads from STDIN.
| Type | Value |
| Input | Any | any value (is ignored) |
| Output | Reader | a Reader representing STDIN |
Reads JSON values from a stream
| Type | Value |
| Input | Reader | a Reader |
| Output | Arr<Any...> | array of data structures as they appear in the stream |
Reads a stream line-by-line
| Type | Value |
| Input | Reader | a Reader |
| Output | Arr<Str...> | an array of lines, without the line-break character |
| Program | Type | Value | Error |
"abc\nde\n\nf" reader lines | Arr<Str...> | ["abc", "de", "", "f"] | |
Writes to STDOUT.
| Type | Value |
| Input | <A Any> | any value |
| Output | <A Any> | the same value |
Identity function with the side effect of writing a string representation of the value to STDERR, followed by a line break.
Writes to STDOUT with a custom line end.
| Type | Value |
| Input | <A Any> | any value |
| end (param #1) | Str | |
| Output | <A Any> | the same value |
Identity function with the side effect of writing a string representation of the value to STDOUT, followed by a line break.
Creates a Reader from a Str.
| Type | Value |
| Input | Str | a string |
| Output | Reader | a Reader from which the input can be read |
Returns the value representing logical truth.
| Type | Value |
| Input | Any | any value (is ignored) |
| Output | Bool | the unique value representing logical truth |
| Program | Type | Value | Error |
true | Bool | true | |
Returns the value representing logical falsehood.
| Type | Value |
| Input | Any | any value (is ignored) |
| Output | Bool | the unique value representing logical falsehood |
| Program | Type | Value | Error |
false | Bool | false | |
Computes logical conjunction.
| Type | Value |
| Input | Bool | a boolean value |
| q (param #1) | Bool | another boolean value |
| Output | Bool | true if both the input and q are true, false otherwise |
| Program | Type | Value | Error |
false and(false) | Bool | false | |
false and(true) | Bool | false | |
true and(false) | Bool | false | |
true and(true) | Bool | true | |
Computes logical disjunction.
| Type | Value |
| Input | Bool | a boolean value |
| q (param #1) | Bool | another boolean value |
| Output | Bool | true if at least one of the input and q is true, false otherwise |
| Program | Type | Value | Error |
false or(false) | Bool | false | |
false or(true) | Bool | true | |
true or(false) | Bool | true | |
true or(true) | Bool | true | |
Computes logical negation.
| Type | Value |
| Input | Bool | a boolean value |
| Output | Bool | true if the input is false, and false if the input is true |
| Program | Type | Value | Error |
false not | Bool | true | |
true not | Bool | false | |
1 +1 ==2 and(2 +2 ==5 not) | Bool | true | |
Checks equality of boolean values.
| Type | Value |
| Input | Bool | a boolean value |
| q (param #1) | Bool | another boolean value |
| Output | Bool | true if the input and q are identical, false otherwise |
| Program | Type | Value | Error |
false ==false | Bool | true | |
false ==true | Bool | false | |
true ==false | Bool | false | |
true ==true | Bool | true | |
Adds two numbers.
| Type | Value |
| Input | Num | the first summand |
| b (param #1) | Num | the second summand |
| Output | Num | the sum |
| Program | Type | Value | Error |
1 +1 | Num | 2 | |
1 +2 *3 | Num | 9 | |
1 +(2 *3) | Num | 7 | |
Subtracts a number from another.
| Type | Value |
| Input | Num | the minuend |
| b (param #1) | Num | the subtrahend |
| Output | Num | the difference |
| Program | Type | Value | Error |
5 -3 | Num | 2 | |
Returns the additive inverse of a number.
| Type | Value |
| Input | Any | any value (is ignored) |
| n (param #1) | Num | a number |
| Output | Num | the additive inverse (opposite number) of n |
| Program | Type | Value | Error |
-1 | Num | -1 | |
-(-2.0) | Num | 2 | |
-inf | Num | -inf | |
-nan | Num | nan | |
Multiplies two numbers.
| Type | Value |
| Input | Num | the first factor |
| b (param #1) | Num | the second factor |
| Output | Num | the product |
| Program | Type | Value | Error |
2 *3 | Num | 6 | |
Divides a number by another.
| Type | Value |
| Input | Num | the dividend |
| b (param #1) | Num | the divisor |
| Output | Num | the quotient |
| Program | Type | Value | Error |
3 /2 | Num | 1.5 | |
1 /0 | Num | inf | |
Remainder
| Type | Value |
| Input | Num | the dividend |
| b (param #1) | Num | the divisor |
| Output | Num | the remainder of integer division (rounded towards zero) |
| Program | Type | Value | Error |
3 %2 | Num | 1 | |
-8.5 %3 | Num | -2.5 | |
Less than
| Type | Value |
| Input | Num | a number |
| b (param #1) | Num | another number |
| Output | Bool | true iff the input is smaller than b |
| Program | Type | Value | Error |
2 <1 | Bool | false | |
-inf <inf | Bool | true | |
0 <0 | Bool | false | |
Greater than
| Type | Value |
| Input | Num | a number |
| b (param #1) | Num | another number |
| Output | Bool | true iff the input is greater than b |
| Program | Type | Value | Error |
2 >1 | Bool | true | |
-inf >inf | Bool | false | |
0 >0 | Bool | false | |
Less than or equal to
| Type | Value |
| Input | Num | a number |
| b (param #1) | Num | another number |
| Output | Bool | true iff the input is less than or equal to b |
| Program | Type | Value | Error |
2 <=1 | Bool | false | |
-inf <=inf | Bool | true | |
0 <=0 | Bool | true | |
Greater than or equal to
| Type | Value |
| Input | Num | a number |
| b (param #1) | Num | another number |
| Output | Bool | true iff the input is greater than or equal to b |
| Program | Type | Value | Error |
2 >=1 | Bool | true | |
-inf >=inf | Bool | false | |
0 >=0 | Bool | true | |
Computes the sum of several numbers.
| Type | Value |
| Input | Arr<Num...> | an array of numbers |
| Output | Num | their sum |
| Program | Type | Value | Error |
[1, 2, 3, 4] sum | Num | 10 | |
[] sum | Num | 0 | |
Computes the median of several numbers.
| Type | Value |
| Input | Arr<Num...> | an array of numbers |
| Output | Num | their median |
| Program | Type | Value | Error |
[2, 3, 7] median | Num | 3 | |
[2, 3, 5, 7] median | Num | 4 | |
[1.25] median | Num | 1.25 | |
[] median | Num | nan | |
Computes the arithmetic mean (average) of several numbers.
| Type | Value |
| Input | Arr<Num...> | an array of numbers |
| Output | Num | their mean |
| Program | Type | Value | Error |
[2, 3, 5, 7] mean | Num | 4.25 | |
[1.25] mean | Num | 1.25 | |
[] mean | Num | nan | |
Returns infinity.
| Type | Value |
| Input | Any | any value (is ignored) |
| Output | Num | the special number value representing positive infinity |
| Program | Type | Value | Error |
inf | Num | inf | |
-inf | Num | -inf | |
inf +inf | Num | inf | |
inf -inf | Num | nan | |
Returns NaN.
| Type | Value |
| Input | Any | any value (is ignored) |
| Output | Num | the special number value representing “not a number” |
| Program | Type | Value | Error |
nan | Num | nan | |
nan ==2 | Bool | false | |
nan ==nan | Bool | false | |
-nan | Num | nan | |
Checks whether a number is finite.
| Type | Value |
| Input | Num | a number |
| Output | Bool | true unless the input is positive or negative infinity |
| Program | Type | Value | Error |
1024 isFinite | Bool | true | |
-1024 isFinite | Bool | true | |
inf isFinite | Bool | false | |
-inf isFinite | Bool | false | |
nan isFinite | Bool | true | |
Checks whether a number is NaN.
| Type | Value |
| Input | Num | a number |
| Output | Bool | true iff the input is NaN |
| Program | Type | Value | Error |
1024 isNaN | Bool | false | |
nan isNaN | Bool | true | |
Returns the floating point epsilon.
| Type | Value |
| Input | Any | any value (is ignored) |
| Output | Num | the difference between 1 and the smallest floating point number greater than 1 |
| Program | Type | Value | Error |
epsilon | Num | 2.220446049250313e-16 | |
Returns the largest safe integer.
| Type | Value |
| Input | Any | any value (is ignored) |
| Output | Num | the largest integer that can be represented as an IEEE-754 double precision number and cannot be the result of rounding another number to fit IEEE-754 |
| Program | Type | Value | Error |
largestSafeInteger +0 | Num | 9007199254740991 | |
largestSafeInteger +1 | Num | 9007199254740992 | |
largestSafeInteger +2 | Num | 9007199254740992 | |
Returns the largest representable number.
| Type | Value |
| Input | Any | any value (is ignored) |
| Output | Num | the largest number representable as an IEEE-754 double precision number |
| Program | Type | Value | Error |
largestNum | Num | 1.7976931348623157e+308 | |
largestNum +largestNum | Num | inf | |
Returns the smallest safe integer.
| Type | Value |
| Input | Any | any value (is ignored) |
| Output | Num | the smallest integer that can be represented as an IEEE-754 double precision number and cannot be the result of rounding another integer to fit the IEEE-754 double precision representation |
| Program | Type | Value | Error |
smallestSafeInteger -0 | Num | -9007199254740991 | |
smallestSafeInteger -1 | Num | -9007199254740992 | |
smallestSafeInteger -2 | Num | -9007199254740992 | |
Returns the smallest representable positive number.
| Type | Value |
| Input | Any | any value (is ignored) |
| Output | Num | the smallest representable positive number |
| Program | Type | Value | Error |
smallestPositiveNum | Num | 5e-324 | |
Checks whether a number is integer.
| Type | Value |
| Input | Num | a number |
| Output | Bool | true iff the input represents a whole number |
| Program | Type | Value | Error |
1.0 isInteger | Bool | true | |
1.1 isInteger | Bool | false | |
Checks whether a number is a safe integer.
| Type | Value |
| Input | Num | a number |
| Output | Bool | true iff the input is an integer and cannot be the result of rounding another integer to fit the IEEE-754 double precision representation |
| Program | Type | Value | Error |
-9007199254740992 isSafeInteger | Bool | false | |
-9007199254740991 isSafeInteger | Bool | true | |
0 isSafeInteger | Bool | true | |
0.1 isSafeInteger | Bool | false | |
9007199254740991 isSafeInteger | Bool | true | |
9007199254740992 isSafeInteger | Bool | false | |
Converts an integer to a specified base.
| Type | Value |
| Input | Num | an integer |
| base (param #1) | Num | an integer between 2 and 36 (inclusive) |
| Output | Str | a string representation of the input in the specified base |
| Program | Type | Value | Error |
233 toBase(16) | Str | "e9" | |
11 toBase(16) | Str | "b" | |
Converts a number to exponential notation.
| Type | Value |
| Input | Num | a number |
| Output | Str | a string representation of the input in exponential notation with 6 digits after the decimal point |
| Program | Type | Value | Error |
77.1234 toExponential | Str | "7.712340e+01" | |
77 toExponential | Str | "7.700000e+01" | |
Converts a number to exponential notation.
| Type | Value |
| Input | Num | a number |
| precision (param #1) | Num | the number of digits after the decimal point |
| Output | Str | a string representation of the input in exponential notation with the specified number of digits after the decimal point |
| Program | Type | Value | Error |
77.1234 toExponential(4) | Str | "7.7123e+01" | |
77.1234 toExponential(2) | Str | "7.71e+01" | |
Converts a number to fixed-point notation.
| Type | Value |
| Input | Num | a number |
| precision (param #1) | Num | the number of digits after the decimal point |
| Output | Str | a rounded string representation of the input with the specified number of digits after the decimal point |
| Program | Type | Value | Error |
123.456 toFixed(2) | Str | "123.46" | |
0.004 toFixed(2) | Str | "0.00" | |
1.23e+5 toFixed(2) | Str | "123000.00" | |
Returns Euler's number.
| Type | Value |
| Input | Any | any value (is ignored) |
| Output | Num | an approximation of Euler's number |
| Program | Type | Value | Error |
e | Num | 2.718281828459045 | |
Returns the natural logarithm of 2.
| Type | Value |
| Input | Any | any value (is ignored) |
| Output | Num | the approximate natural logarithm of 2 |
| Program | Type | Value | Error |
ln2 | Num | 0.6931471805599453 | |
Returns the natural logarithm of 10.
| Type | Value |
| Input | Any | any value (is ignored) |
| Output | Num | the approximate natural logarithm of 10 |
| Program | Type | Value | Error |
ln10 | Num | 2.302585092994046 | |
Returns the base-2 logarithm of e
| Type | Value |
| Input | Any | any value (is ignored) |
| Output | Num | the approximate base-2 logarithm of Euler's number |
| Program | Type | Value | Error |
log2e | Num | 1.4426950408889634 | |
Returns the base-10 logarithm of e
| Type | Value |
| Input | Any | any value (is ignored) |
| Output | Num | the approximate base-10 logarithm of Euler's number |
| Program | Type | Value | Error |
log10e | Num | 0.4342944819032518 | |
Returns pi.
| Type | Value |
| Input | Any | any value (is ignored) |
| Output | Num | an approximation of pi |
| Program | Type | Value | Error |
pi | Num | 3.141592653589793 | |
for Num def radiusToCircumference Num as *2 *pi ok 10 radiusToCircumference | Num | 62.83185307179586 | |
Returns the square root of 1/2.
| Type | Value |
| Input | Any | any value (is ignored) |
| Output | Num | the approximate square root of 1/2 |
| Program | Type | Value | Error |
sqrt1_2 | Num | 0.7071067811865476 | |
Returns the square root of 2.
| Type | Value |
| Input | Any | any value (is ignored) |
| Output | Num | the approximate square root of 2 |
| Program | Type | Value | Error |
sqrt2 | Num | 1.4142135623730951 | |
Computes the absolute value.
| Type | Value |
| Input | Num | a number |
| Output | Num | the absolute value of the input |
| Program | Type | Value | Error |
3 -5 abs | Num | 2 | |
5 -3 abs | Num | 2 | |
1.23456 -7.89012 abs | Num | 6.6555599999999995 | |
Computes the inverse cosine.
| Type | Value |
| Input | Num | a number in the interval [-1, 1] |
| Output | Num | the inverse cosine (in radians) of the input, or nan if the input is invalid |
| Program | Type | Value | Error |
-2 acos | Num | nan | |
-1 acos | Num | 3.141592653589793 | |
0 acos | Num | 1.5707963267948966 | |
1 acos | Num | 0 | |
1.1 acos | Num | nan | |
Computes the inverse hyperbolic cosine.
| Type | Value |
| Input | Num | a number greater than or equal to 1 |
| Output | Num | the inverse hyperoblic cosine of the input, or nan if the input is invalid |
| Program | Type | Value | Error |
0.9 acosh | Num | nan | |
1 acosh | Num | 0 | |
10 acosh | Num | 2.993222846126381 | |
Computes the inverse sine.
| Type | Value |
| Input | Num | a number in the interval [-1, 1] |
| Output | Num | the inverse sine (in radians) of the input, of nan if the input is invalid |
| Program | Type | Value | Error |
-2 asin | Num | nan | |
-1 asin | Num | -1.5707963267948966 | |
0 asin | Num | 0 | |
1 asin | Num | 1.5707963267948966 | |
1.1 asin | Num | nan | |
Computes the inverse hyperbolic sine.
| Type | Value |
| Input | Num | a number |
| Output | Num | the inverse hyperbolic sine of the input |
| Program | Type | Value | Error |
-1 asinh | Num | -0.881373587019543 | |
0 asinh | Num | 0 | |
1 asinh | Num | 0.881373587019543 | |
2 asinh | Num | 1.4436354751788103 | |
Computes the inverse tangent.
| Type | Value |
| Input | Num | a number |
| Output | Num | the inverse tangent (in radians) of the input |
| Program | Type | Value | Error |
-10 atan | Num | -1.4711276743037345 | |
-1 atan | Num | -0.7853981633974483 | |
0 atan | Num | 0 | |
1 atan | Num | 0.7853981633974483 | |
Computes the angle in the plane.
| Type | Value |
| Input | Num | a number y (y-coordinate) |
| x (param #1) | Num | a number (x-coordinate) |
| Output | Num | the angle in the plane (in radians) between the positive x-axis and the ray from (0, 0) to (x, y) |
| Program | Type | Value | Error |
5 atan2(5) | Num | 0.7853981633974483 | |
10 atan2(10) | Num | 0.7853981633974483 | |
10 atan2(0) | Num | 1.5707963267948966 | |
Computes the inverse hyperbolic tangent.
| Type | Value |
| Input | Num | a number in the interval [-1, 1] |
| Output | Num | the inverse hyperbolic tangent of the input, or nan if the input is invalid |
| Program | Type | Value | Error |
-2 atanh | Num | nan | |
-1 atanh | Num | -inf | |
0 atanh | Num | 0 | |
0.5 atanh | Num | 0.5493061443340548 | |
1 atanh | Num | inf | |
Computes the cube root.
| Type | Value |
| Input | Num | a number |
| Output | Num | the cube root of the input |
| Program | Type | Value | Error |
-1 cbrt | Num | -1 | |
1 cbrt | Num | 1 | |
inf cbrt | Num | inf | |
64 cbrt | Num | 4 | |
Rounds a number up.
| Type | Value |
| Input | Num | a number |
| Output | Num | the smallest integer greater than or equal to the input |
| Program | Type | Value | Error |
.95 ceil | Num | 1 | |
4 ceil | Num | 4 | |
7.004 ceil | Num | 8 | |
-7.004 ceil | Num | -7 | |
Count leading zeros.
| Type | Value |
| Input | Num | a number (is truncated to integer) |
| Output | Num | the number of leading zero bits in the 32-bit binary representation of the input |
| Program | Type | Value | Error |
-inf clz32 | Num | 32 | |
-4 clz32 | Num | 0 | |
-1 clz32 | Num | 0 | |
0 clz32 | Num | 32 | |
0.5 clz32 | Num | 32 | |
1 clz32 | Num | 31 | |
1.1 clz32 | Num | 31 | |
4 clz32 | Num | 29 | |
4.7 clz32 | Num | 29 | |
1000 clz32 | Num | 22 | |
inf clz32 | Num | 32 | |
Computes the cosine.
| Type | Value |
| Input | Num | an angle in radians |
| Output | Num | the cosine of the input |
| Program | Type | Value | Error |
-inf cos | Num | nan | |
-0 cos | Num | 1 | |
0 cos | Num | 1 | |
1 cos | Num | 0.5403023058681398 | |
pi cos | Num | -1 | |
pi *2 cos | Num | 1 | |
inf cos | Num | nan | |
Computes the hyperbolic cosine.
| Type | Value |
| Input | Num | a number |
| Output | Num | the hyperbolic cosine of the input |
| Program | Type | Value | Error |
0 cosh | Num | 1 | |
1 cosh | Num | 1.5430806348152437 | |
-1 cosh | Num | 1.5430806348152437 | |
2 cosh | Num | 3.7621956910836314 | |
Computes the exponential function.
| Type | Value |
| Input | Num | the exponent |
| Output | Num | e (Euler's number) raised to the exponent |
| Program | Type | Value | Error |
-inf exp | Num | 0 | |
-1 exp | Num | 0.36787944117144233 | |
0 exp | Num | 1 | |
1 exp | Num | 2.718281828459045 | |
inf exp | Num | inf | |
Computes the exponential function, subtracted by one.
| Type | Value |
| Input | Num | the exponent |
| Output | Num | |
e (Euler's number) raised to the exponent, minus 1
| Program | Type | Value | Error |
-inf expm1 | Num | -1 | |
-1 expm1 | Num | -0.6321205588285577 | |
-0 expm1 | Num | -0 | |
0 expm1 | Num | 0 | |
1 expm1 | Num | 1.718281828459045 | |
inf expm1 | Num | inf | |
Rounds down.
| Type | Value |
| Input | Num | a number |
| Output | Num | the largest integer less than or equal to the input |
| Program | Type | Value | Error |
5.95 floor | Num | 5 | |
5.05 floor | Num | 5 | |
5 floor | Num | 5 | |
-5.05 floor | Num | -6 | |
Rounds to 32-bit precision.
| Type | Value |
| Input | Num | a number |
| Output | Num | the nearest 32-bit single precision float representation |
| Program | Type | Value | Error |
5.5 fround | Num | 5.5 | |
5.05 fround | Num | 5.050000190734863 | |
5 fround | Num | 5 | |
-5.05 fround | Num | -5.050000190734863 | |
Computes the square root of the sum of squares
| Type | Value |
| Input | Arr<Num...> | an array of numbers |
| Output | Num | the square root of the sum of the squares of the input numbers |
| Program | Type | Value | Error |
[3, 4] hypot | Num | 5 | |
[5, 12] hypot | Num | 13 | |
[3, 4, 5] hypot | Num | 7.0710678118654755 | |
[-5] hypot | Num | 5 | |
32-bit multiplication
| Type | Value |
| Input | Num | the first factor |
| y (param #1) | Num | the second factor |
| Output | Num | the product of the 32-bit versions (cf. fround) of the factors |
| Program | Type | Value | Error |
3 imul(4) | Num | 12 | |
-5 imul(12) | Num | -60 | |
"ffffffff" parseInt(16) imul(5) | Num | -5 | |
"fffffffe" parseInt(16) imul(5) | Num | -10 | |
Computes the natural logarithm.
| Type | Value |
| Input | Num | a number |
| Output | Num | the natural (base e) logarithm of the input |
| Program | Type | Value | Error |
-1 log | Num | nan | |
-0 log | Num | -inf | |
0 log | Num | -inf | |
1 log | Num | 0 | |
10 log | Num | 2.302585092994046 | |
inf log | Num | inf | |
8 log /(2 log) | Num | 3 | |
625 log /(5 log) | Num | 4 | |
Computes the base 10 logarithm.
| Type | Value |
| Input | Num | a number |
| Output | Num | the base 10 logarithm of the input |
| Program | Type | Value | Error |
-2 log10 | Num | nan | |
-0 log10 | Num | -inf | |
0 log10 | Num | -inf | |
1 log10 | Num | 0 | |
2 log10 | Num | 0.3010299956639812 | |
100000 log10 | Num | 5 | |
inf log10 | Num | inf | |
Computes the natural logarithm of x + 1.
| Type | Value |
| Input | Num | a number (x) |
| Output | Num | |
the natural (base e) logarithm of (x + 1)
| Program | Type | Value | Error |
1 log1p | Num | 0.6931471805599453 | |
0 log1p | Num | 0 | |
-1 log1p | Num | -inf | |
-2 log1p | Num | nan | |
Computes the base 2 logarithm.
| Type | Value |
| Input | Num | a number |
| Output | Num | the base 2 logarithm of the input |
| Program | Type | Value | Error |
3 log2 | Num | 1.5849625007211563 | |
2 log2 | Num | 1 | |
1 log2 | Num | 0 | |
0 log2 | Num | -inf | |
Finds the largest number
| Type | Value |
| Input | Arr<Num...> | an array of numbers |
| Output | Num | the largest number in the input, or -inf if the input is empty |
| Program | Type | Value | Error |
[1, 3, 2] max | Num | 3 | |
[-1, -3, -2] max | Num | -1 | |
[] max | Num | -inf | |
Finds the smallest number
| Type | Value |
| Input | Arr<Num...> | an array of numbers |
| Output | Num | the smallest number in the input, or inf if the input is empty |
| Program | Type | Value | Error |
[1, 3, 2] min | Num | 1 | |
[-1, -3, -2] min | Num | -3 | |
[] min | Num | inf | |
Computes powers (exponentiation).
| Type | Value |
| Input | Num | the base |
| y (param #1) | Num | the exponent |
| Output | Num | the base taken to the y-th power |
| Program | Type | Value | Error |
7 **3 | Num | 343 | |
4 **.5 | Num | 2 | |
7 **(-2) | Num | 0.02040816326530612 | |
-7 **0.5 | Num | nan | |
Returns a random number between 0 and 1.
| Type | Value |
| Input | Any | any value (is ignored) |
| Output | Num | a floating-point, pseudo-random number n with 0 <= n < 1 and approximately uniform distribution over that range |
| Program | Type | Value | Error |
[null] repeat(1000) each(random) each(>=0) all | Bool | true | |
[null] repeat(1000) each(random) each(<1) all | Bool | true | |
Returns a random integer number in a specified interval.
| Type | Value |
| Input | Any | any value (is ignored) |
| from (param #1) | Num | lower bound (inclusive); rounded towards 0 if not integer |
| to (param #2) | Num | upper bound (exclusive); rounded towards 0 if not integer |
| Output | Num | a integer, pseudorandom number n with from <= n < to and approximately uniform distribution over that range |
| Program | Type | Value | Error |
[null] repeat(1000) each(random(2, 7)) each(>=2) all | Bool | true | |
[null] repeat(1000) each(random(2, 7)) each(<7) all | Bool | true | |
[null] repeat(1000) each(random(2, 7)) each(=n floor ==n) all | Bool | true | |
[null] repeat(1000) each(random(2.4, 7.6)) each(>=2) all | Bool | true | |
[null] repeat(1000) each(random(2.4, 7.6)) each(<7) all | Bool | true | |
[null] repeat(1000) each(random(2.4, 7.6)) each(=n floor ==n) all | Bool | true | |
[null] repeat(1000) each(random(-7, -2)) each(>=(-7)) all | Bool | true | |
[null] repeat(1000) each(random(-7, -2)) each(<(-2)) all | Bool | true | |
[null] repeat(1000) each(random(-7, -2)) each(=n floor ==n) all | Bool | true | |
[null] repeat(1000) each(random(-7.6, -2.4)) each(>=(-7)) all | Bool | true | |
[null] repeat(1000) each(random(-7.6, -2.4)) each(<(-2)) all | Bool | true | |
[null] repeat(1000) each(random(-7.6, -2.4)) each(=n floor ==n) all | Bool | true | |
random(7, 2) | Bool | | Value error Code: UnexpectedValue Message: Component got an unexpected input value.
|
random(2, 2) | Bool | | Value error Code: UnexpectedValue Message: Component got an unexpected input value.
|
Rounds a number to the nearest integer.
| Type | Value |
| Input | Num | a number |
| Output | Num | the nearest integer, or away from zero if there's two nearest integers |
| Program | Type | Value | Error |
0.9 round | Num | 1 | |
5.95 round | Num | 6 | |
5.5 round | Num | 6 | |
5.05 round | Num | 5 | |
-5.05 round | Num | -5 | |
-5.5 round | Num | -6 | |
-5.95 round | Num | -6 | |
Determines the sign of a number.
| Type | Value |
| Input | Num | a number |
| Output | Num | 1 if the input is positive, -1 if negative, 0 if it's 0, and -0 if it's -0 |
| Program | Type | Value | Error |
3 sign | Num | 1 | |
0 sign | Num | 0 | |
-0 sign | Num | -0 | |
-3 | Num | -3 | |
Computes the sine.
| Type | Value |
| Input | Num | an angle in radians |
| Output | Num | the sine of the input |
| Program | Type | Value | Error |
-inf sin | Num | nan | |
-0 sin | Num | -0 | |
0 sin | Num | 0 | |
1 sin | Num | 0.8414709848078965 | |
pi /2 sin | Num | 1 | |
inf sin | Num | nan | |
Computes the hyperbolic sine.
| Type | Value |
| Input | Num | a number |
| Output | Num | the hyperbolic sine of the input |
| Program | Type | Value | Error |
0 sinh | Num | 0 | |
1 sinh | Num | 1.1752011936438014 | |
-1 sinh | Num | -1.1752011936438014 | |
2 sinh | Num | 3.626860407847019 | |
Computes square roots.
| Type | Value |
| Input | Num | a number |
| Output | Num | the square root of the input, or nan if it's negative |
| Program | Type | Value | Error |
-1 sqrt | Num | nan | |
-0 sqrt | Num | -0 | |
0 sqrt | Num | 0 | |
1 sqrt | Num | 1 | |
2 sqrt | Num | 1.4142135623730951 | |
9 sqrt | Num | 3 | |
inf sqrt | Num | inf | |
Computes the tangent.
| Type | Value |
| Input | Num | an angle in radians |
| Output | Num | the tangent of the input |
| Program | Type | Value | Error |
0 *pi /180 tan | Num | 0 | |
45 *pi /180 tan | Num | 1 | |
90 *pi /180 tan | Num | 16331239353195392 | |
Computes the hyperbolic tangent.
| Type | Value |
| Input | Num | a number |
| Output | Num | the hyperbolic tangent of the input |
| Program | Type | Value | Error |
-1 tanh | Num | -0.7615941559557649 | |
0 tanh | Num | 0 | |
inf tanh | Num | 1 | |
1 tanh | Num | 0.7615941559557649 | |
Rounds towards zero.
| Type | Value |
| Input | Num | a number |
| Output | Num | the input without fractional digits |
| Program | Type | Value | Error |
13.37 trunc | Num | 13 | |
42.84 trunc | Num | 42 | |
0.123 trunc | Num | 0 | |
-0.123 trunc | Num | -0 | |
Compares strings lexicographically.
| Type | Value |
| Input | Str | a string |
| other (param #1) | Str | another string |
| Output | Bool | true if the input appears before other in lexicographical order, false otherwise |
| Program | Type | Value | Error |
"a" <"b" | Bool | true | |
"Ab" <"A" | Bool | false | |
"cd" <"cd" | Bool | false | |
Compares strings lexicographically.
| Type | Value |
| Input | Str | a string |
| other (param #1) | Str | another string |
| Output | Bool | true if the input appears after other in lexicographical order, false otherwise |
| Program | Type | Value | Error |
"a" >"b" | Bool | false | |
"Ab" >"A" | Bool | true | |
"cd" >"cd" | Bool | false | |
Compares strings lexicographically.
| Type | Value |
| Input | Str | a string |
| other (param #1) | Str | another string |
| Output | Bool | true if the input appears before other in lexicographical order or is equal to it, false otherwise |
| Program | Type | Value | Error |
"a" <="b" | Bool | true | |
"Ab" <="A" | Bool | false | |
"cd" <="cd" | Bool | true | |
Compares strings lexicographically.
| Type | Value |
| Input | Str | a string |
| other (param #1) | Str | another string |
| Output | Bool | true if the input appears after other in lexicographical order or is equal to it, false otherwise |
| Program | Type | Value | Error |
"a" >="b" | Bool | false | |
"Ab" >="A" | Bool | true | |
"cd" >="cd" | Bool | true | |
Concatenates two strings.
| Type | Value |
| Input | Str | a string |
| b (param #1) | Str | another string |
| Output | Str | The input and b, concatenated. |
| Program | Type | Value | Error |
"ab" +"cd" | Str | "abcd" | |
"ab" +"" | Str | "ab" | |
"" +"cd" | Str | "cd" | |
Converts a string to bytes.
| Type | Value |
| Input | Str | a string |
| Output | Arr<Num...> | The UTF-8 bytes representing the string. |
| Program | Type | Value | Error |
"abc" bytes | Arr<Num...> | [97, 98, 99] | |
"Köln" bytes | Arr<Num...> | [75, 195, 182, 108, 110] | |
"日本語" bytes | Arr<Num...> | [230, 151, 165, 230, 156, 172, 232, 170, 158] | |
"\x00" bytes | Arr<Num...> | [0] | |
Converts bytes to a string.
| Type | Value |
| Input | Arr<Num...> | an array of numbers (interpreted modulo 256 as UTF-8 bytes) |
| Output | Str | the string represented by the input |
| Program | Type | Value | Error |
[97, 98, 99] bytesToStr | Str | "abc" | |
[75, 195, 182, 108, 110] bytesToStr | Str | "Köln" | |
[230, 151, 165, 230, 156, 172, 232, 170, 158] bytesToStr | Str | "日本語" | |
[0] bytesToStr | Str | "\x00" | |
[256] bytesToStr | Str | "\x00" | |
Converts a string to Unicode code points.
| Type | Value |
| Input | Str | a string |
| Output | Arr<Num...> | the input represented as a sequence of code points |
If the input string contains invalid UTF-8 byte sequences, they will be represented by the Unicode replacement character (code point 65533).
| Program | Type | Value | Error |
"abc" codePoints | Arr<Num...> | [97, 98, 99] | |
"Köln" codePoints | Arr<Num...> | [75, 246, 108, 110] | |
"日本語" codePoints | Arr<Num...> | [26085, 26412, 35486] | |
"\x80" codePoints | Arr<Num...> | [65533] | |
Converts Unicode code points to a string.
| Type | Value |
| Input | Arr<Num...> | a sequence of numbers |
| Output | Str | UTF-8 encoded version of the input |
| Program | Type | Value | Error |
[97, 98, 99] codePointsToStr | Str | "abc" | |
[75, 246, 108, 110] codePointsToStr | Str | "Köln" | |
[26085, 26412, 35486] codePointsToStr | Str | "日本語" | |
[65533] codePointsToStr | Str | "�" | |
Splits a string around whitespace.
| Type | Value |
| Input | Str | a string |
| Output | Arr<Str...> | the result of splitting the string around any kind or amount of white space |
| Program | Type | Value | Error |
" foo bar baz " fields | Arr<Str...> | ["foo", "bar", "baz"] | |
Finds the position of a string within another.
| Type | Value |
| Input | Str | string to search inside |
| needle (param #1) | Str | string to search for |
| Output | Num | offset of first occurrence of needle from the beginning of the input, measured in bytes, or -1 if none |
| Program | Type | Value | Error |
"abc" indexOf("bc") | Num | 1 | |
"abc" indexOf("a") | Num | 0 | |
"abc" indexOf("d") | Num | -1 | |
"Köln" indexOf("l") | Num | 3 | |
Concatenates any number of strings.
| Type | Value |
| Input | Arr<Str...> | an array of strings |
| Output | Str | the concatenation of all the strings in the input |
| Program | Type | Value | Error |
["ab", "cd", "ef"] join | Str | "abcdef" | |
["ab", "cd"] join | Str | "abcd" | |
["ab"] join | Str | "ab" | |
for Any def f Arr<Str...> as [] ok f join | Str | "" | |
Concatenates any number of strings with a custom delimiter.
| Type | Value |
| Input | Arr<Str...> | an array of strings |
| glue (param #1) | Str | delimiter to put between strings |
| Output | Str | the concatenation of all the strings in the input, with the glue in between |
| Program | Type | Value | Error |
["ab", "cd", "ef"] join(";") | Str | "ab;cd;ef" | |
["ab", "", "cd"] join(";") | Str | "ab;;cd" | |
["ab"] join(";") | Str | "ab" | |
for Any def f Arr<Str...> as [] ok f join(";") | Str | "" | |
Right-pads a string.
| Type | Value |
| Input | Str | a string |
| targetLength (param #1) | Num | minimum string length for the output |
| padString (param #2) | Str | string to use as padding to bring the input to the desired length |
| Output | Str | the input followed by as many repetitions of padString as necessary to reach targetLength |
padString is usually one character but can be longer, in which case each repetition starts from the left.
| Program | Type | Value | Error |
"z" padEnd(2, " ") | Str | "z " | |
"z" padEnd(3, " ") | Str | "z " | |
"zzz" padEnd(3, " ") | Str | "zzz" | |
"zzzz" padEnd(3, " ") | Str | "zzzz" | |
"zzzz" padEnd(7, "ab") | Str | "zzzzaba" | |
Left-pads a string.
| Type | Value |
| Input | Str | a string |
| targetLength (param #1) | Num | minimum string length for the output |
| padString (param #2) | Str | string to use as padding to bring the input to the desired length |
| Output | Str | the input following as many repetitions of padString as necessary to reach targetLength |
padString is usually one character but can be longer, in which case each repetition starts from the left.
| Program | Type | Value | Error |
"z" padStart(2, " ") | Str | " z" | |
"z" padStart(3, " ") | Str | " z" | |
"zzz" padStart(3, " ") | Str | "zzz" | |
"zzzz" padStart(3, " ") | Str | "zzzz" | |
"zzzz" padStart(7, "ab") | Str | "abazzzz" | |
Replaces substrings.
| Type | Value |
| Input | Str | a string |
| needle (param #1) | Str | a substring to look for |
| replacement (param #2) | Str | a new string to replace needle with |
| Output | Str | the input with all occurrences of needle replaced with replacement |
More precisely, whenever there are two or more overlapping occurrences of needle in the input, only the first one is replaced.
| Program | Type | Value | Error |
"ababa" replaceAll("b", "c") | Str | "acaca" | |
Replaces a substring.
| Type | Value |
| Input | Str | a string |
| needle (param #1) | Str | a substring to look for |
| replacement (param #2) | Str | a new string to replace needle with |
| Output | Str | the input with the first ocurrence of needle replaced with replacement |
| Program | Type | Value | Error |
"ababa" replaceFirst("b", "c") | Str | "acaba" | |
Checks whether a string starts with a specific substring.
| Type | Value |
| Input | Str | a string |
| needle (param #1) | Str | a prefix to look for |
| Output | Bool | true if the input starts with needle, false otherwise |
| Program | Type | Value | Error |
"abc" startsWith("ab") | Bool | true | |
"abc" startsWith("b") | Bool | false | |
Checks whether a string ends with a specific substring.
| Type | Value |
| Input | Str | a string |
| needle (param #1) | Str | a suffix to look for |
| Output | Bool | true if the input ends with needle, false otherwise |
| Program | Type | Value | Error |
"abc" endsWith("bc") | Bool | true | |
"abc" endsWith("b") | Bool | false | |
Concatenates multiple repetitions of a string.
| Type | Value |
| Input | Str | a string |
| n (param #1) | Num | number of repetitions (will be truncated and min'd to 0) |
| Output | Str | the input, repeated n times |
| Program | Type | Value | Error |
"abc" repeat(3) | Str | "abcabcabc" | |
"abc" repeat(0) | Str | "" | |
"abc" repeat(-1) | Str | "" | |
"abc" repeat(1.6) | Str | "abc" | |
Extracts a substring from a string.
| Type | Value |
| Input | Str | a string |
| start (param #1) | Num | a positive integer |
| Output | Str | the portion of the input that is after offset start |
| Program | Type | Value | Error |
"abc" slice(-4) | Str | "abc" | |
"abc" slice(-3) | Str | "abc" | |
"abc" slice(-2) | Str | "bc" | |
"abc" slice(-1) | Str | "c" | |
"abc" slice(0) | Str | "abc" | |
"abc" slice(1) | Str | "bc" | |
"abc" slice(2) | Str | "c" | |
"abc" slice(3) | Str | "" | |
"abc" slice(4) | Str | "" | |
Extracts a substring from a string.
| Type | Value |
| Input | Str | a string |
| start (param #1) | Num | offset to start after |
| end (param #2) | Num | offset to end before |
| Output | Str | the portion of the input that is after offset start but before offset end |
Negative offsets are counted from the end of the string.
| Program | Type | Value | Error |
"abc" slice(1, 2) | Str | "b" | |
"abc" slice(1, -1) | Str | "b" | |
"abc" slice(-2, -1) | Str | "b" | |
"abc" slice(-1, -2) | Str | "" | |
"abc" slice(2, 1) | Str | "" | |
"abc" slice(-5, -4) | Str | "" | |
"abc" slice(-5, -3) | Str | "" | |
"abc" slice(-5, -2) | Str | "a" | |
"abc" slice(-5, -1) | Str | "ab" | |
"abc" slice(-1, -5) | Str | "" | |
"abc" slice(2, -5) | Str | "" | |
"abc" slice(0, 4) | Str | "abc" | |
"abc" slice(4, 4) | Str | "" | |
Removes whitespace from the start and end of a string.
| Type | Value |
| Input | Str | a string |
| Output | Str | the input, with leading and trailing whitespace removed |
| Program | Type | Value | Error |
" abc " trim | Str | "abc" | |
Removes whitespace from the start of a string.
| Type | Value |
| Input | Str | a string |
| Output | Str | the input, with leading whitespace removed |
| Program | Type | Value | Error |
" abc " trimStart | Str | "abc " | |
Removes whitespace from the end of a string.
| Type | Value |
| Input | Str | a string |
| Output | Str | the input, with trailing whitespace removed |
| Program | Type | Value | Error |
" abc " trimEnd | Str | " abc" | |
Concatenates two arrays.
| Type | Value |
| Input | Arr<<A Any>...> | an array |
| other (param #1) | Arr<<B Any>...> | another array |
| Output | Arr<<A Any>|<B Any>...> | the concatenation of both arrays |
| Program | Type | Value | Error |
[1, 2, 3] +[4, 5] | Arr<Num...> | [1, 2, 3, 4, 5] | |
[] +[4, 5] | Arr<Num...> | [4, 5] | |
[1, 2, 3] +[] | Arr<Num...> | [1, 2, 3] | |
[] +[] | Arr<> | [] | |
["a", "b"] +["c", "d"] | Arr<Str...> | ["a", "b", "c", "d"] | |
["a", "b"] +[1, 2] | Arr<Str|Num...> | ["a", "b", 1, 2] | |
Checks if all elements are true.
| Type | Value |
| Input | Arr<Bool...> | an array of boolean values |
| Output | Bool | true iff all elements of the input are true |
| Program | Type | Value | Error |
[] all | Bool | true | |
[true] all | Bool | true | |
[true, true] all | Bool | true | |
[true, false, true] all | Bool | false | |
Removes the first n elements from the beginning of an array.
| Type | Value |
| Input | Arr<<A Any>...> | an array |
| n (param #1) | Num | number of elements to remove |
| Output | Arr<<A Any>...> | the input with the first n elements removed |
| Program | Type | Value | Error |
[1, 2, 3] drop(-1) | Arr<Num...> | [1, 2, 3] | |
[1, 2, 3] drop(0) | Arr<Num...> | [1, 2, 3] | |
[1, 2, 3] drop(1) | Arr<Num...> | [2, 3] | |
[1, 2, 3] drop(2) | Arr<Num...> | [3] | |
[1, 2, 3] drop(3) | Arr<Num...> | [] | |
[1, 2, 3] drop(4) | Arr<Num...> | [] | |
Removes elements satisfying a predicate from the beginning of an array.
| Type | Value |
| Input | Arr<<A Any>...> | an array |
| predicate (param #1) | for <A Any> Obj<yes: <B Any>, Any>|Obj<no: <C Any>, Any> | a predicate to apply to the elements of the input |
| Output | Arr<<A Any>...> | The elements from the input, starting with the first one that does not satisfy the predicate. |
| Program | Type | Value | Error |
[{a: 1}, {a: 2}, {b: 3}, {a: 4}] dropWhile(is {a: _}) | Arr<Obj<b: Num, Void>|Obj<a: Num, Void>...> | [{b: 3}, {a: 4}] | |
Applies a function to every element.
| Type | Value |
| Input | Arr<<A Any>...> | an array |
| f (param #1) | for <A Any> <B Any> | a function to apply to each element of the input |
| Output | Arr<<B Any>...> | a list with the outputs of f applied to each element of the input |
| Program | Type | Value | Error |
[1, 2, 3] each(*2) | Arr<Num...> | [2, 4, 6] | |
[{a: 1}, {a: 2}, {b: 3}, {a: 4}] takeWhile(is {a: _}) each(@a) | Arr<Num...> | [1, 2] | |
Pairs each element with a 0-based index.
| Type | Value |
| Input | Arr<<A Any>...> | |
| Output | Arr<Arr<Num, <A Any>>...> | the input with each element replaced with a 2-element array, the second element of which is the original element and the first is its index in the array, counting from 0 |
| Program | Type | Value | Error |
["a", "b", "c"] enum | Arr<Arr<Num, Str>...> | [[0, "a"], [1, "b"], [2, "c"]] | |
Pairs each element with an index.
| Type | Value |
| Input | Arr<<A Any>...> | an array |
| start (param #1) | Num | at which number to start counting |
| Output | Arr<Arr<Num, <A Any>>...> | the input with each element replaced with a 2-element array, the second element of which is the original element and the first is its index in the array, counting from start |
| Program | Type | Value | Error |
["a", "b", "c"] enum(1) | Arr<Arr<Num, Str>...> | [[1, "a"], [2, "b"], [3, "c"]] | |
Finds the index and first element satisfying a predicate.
| Type | Value |
| Input | Arr<<A Any>...> | an array |
| predicate (param #1) | for <A Any> Obj<yes: <B Any>, Any>|Obj<no: <C Any>, Any> | a predicate to apply to elements of the input |
| Output | Null|Arr<Num, <A Any>> | the first element of the input satisfying the predicate, paired with its index, or Null if none |
| Program | Type | Value | Error |
[1, 2, 3] findFirst(is Num with %2 ==0) | Null|Arr<Num, Num> | [1, 2] | |
[1, 2, 3] findFirst(is Num with %4 ==0) | Null|Arr<Num, Num> | null | |
Finds the index of the first element satisfying a predicate.
| Type | Value |
| Input | Arr<<A Any>...> | an array |
| predicate (param #1) | for <A Any> Obj<yes: <B Any>, Any>|Obj<no: <C Any>, Any> | a predicate to apply to elements of the input |
| Output | Null|Num | the index of the first element of the input satisfying the predicate, or Null if none |
| Program | Type | Value | Error |
[1, 2, 3] findFirstIndex(is Num with %2 ==0) | Null|Num | 1 | |
[1, 2, 3] findFirstIndex(is Num with %4 ==0) | Null|Num | null | |
Finds the first element satisfying a predicate.
| Type | Value |
| Input | Arr<<A Any>...> | an array |
| predicate (param #1) | for <A Any> Obj<yes: <B Any>, Any>|Obj<no: <C Any>, Any> | a predicate to apply to elements of the input |
| Output | Null|<A Any> | the first element of the input satisfying the predicate, or Null if none |
| Program | Type | Value | Error |
[1, 2, 3] findFirstValue(is Num with %2 ==0) | Null|Num | 2 | |
[1, 2, 3] findFirstValue(is Num with %4 ==0) | Null|Num | null | |
Finds the index and last element satisfying a predicate.
| Type | Value |
| Input | Arr<<A Any>...> | an array |
| predicate (param #1) | for <A Any> Obj<yes: <B Any>, Any>|Obj<no: <C Any>, Any> | a predicate to apply to elements of the input |
| Output | Null|Arr<Num, <A Any>> | the last element of the input satisfying the predicate, paired with its index, or Null if none |
| Program | Type | Value | Error |
[1, 2, 3, 4] findLast(is Num with %2 ==0) | Null|Arr<Num, Num> | [3, 4] | |
[1, 2, 3, 4] findLast(is Num with %8 ==0) | Null|Arr<Num, Num> | null | |
Finds the index of the last element satisfying a predicate.
| Type | Value |
| Input | Arr<<A Any>...> | an array |
| predicate (param #1) | for <A Any> Obj<yes: <B Any>, Any>|Obj<no: <C Any>, Any> | a predicate to apply to elements of the input |
| Output | Null|Num | the index of the last element of the input satisfying the predicate, or Null if none |
| Program | Type | Value | Error |
[1, 2, 3, 4] findLastIndex(is Num with %2 ==0) | Null|Num | 3 | |
[1, 2, 3, 4] findLastIndex(is Num with %8 ==0) | Null|Num | null | |
Finds the last element satisfying a predicate.
| Type | Value |
| Input | Arr<<A Any>...> | an array |
| predicate (param #1) | for <A Any> Obj<yes: <B Any>, Any>|Obj<no: <C Any>, Any> | a predicate to apply to elements of the input |
| Output | Null|<A Any> | the last element of the input satisfying the predicate, or Null if none |
| Program | Type | Value | Error |
[1, 2, 3, 4] findLastValue(is Num with %2 ==0) | Null|Num | 4 | |
[1, 2, 3, 4] findLastValue(is Num with %8 ==0) | Null|Num | null | |
Aggregates an array recursively.
| Type | Value |
| Input | Arr<<A Any>...> | an array |
| start (param #1) | <B Any> | initial accumulator |
| combine (param #2) | for <B Any> (<A Any>) <B Any> | a function that combines the current accumulator with the next element to produce a new accumulator |
| Output | <B Any> | the accumulator after processing all elements |
| Program | Type | Value | Error |
[2, 3, 4] fold(0, +) | Num | 9 | |
[2, 3, 4] fold(1, *) | Num | 24 | |
Returns the element at a given index.
| Type | Value |
| Input | Arr<<A Any>...> | an array |
| index (param #1) | Num | a 0-based index into the input |
| Output | <A Any> | the element at the given index |
Throws BadIndex if the index is invalid or NoSuchIndex if there is not element at the give index.
| Program | Type | Value | Error |
[] get(0) | Void | | Type error Code: VoidProgram Message: Type of program cannot be Void.
|
["a", "b", "c"] get(0) | Str | "a" | |
["a", "b", "c"] get(-1) | Str | | Value error Code: BadIndex Message: Index must be a nonnegative integer. Got value: -1
|
["a", "b", "c"] get(2) | Str | "c" | |
["a", "b", "c"] get(3) | Str | | Value error Code: NoSuchIndex Message: Array is not long enough. Got value: 3
|
Returns the element at a given index, or a default value.
| Type | Value |
| Input | Arr<<A Any>...> | an array |
| index (param #1) | Num | a 0-based index into the input |
| default (param #2) | <B Any> | default value to retur if there is no element at the given index |
| Output | <A Any>|<B Any> | the element at the given index, or default if there isn't one. |
Throws BadIndex if the index is invalid.
| Program | Type | Value | Error |
[] get(0, null) | Null | null | |
["a", "b", "c"] get(0, null) | Str|Null | "a" | |
["a", "b", "c"] get(-1, null) | Str|Null | | Value error Code: BadIndex Message: Index must be a nonnegative integer. Got value: -1
|
["a", "b", "c"] get(3, null) | Str|Null | null | |
Join several arrays together into one.
| Type | Value |
| Input | Arr<Arr<<A Any>...>...> | an array of arrays |
| Output | Arr<<A Any>...> | All arrays in the input joined together into one. |
| Program | Type | Value | Error |
[] join | Arr<<A>...> | [] | |
[[]] join | Arr<> | [] | |
[[1]] join | Arr<Num...> | [1] | |
[[1, 2]] join | Arr<Num...> | [1, 2] | |
[[], []] join | Arr<> | [] | |
[[], [1]] join | Arr<Num...> | [1] | |
[[1], [2, 3]] join | Arr<Num...> | [1, 2, 3] | |
[[1], [2, [3]]] join | Arr<Num|Arr<Num>...> | [1, 2, [3]] | |
Keep only elements satisfying a predicate.
| Type | Value |
| Input | Arr<<A Any>...> | an array |
| predicate (param #1) | for <A Any> Obj<yes: <B Any>, Any>|Obj<no: <C Any>, Any> | a predicate to apply to elements of the input |
| Output | Arr<<B Any>...> | The input with all elements not satisfying the predicate removed. |
| Program | Type | Value | Error |
["a", 1, "b", 2, "c", 3] keep(is Num with %2 >0 elis Str) | Arr<Num|Str...> | ["a", 1, "b", "c", 3] | |
[{n: 1}, {n: 2}, {n: 3}] keep(is {n: n} with n %2 >0) | Arr<Obj<n: Num, Void>...> | [{n: 1}, {n: 3}] | |
[1, 2, 3, 4, 5, 6] keep(if %2 ==0) each(*2) | Arr<Num...> | [4, 8, 12] | |
[1, 2, 3, 4, 5, 6] keep(if %2 ==0 not) each(id) | Arr<Num...> | [1, 3, 5] | |
[1, 2, 3] keep(if %2 ==0 not) each(+1) | Arr<Num...> | [2, 4] | |
[1, 2, 3] keep(if false) | Arr<Num...> | [] | |
[{n: 1}, 2, {n: 3}] keep(is {n: n}) each(@n) | Arr<Num...> | [1, 3] | |
Computes the length of an array.
| Type | Value |
| Input | Arr<Any...> | an array |
| Output | Num | how many elements the input has |
| Program | Type | Value | Error |
[] len | Num | 0 | |
["a", 2, []] len | Num | 3 | |
Finds the maximum element.
| Type | Value |
| Input | Arr<<A Any>...> | an array |
| less (param #1) | for <A Any> (<A Any>) Bool | funcer that tests whether its input is "less than" its first argument |
| default (param #2) | <B Any> | default value to return if the input is empty |
| Output | <A Any>|<B Any> | the maximum element, or default if the input is empty |
| Program | Type | Value | Error |
["abc", "b", "ab"] max(<, "") | Str | "b" | |
[0, 1, 2] max(>, -100) | Num | 0 | |
for Any def f Arr<Num...> as [] ok f max(<, 0) | Num | 0 | |
Finds the maximum element according to a sorting key.
| Type | Value |
| Input | Arr<<A Any>...> | an array |
| sortKey (param #1) | for <A Any> <B Any> | funcer that maps input elements to values by which they will be compared |
| less (param #2) | for <B Any> (<B Any>) Bool | funcer that tests whether its input is "less than" its first argument |
| default (param #3) | <C Any> | default value to return if the input is empty |
| Output | <A Any>|<C Any> | the maximum element, or default if the input is empty |
| Program | Type | Value | Error |
["abc", "b", "ab"] max(bytes len, <, "") | Str | "abc" | |
Finds the minimum element.
| Type | Value |
| Input | Arr<<A Any>...> | an array |
| less (param #1) | for <A Any> (<A Any>) Bool | funcer that tests whether its input is "less than" its first argument |
| default (param #2) | <B Any> | default value to return if the input is empty |
| Output | <A Any>|<B Any> | the minimum element, or default if the input is empty |
| Program | Type | Value | Error |
["abc", "b", "ab"] min(<, "") | Str | "ab" | |
Finds the minimum element according to a sorting key.
| Type | Value |
| Input | Arr<<A Any>...> | an array |
| sortKey (param #1) | for <A Any> <B Any> | funcer that maps input elements to values by which they will be compared |
| less (param #2) | for <B Any> (<B Any>) Bool | funcer that tests whether its input is "less than" its first argument |
| default (param #3) | <C Any> | default value to return if the input is empty |
| Output | <A Any>|<C Any> | the minimum element, or default if the input is empty |
| Program | Type | Value | Error |
["abc", "b", "ab"] min(bytes len, <, "") | Str | "b" | |
Builds a range of numbers
| Type | Value |
| Input | Any | any value (is ignored) |
| from (param #1) | Num | lower limit (inclusive) |
| to (param #2) | Num | upper limit (inclusive) |
| Output | Arr<Num...> | an array with the integers in the specified interval |
| Program | Type | Value | Error |
range(0, 4) | Arr<Num...> | [0, 1, 2, 3] | |
range(-1, 2) | Arr<Num...> | [-1, 0, 1] | |
range(3, 2) | Arr<Num...> | [] | |
Concatenates an array with copies of itself.
| Type | Value |
| Input | Arr<<A Any>...> | an array |
| times (param #1) | Num | how many times to repeat the input |
| Output | Arr<<A Any>...> | the elements of the input repeated times times in a single array |
| Program | Type | Value | Error |
[1, 2, 3] repeat(3) | Arr<Num...> | [1, 2, 3, 1, 2, 3, 1, 2, 3] | |
[1, 2, 3] repeat(0) | Arr<Num...> | [] | |
[1, 2, 3] repeat(1) | Arr<Num...> | [1, 2, 3] | |
Reverses an array.
| Type | Value |
| Input | Arr<<A Any>...> | an array |
| Output | Arr<<A Any>...> | the input in reverse order |
| Program | Type | Value | Error |
[1, 2, 3] rev | Arr<Num...> | [3, 2, 1] | |
[] rev | Arr<> | [] | |
Checks if some element is true.
| Type | Value |
| Input | Arr<Bool...> | an array of boolean values |
| Output | Bool | true iff at least one element of the input is true |
| Program | Type | Value | Error |
[] some | Bool | false | |
[false] some | Bool | false | |
[false, false] some | Bool | false | |
[false, true, false] some | Bool | true | |
Sorts numbers.
| Type | Value |
| Input | Arr<Num...> | an array of numbers |
| Output | Arr<Num...> | the input sorted from smallest to greatest |
| Program | Type | Value | Error |
[7, 3, 2, 5, 2] sort | Arr<Num...> | [2, 2, 3, 5, 7] | |
Sorts strings.
| Type | Value |
| Input | Arr<Str...> | an array of strings |
| Output | Arr<Str...> | the input sorted lexicographically |
| Program | Type | Value | Error |
"Zwölf Boxkämpfer jagen Victor quer über den großen Sylter Deich . Voilà !" fields sort | Arr<Str...> | ["!", ".", "Boxkämpfer", "Deich", "Sylter", "Victor", "Voilà", "Zwölf", "den", "großen", "jagen", "quer", "über"] | |
Sorts values.
| Type | Value |
| Input | Arr<<A Any>...> | an array |
| less (param #1) | for <A Any> (<A Any>) Bool | funcer that tests whether its input is "less than" its first argument |
| Output | Arr<<A Any>...> | the input sorted by the given ordering function |
| Program | Type | Value | Error |
[7, 3, 2, 5] sort(>) | Arr<Num...> | [7, 5, 3, 2] | |
"Zwölf Boxkämpfer jagen Victor quer über den großen Sylter Deich . Voilà !" fields sort(>) | Arr<Str...> | ["über", "quer", "jagen", "großen", "den", "Zwölf", "Voilà", "Victor", "Sylter", "Deich", "Boxkämpfer", ".", "!"] | |
[{a: 7}, {a: 3}, {a: 2}, {a: 5}] for Obj<a: Num, Void> def <(other Obj<a: Num, Void>) Bool as @a <(other @a) ok sort(<) | Arr<Obj<a: Num, Void>...> | [{a: 2}, {a: 3}, {a: 5}, {a: 7}] | |
[{a: 7, b: 2}, {a: 3, b: 1}, {a: 2, b: 2}, {a: 5, b: 2}] for Obj<a: Num, b: Num, Void> def <(other Obj<a: Num, b: Num, Void>) Bool as @b <(other @b) ok sort(<) | Arr<Obj<a: Num, b: Num, Void>...> | [{a: 3, b: 1}, {a: 7, b: 2}, {a: 2, b: 2}, {a: 5, b: 2}] | |
Sorts an array with a sorting key.
| Type | Value |
| Input | Arr<<A Any>...> | an array |
| sortKey (param #1) | for <A Any> <B Any> | funcer that maps input elements to values by which they will be compared |
| less (param #2) | for <B Any> (<B Any>) Bool | funcer that tests whether its input is "less than" its first argument |
| Output | Arr<<A Any>...> | the input sorting according to the given sorting key and ordering function |
| Program | Type | Value | Error |
[{a: 7}, {a: 3}, {a: 2}, {a: 5}] sortBy(@a, <) | Arr<Obj<a: Num, Void>...> | [{a: 2}, {a: 3}, {a: 5}, {a: 7}] | |
[{a: 7, b: 2}, {a: 3, b: 1}, {a: 2, b: 2}, {a: 5, b: 2}] sortBy(@b, <) | Arr<Obj<a: Num, b: Num, Void>...> | [{a: 3, b: 1}, {a: 7, b: 2}, {a: 2, b: 2}, {a: 5, b: 2}] | |
Takes the first n elements from an array.
| Type | Value |
| Input | Arr<<A Any>...> | an array |
| n (param #1) | Num | number of elements to take |
| Output | Arr<<A Any>...> | array with the first n elements from the input |
| Program | Type | Value | Error |
[1, 2, 3] take(2) | Arr<Num...> | [1, 2] | |
[1, 2, 3] take(1) | Arr<Num...> | [1] | |
[1, 2, 3] take(0) | Arr<Num...> | [] | |
[1, 2, 3] take(-1) | Arr<Num...> | [] | |
[1, 2, 3] take(4) | Arr<Num...> | [1, 2, 3] | |
[1, 2, 3] take(3) | Arr<Num...> | [1, 2, 3] | |
Takes elements from the beginning of an array that satisfy a certain predicate.
| Type | Value |
| Input | Arr<<A Any>...> | an array |
| predicate (param #1) | for <A Any> Obj<yes: <B Any>, Any>|Obj<no: <C Any>, Any> | a predicate to apply to elements of the input |
| Output | Arr<<B Any>...> | an array with the elements that satisfy the predicate, up to and excluding the first one that doesn't |
| Program | Type | Value | Error |
[1, 3, 5, 2, 4, 7] takeWhile(if %2 ==1) | Arr<Num...> | [1, 3, 5] | |
[1, 3, 5, 2, 4, 7] takeWhile(if %2 ==0) | Arr<Num...> | [] | |
[{a: 1}, {a: 2}, {b: 3}, {a: 4}] takeWhile(is {a: _}) each(@a) | Arr<Num...> | [1, 2] | |
Combines the items of two objects into one.
| Type | Value |
| Input | Obj<Any> | an object |
| other (param #1) | Obj<Any> | another object |
| Output | Obj<Any> | an object with the items of the input and other |
Where the input and other share keys, the values of other are used in the output.
| Program | Type | Value | Error |
{a: 1} +{b: 2} | Obj<> | {a: 1, b: 2} | |
{a: 1, b: 2} +{b: 3} | Obj<> | {a: 1, b: 3} | |
Retrieves the value of a given property.
| Type | Value |
| Input | Obj<<A Any>> | an object |
| prop (param #1) | Str|Num | a property key |
| Output | <A Any> | the value associated in the input object with the given property key |
| Program | Type | Value | Error |
{} get("a") | Void | | {"Code":"VoidProgram","Kind":"Type"} |
{a: 1} get("a") | Num | 1 | |
{a: 1, b: "hey"} get("a") | Num|Str | 1 | |
{a: 1, b: "hey", c: false} get("a") | Num|Str|Bool | 1 | |
{1: "a"} get(1) | Str | "a" | |
{1.5: "a"} get(1.5) | Str | "a" | |
{b: 1} get("a") | Num | | {"Code":"NoSuchProperty","GotValue":"\"a\"","Kind":"Value"} |
Checks for the presence of a given property.
| Type | Value |
| Input | Obj<<A Any>> | an object |
| prop (param #1) | Str|Num | a property key |
| Output | Bool | true if the object has the given property, false if not |
| Program | Type | Value | Error |
{} has("a") | Bool | false | |
{a: 1} has("a") | Bool | true | |
Iterates over the properties together with the values.
| Type | Value |
| Input | Obj<<A Any>> | an object |
| Output | Arr<Arr<Str, <A Any>>...> | an array of tuples of the properties and associated values of the input object |
| Program | Type | Value | Error |
{a: 1, b: 2} items sortBy(@0, <) | Arr<Arr<Str, Num>...> | [["a", 1], ["b", 2]] | |
{} items | Arr<Arr<Str, Void>...> | [] | |
Lists the properties of an object.
| Type | Value |
| Input | Obj<<A Any>> | an object |
| Output | Arr<Str...> | all the property keys of the object, as an array |
| Program | Type | Value | Error |
{a: 1, b: 2} props sort | Arr<Str...> | ["a", "b"] | |
{} props | Arr<Str...> | [] | |
Lists the values of an object.
| Type | Value |
| Input | Obj<<A Any>> | an object |
| Output | Arr<<A Any>...> | all the property values of the object, as an array |
| Program | Type | Value | Error |
{a: 1, b: 2} values sort | Arr<Num...> | [1, 2] | |
{} values | Arr<> | [] | |
Removes a property from an object.
| Type | Value |
| Input | Obj<<A Any>> | an object |
| prop (param #1) | Str | a property key |
| Output | Obj<<A Any>> | the input object, but with the specified property removed |
| Program | Type | Value | Error |
{a: 1, b: 2} without("b") | Obj<Num> | {a: 1} | |
{a: 1, b: 2} without("c") | Obj<Num> | {a: 1, b: 2} | |
Gives the type of the input expression.
| Type | Value |
| Input | <A Any> | any value (is ignored) |
| Output | Str | a string representation of the type of the input expression |
| Program | Type | Value | Error |
type | Str | "Null" | |
null type | Str | "Null" | |
true type | Str | "Bool" | |
1 type | Str | "Num" | |
1.5 type | Str | "Num" | |
"abc" type | Str | "Str" | |
for Any def f Any as null ok f type | Str | "Any" | |
[] type | Str | "Arr<>" | |
["dog", "cat"] type | Str | "Arr<Str, Str>" | |
["dog", 1] type | Str | "Arr<Str, Num>" | |
["dog", 1, {}] type | Str | "Arr<Str, Num, Obj<Void>>" | |
["dog", 1, {}, 2] type | Str | "Arr<Str, Num, Obj<Void>, Num>" | |
{} type | Str | "Obj<Void>" | |
{a: null} type | Str | "Obj<a: Null, Void>" | |
{b: false, a: null} type | Str | "Obj<a: Null, b: Bool, Void>" | |
{c: 0, b: false, a: null} type | Str | "Obj<a: Null, b: Bool, c: Num, Void>" | |
{d: "", c: 0, b: false, a: null} type | Str | "Obj<a: Null, b: Bool, c: Num, d: Str, Void>" | |
{e: [], d: "", c: 0, b: false, a: null} type | Str | "Obj<a: Null, b: Bool, c: Num, d: Str, e: Arr<>, Void>" | |
{f: {}, e: [], d: "", c: 0, b: false, a: null} type | Str | "Obj<a: Null, b: Bool, c: Num, d: Str, e: Arr<>, f: Obj<Void>, Void>" | |
for Num def f Num|Str as if ==1 then 1 else "abc" ok ok 1 f type | Str | "Num|Str" | |
for Any def f Num|Str as 1 ok f type | Str | "Num|Str" | |
for Any def f Void|Num as 1 ok f type | Str | "Num" | |
for Any def f Num|Any as 1 ok f type | Str | "Any" | |
Checks two values for equality.
| Type | Value |
| Input | Any | a value |
| other (param #1) | Any | another value |
| Output | Bool | true if the input is the same as other, false otherwise |
| Program | Type | Value | Error |
null ==null | Bool | true | |
null =={} | Bool | false | |
true ==true | Bool | true | |
true ==false | Bool | false | |
true ==[] | Bool | false | |
1 ==1.0 | Bool | true | |
1 ==2 | Bool | false | |
57 =="a" | Bool | false | |
"abc" =="abc" | Bool | true | |
"" =="abc" | Bool | false | |
"" ==null | Bool | false | |
[false, 1.0, "ab"] ==[false, 1, "a" +"b"] | Bool | true | |
[] ==[11] | Bool | false | |
["a"] =={a: 1} | Bool | false | |
{a: 1, b: 2} =={b: 2, a: 1} | Bool | true | |
{a: 1, b: 2} =={a: 2, b: 1} | Bool | false | |
{} ==[] | Bool | false | |
The identity function.
| Type | Value |
| Input | <A Any> | any value |
| Output | <A Any> | the input value |
| Program | Type | Value | Error |
123 id | Num | 123 | |
"abc" id | Str | "abc" | |
false if id then 1 else fatal ok | Num | "abc" | {"Code":"UnexpectedValue","GotValue":"false","Kind":"Value"} |
Parses the string representation of a floating-point number.
| Type | Value |
| Input | Str | a floating-point number in string representation |
| Output | Num | the corresponding Num value |
| Program | Type | Value | Error |
"4.567" parseFloat | Num | 4.567 | |
"4.567e3" parseFloat | Num | 4567 | |
"4.567abcdefgh" parseFloat | Num | | {"Code":"UnexpectedValue","GotValue":"\"4.567abcdefgh\"","Kind":"Value"} |
Parses the string representation of an integer number.
| Type | Value |
| Input | Str | an integer number in string representation |
| base (param #1) | Num | the base that the input is in |
| Output | Num | the corresponding Num value |
| Program | Type | Value | Error |
"123" parseInt(10) | Num | 123 | |
"ff" parseInt(10) | Num | | {"Code":"UnexpectedValue","GotValue":"\"ff\"","Kind":"Value"} |
"ff" parseInt(16) | Num | 255 | |
"0xFF" parseInt(16) | Num | 255 | {"Code":"UnexpectedValue","GotValue":"\"0xFF\"","Kind":"Value"} |
Parses the string representation of a base-10 integer number.
| Type | Value |
| Input | Str | an integer number in base-10 string representation |
| Output | Num | the corresponding Num value |
| Program | Type | Value | Error |
"123" parseInt | Num | 123 | |
" 123 " parseInt | Num | 123 | {"Code":"UnexpectedValue","GotValue":"\" 123 \"","Kind":"Value"} |
"077" parseInt | Num | 77 | |
"1.9" parseInt | Num | 1 | {"Code":"UnexpectedValue","GotValue":"\"1.9\"","Kind":"Value"} |
"ff" parseInt | Num | 16 | {"Code":"UnexpectedValue","GotValue":"\"ff\"","Kind":"Value"} |
"xyz" parseInt | Num | 16 | {"Code":"UnexpectedValue","GotValue":"\"xyz\"","Kind":"Value"} |
| Type | Value |
| Input | Str | |
| Output | Any | |
| Type | Value |
| Input | Any | |
| Output | Str | |
| Type | Value |
| Input | Arr<Arr<Str, <A Any>>...> | |
| Output | Obj<<A Any>> | |
| Type | Value |
| Input | Any | |
| Output | Str | |
Finds all non-overlapping matches in a string.
| Type | Value |
| Input | Str | a string |
| regexp (param #1) | for Str <A Null|Obj<0: Str, start: Num, Any>> | a regexp |
| Output | Arr<<A Any>...> | array of matches |
Matches appear in the output from leftmost to rightmost. Matches that overlap an earlier match (i.e., a match that starts at a lower offset or one that starts at the same offset but is found earlier by the regexp) are not included.
| Program | Type | Value | Error |
"a" reFindAll~a+~ | Arr<Null|Obj<0: Str, start: Num, Void>...> | [{start: 0, 0: "a"}] | |
"aa" reFindAll~a+~ | Arr<Null|Obj<0: Str, start: Num, Void>...> | [{start: 0, 0: "aa"}] | |
"aba" reFindAll~a+~ | Arr<Null|Obj<0: Str, start: Num, Void>...> | [{start: 0, 0: "a"}, {start: 2, 0: "a"}] | |
Replaces the first match of a regexp in a string with something else.
| Type | Value |
| Input | Str | a string |
| regexp (param #1) | for Str Null|Obj<0: Str, start: Num, Any> | a regexp |
| replacement (param #2) | for Obj<0: Str, start: Num, Any> Str | takes a match and returns a string |
| Output | Str | the input with the first match of the regexp replaced with the corresponding replacement, or unchanged if there is no match |
| Program | Type | Value | Error |
"aba" reReplaceFirst(a+, "c") | Str | "cba" | |
"abc" reReplaceFirst(d, "e") | Str | "abc" | |
"b0b" reReplaceFirst(\d+, @0 parseInt =n "a" repeat(n)) | Str | "bb" | |
"b3b" reReplaceFirst(\d+, @0 parseInt =n "a" repeat(n)) | Str | "baaab" | |
" a b c " reReplaceFirst([abc], "({@0})") | Str | " (a) b c " | |
Replaces all non-overlapping matches of a regexp in a string with something else.
| Type | Value |
| Input | Str | a string |
| regexp (param #1) | for Str <A Null|Obj<0: Str, start: Num, Any>> | a regexp |
| replacement (param #2) | for Obj<0: Str, start: Num, Any> Str | takes a match and returns a string |
| Output | Str | the input with all matches of the regexp replaced with the corresponding replacement, or unchanged if there is no match |
Matches are replaced from leftmost to rightmost. Matches that overlap an earlier match (i.e., a match that starts at a lower offset or one that starts at the same offset but is found earlier by the regexp) are not replaced.
| Program | Type | Value | Error |
"aba" reReplaceAll(a+, "c") | Str | "cbc" | |
"abc" reReplaceAll(d, "e") | Str | "abc" | |
" a b c " reReplaceAll([abc], "({@0})") | Str | " (a) (b) (c) " | |
Splits a string around a regexp.
| Type | Value |
| Input | Str | a string |
| separator (param #1) | for Str Null|Obj<0: Str, start: Num, Any> | a regexp |
| Output | Arr<Str...> | the parts of the input found in between occurrences of the separator |
If the separator regexp matches the empty string, the input is split into its individual code points. Separators are found from leftmost to rightmost. Separators that overlap an earlier separator (i.e., a separator that starts at a lower offset or one that starts at the same offset but is found earlier by the regexp) do not lead to splits.
| Program | Type | Value | Error |
"zabacad" reSplit~a~ | Arr<Str...> | ["z", "b", "c", "d"] | |
"zabaca" reSplit~a~ | Arr<Str...> | ["z", "b", "c", ""] | |
"abacad" reSplit~a~ | Arr<Str...> | ["", "b", "c", "d"] | |
"abaca" reSplit~a~ | Arr<Str...> | ["", "b", "c", ""] | |
"abaca" reSplit~~ | Arr<Str...> | ["a", "b", "a", "c", "a"] | |
"你好" reSplit~~ | Arr<Str...> | ["你", "好"] | |
"" reSplit~a~ | Arr<Str...> | [""] | |
"" reSplit~~ | Arr<Str...> | [] | |
Splits a string around a regexp, up to a certain number of times.
| Type | Value |
| Input | Str | a string |
| separator (param #1) | for Str Null|Obj<0: Str, start: Num, Any> | a regexp |
| n (param #2) | Num | maximum number of splits to make |
| Output | Arr<Str...> | the parts of the input found in between occurrences of the separator |
If the separator regexp matches the empty string, the input is split into its individual code points. Separators are found from leftmost to rightmost. Separators that overlap an earlier separator (i.e., a separator that starts at a lower offset or one that starts at the same offset but is found earlier by the regexp) do not lead to splits. At most n splits are made so that the output contains at most n + 1 elements; later separator occurrences are ignored.
| Program | Type | Value | Error |
"zabacad" reSplit(a, 1) | Arr<Str...> | ["z", "bacad"] | |
"zabaca" reSplit(a, 1) | Arr<Str...> | ["z", "baca"] | |
"zabaca" reSplit(a, 3) | Arr<Str...> | ["z", "b", "c", ""] | |
"zabaca" reSplit(a, 4) | Arr<Str...> | ["z", "b", "c", ""] | |
"abacad" reSplit(a, 1) | Arr<Str...> | ["", "bacad"] | |
"abacad" reSplit(a, 2) | Arr<Str...> | ["", "b", "cad"] | |
"abaca" reSplit(a, 1) | Arr<Str...> | ["", "baca"] | |
"abaca" reSplit(a, 2) | Arr<Str...> | ["", "b", "ca"] | |
"abaca" reSplit(a, 3) | Arr<Str...> | ["", "b", "c", ""] | |
"abaca" reSplit(~~, 2) | Arr<Str...> | ["a", "b", "aca"] | |
"abaca" reSplit(~~, 1000) | Arr<Str...> | ["a", "b", "a", "c", "a"] | |
"你好" reSplit(~~, 0) | Arr<Str...> | ["你好"] | |
"" reSplit(a, 0) | Arr<Str...> | [""] | |
"" reSplit(a, 1) | Arr<Str...> | [""] | |
"" reSplit(~~, 0) | Arr<Str...> | [] | |
"" reSplit(~~, 1) | Arr<Str...> | [] | |
Aborts with an error message
| Type | Value |
| Input | Any | any value |
| Output | Void | does not return |
| Program | Type | Value | Error |
1 if ==1 then fatal else true ok | Bool | | {"Code":"UnexpectedValue","GotValue":"1","Kind":"Value","Pos":"1:15"} |
Aborts with an error message if input is null
| Type | Value |
| Input | Null|<A Any> | a value that you want to be sure is not null |
| Output | <A Any> | the input value, unless it is null |
| Program | Type | Value | Error |
null must | Null | | {"Code":"UnexpectedValue","GotValue":"null","Kind":"Value","Pos":"1:6"} |