Skip to content

ConditionalCodeEvaluation

DDR0 edited this page Aug 6, 2011 · 1 revision

In FrogattoFormulaLanguage, several functions are available that conditionally evaluate code. These are handy, since code that is not evaluated can't crash. Lines marked '(optionally)' aren't required for the statement, and can be safely left out.

if

if(<this is not false>, 
<do this>, 
(optionally) <otherwise do this.>)

Essentially, the third arg for the if() is what other languages put after elseif statements. However, many else-if statements can gather a lot of close brackets at the end, so sometimes it's better to use a...

switch

switch(<This>, 
<if This is x>, <then do this stuff.>, 
(optionally) <otherwise, if This is y>, <then do this other stuff.>, 
(optionally) <or, if This is z>, <then do this stuff here.>, 
(optionally) ..., ..., (repeat previous line as needed)
(optionally) <but, if This is α>, <then do this stuff.>,
(optionally) <do this stuff if nothing else matched.>)

The switch will crash if it can't return anything. This is very useful for catching out-of-bounds data.

map

map(<this list>, <with each element named this string>,
<for each element in the list, execute this code here>)

map itself returns a list of the results of the last arg. You can map over the returned list, of course. Map neatly eliminates 99% of the use of the next section.

and

<This> and <That>

results in either 0 or 1. It's not actually a function, and so doesn't need commas. However, it deserves mention here because it short-circuiting - if This evaluates to false, then there is no point evaluating That. Since there is no point, That is not evaluated. Since code that is not evaluated can't crash, you can check if the index you're trying to access is actually *in* the list before trying to access it. For example, with a variable-length list, index > 0 and index < size(list) and list[index] = value results in 0 if the index is not equal to value or that element doesn't actually exist. If we just checked list[index], list might not be that long and we'd crash the program.

Clone this wiki locally