map
#define map(I, F)
Construct a Map
object on the stack over iterable I
applying function F
.
Usage
var convert_to_int(var x) {
var y = new(Int);
look_from(y, x, 0);
return y;
}
var x = tuple($S("1"), $S("2"), $S("3"));
foreach (y in map(x, $(Function, convert_to_int))) {
show(y); /* 1, 2, 3 */
};
Usage 2
var print_object(var x) {
println("Object %$ is of type %$", x, type_of(x));
return NULL;
}
var x = tuple($I(0), $S("Hello!"), $F(2.4));
call(map(x, $(Function, print_object)));
Apply Function to Iterable
The Map
type is an iterable that applies some callable to to each item in another iterable and returns the result. This can be useful to make more concise iteration when there are callback functions available.
If the mapping callable is a purely side-effect callable it is possible to use the call
function on the Map
object directly for a quick way to perform the iteration.
One downside of Map
is that the iter_type
becomes unknown (there is no way to know what type the callable will return so some objects such as Array
s may revert to using Ref
as the object type when assigned a Map
.
struct Map {
var iter;
var curr;
var func;
};
$
alloc
dealloc
assign
cast
cmp
eq
neq
gt
lt
ge
le
copy
hash
hash_data
size
swap
call
name
brief
description
definition
get
set
mem
rem
key_type
val_type
foreach
iter_init
iter_next
iter_type
len
new
del
construct
destruct