Conditional Expressions
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 |
Predicate Expressions
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 | ||
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 |