
# SAI Reference By Example

## Conditionals

	if true
	  debug 'Hello, World.'

	unless false
	  debug 'Hello, World.'

	exists null
	  debug 'Hello, World.'

	count 3 as i
	  if i=2
	    debug 'Third.'
	  else unless i=1
	    debug 'First.'
	  else
	    debug 'Second.'

	count 3 as i
	  switch i
	    default
	      debug 'Third.'
	    case 1
	      debug 'Second.'
	    case 0
	      debug 'First.'

	debug true ?? 'True.' :: 'False.' 

**Generalized syntax**

	if [expr] (as [trial])
	  [code]
	else unless [expr] (as [trial])
	  [code]
	else exists [expr] (as [it])
	  [code]
	else
	  [code]
	 
	switch [expr]
	  case [expr], [expr], [expr]
	    [code] 
	    // does not fall through
	  default
	    [code]
	 
	[expr] ?? [trueval] :: [falseval]

## Iteration and Loops

	set fruits to: // this is our sample data
	  :name 'pear', kcal 57, carb 14, protein 0.3, fat 0.2
	  :name 'banana', kcal 89, carb 23, protein 1.1, fat 0.3
	  :name 'mango', kcal 160, carb 9, protein 2, fat 15 

	ply fruits as fruit
	  debug '100g of ${fruit.name} has ${fruit.kcal} calories.'
	none
	  debug 'There are no fruits.'

	set FruitDetail to task given fruit
	  each fruit as value, key
	    debug key+': '+value
	
	ply fruits using FruitDetail

	count fruits.length as i
	  debug 'Fruit ${i} is ${fruits[i]}'
	none
	  debug 'Why is there no fruit?'

	iterate (fruits sow) as fruit
	  debug '${fruit.name} has ${fruit.carb}g of carbohydrates/100g.'

	set a to 10
	while a > 0
	  dec a
 
**Generalized syntax**

	ply [collection] ( as [it] ) // all list items, 0 to length-1
	ply [collection] using [Task]
	each [collection] ( as [it], [key] ) // all enumerable traits
	each [collection] using [Task]
	count ( [start int] to ) [end int] ( as [key] ) // integer indexing
	count down ( [start int] to ) [end int] ( as [key] )
	iterate [iterable] ( as [it] ) // ES6 iteration
	iterate [iterable] using [Task]
	while [true expr] // old reliable

### PLY - lists/arrays

**Ply** checks the `.length` trait of the subject and visits each numeric key from 0 to length-1.

	ply [list]
	  [code for each item in list]
	none 
	  [code if list is empty]
	
	ply [list] using [task]

### EACH - traits/objects

**Each** visits each enumerable trait of an object in arbitrary order.

	each [traits]
	  [code for each trait]
	none
	  [code if no traits]
	
	each [traits] using [task]

### ITERATE - iteratables

**Iterate** steps through each yielded value of an iterator sequentially.

	iterate [iterable]
	  [code for each iteration]
	none
	  [code if iterator has no output]
	
	iterate [iterable] using [task]

### COUNT - whole numbers

	count ([start expr] to) [stop expr]
	  [code 

## Magic variables

### TRIAL - IF/UNLESS result

	if 2+3
	  debug trial

**Trial** is only valid within the code body of a successful **IF**/**UNLESS** test. _**Trial** is not set by **exists**._

### IT - object/iteration subject

Set within the code block of a successful **exists** test; as well as within the loop of **ply**, **each**, **with**, **iterate**, **thru**, **gather**, **has** and **by**.

	ply list Apple, Banana, Pear
	  debug it

The scoping prefix **.** (dot) is synonymous with `it.` and can be used to directly reference traits of the iteration subject. See the Comprehensions and Scoping Prefix samples for more.

### KEY - iteration index

Set with **each**, **ply**, **count**, **thru**, **gather** and **has**.

\<\>example

### ACCUM - Gather accumulator

Used only with **gather**.

\<\>example

### SELF - Assignment self-reference

Used only within the scope of a single **set** assignment.

\<\>example

### ERROR - Caught exception

Used only within the **catch** code block of an exception handler.

\<\>example

