Iteration and Looping
loop
The loop
recur
construct is a tail call optimised looping expression. It creates a
binding for a set of arguments with a set of initial values. A recusion point is set
by using a recur expression which passes control back to the top of the loop
with a new set of bindings for the arguments.
(loop (result x) ([] 5)
(if (= 0 x)
result
(do
(result.push x)
(recur result --x))))
The example above will return a countdown array starting from 5 "[5, 4, 3, 2, 1]"
each
each
takes an array, an iterator function and an optional context. The iterator is
called for each element of the array with element value, index, array. If context is
provided then "this" inside the iterator function will be the context object.
(each array iterator [(context)])
Example:
(each [1, 2, 3]
(function (elem index list)
(console.log elem)))
each2d
each2d
takes an array whose elements are also arrays (2d array), and calls the
iterator function for each element. The iterator is called with the arguments: value, innerIndex, outerIndex, innerArray, outerArray.
(each2d array iterator)
eachkey
eachkey
iterates over a map (object) and calls the iterator with value, key, object respectively.
(eachKey object (iterator) [context])