1.2 code conditionals - RawIron/rawiron.github.io GitHub Wiki
it is a predicate and a block of statements
if <predicate> <block>
| <predicate> <block>
| <predicate> <block>
| otherwise <block>
as an example here is an if statement:
funds = 20
withdraw = 12
if(sufficientFunds) { funds -= withdraw }
replace the block of statements with an anonymous function
myScope = {
funds = 20
withdraw = 12
}
if(sufficientFunds) { &scope => scope.funds -= scope.withdraw }
the scope can contain functions
myScope = {
funds = 20
withdraw(amount) { funds -= amount }
}
if(sufficientFund, &scope => { scope.withdraw(12) })
implementation of the if function:
- the challenge is that if has to work for any type of scope
def (myScope) if(predicate, func) newScope =
when(predicate) = func(myScope)
otherwise = myScope
case <data variable> of <data constructor> <block>
The case statement can have an interesting variation.
case <predicate> of
True <block>
False <block>