foreach
#define foreach(...)
Iterate over elements in a loop.
iter_init
var iter_init(var self);
var iter_last(var self);
Return the initial item (or final item) in the iteration over self
.
iter_next
var iter_next(var self, var curr);
var iter_prev(var self, var curr);
Given the current item curr
, return the next (or previous) item in the iteration over self
.
iter_type
var iter_type(var self);
Returns the type of item that can be expected to be returned by the iterable.
Usage
var x = new(Array, Int, $I(1), $I(2), $I(5));
foreach(o in x) {
show(o); /* 1, 2, 5 */
}
Table
var prices = new(Table, String, Int);
set(prices, $S("Apple"), $I(12));
set(prices, $S("Banana"), $I( 6));
set(prices, $S("Pear"), $I(55));
foreach(key in prices) {
var price = get(prices, key);
print("Price of %$ is %$\n", key, price);
}
Iterable
The Iter
class is implemented by types which can be looped over. This allows them to be used in conjunction with the foreach
macro as well as various other components of Cello.
To signal that an interation has finished an iteration should return the Cello object Terminal
. Due to this - the Terminal
object cannot be placed inside of Tuples because it artificially shortens their length.
struct Iter {
var (*iter_init)(var);
var (*iter_next)(var, var);
var (*iter_prev)(var, var);
var (*iter_last)(var);
var (*iter_type)(var);
};