filter
#define filter(I, F)
Construct a Filter
object on the stack over iterable I
with filter function F
.
Usage
var greater_than_two(var x) {
return c_int(x) > 2 ? x : NULL;
}
var x = new(Array, Int, $I(0), $I(5), $I(2), $I(9));
foreach (n in filter(x, $(Function, greater_than_two))) {
show(n); /* 5, 9 */
}
Usage 2
var mem_hello(var x) {
return mem(x, $S("Hello")) ? x : NULL;
}
var x = new(Tuple,
$S("Hello World"), $S("Hello Dan"),
$S("Bonjour"));
var y = new(Tuple);
assign(y, filter(x, $(Function, mem_hello)));
show(y); /* tuple("Hello World", "Hello Dan") */
Filtered Iterable
The Filter
type can be used to filter the results of some iterable. Given a callable object Filter
iterable returns only those items in the original iterable for where calling the function returns a non-NULL
value.
struct Filter {
var iter;
var func;
};
$
alloc
dealloc
assign
cast
cmp
eq
neq
gt
lt
ge
le
copy
hash
hash_data
size
swap
name
brief
description
definition
get
set
mem
rem
key_type
val_type
foreach
iter_init
iter_next
iter_type
new
del
construct
destruct