Funcer Call Expressions
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 |
Syntactic Sugar
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" |
Overloading
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 |
Partial Application
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] |