Filtering

This section contains functions to filter values during iteration.

fun.filter(predicate, gen, param, state)
iterator:filter(predicate)
Parameters

param ((function(...) -> bool)) – an predicate to filter the iterator

Return a new iterator of those elements that satisfy the predicate.

Examples:

> each(print, filter(function(x) return x % 3 == 0 end, range(10)))
3
6
9

> each(print, take(5, filter(function(i, x) return i % 3 == 0 end,
    enumerate(duplicate('x')))))
3       x
6       x
9       x
12      x
15      x

Note

Multireturn iterators are supported but can cause performance regressions.

See also

take_while() and drop_while().

fun.remove_if(predicate, gen, param, state)
iterator:remove_if(predicate)

An alias for filter().

fun.grep(regexp_or_predicate, gen, param, state)
iterator:grep(regexp_or_predicate)

If regexp_or_predicate is string then the parameter is used as a regular expression to build filtering predicate. Otherwise the function is just an alias for filter().

Equivalent to:

local fun = regexp_or_predicate
if type(regexp_or_predicate) == "string" then
    fun = function(x) return string.find(x, regexp_or_predicate) ~= nil end
end
return filter(fun, gen, param, state)

Examples:

lines_to_grep = {
    [[Emily]],
    [[Chloe]],
    [[Megan]],
    [[Jessica]],
    [[Emma]],
    [[Sarah]],
    [[Elizabeth]],
    [[Sophie]],
    [[Olivia]],
    [[Lauren]]
}

each(print, grep("^Em", lines_to_grep))
--[[test
Emily
Emma
--test]]

each(print, grep("^P", lines_to_grep))
--[[test
--test]]

> each(print, grep(function(x) return x % 3 == 0 end, range(10)))
3
6
9
fun.partition(predicate, gen, param, state)
iterator:partition(predicate)
Parameters

x – a value to find

Returns

{gen1, param1, state1}, {gen2, param2, state2}

The function returns two iterators where elements do and do not satisfy the prediucate. Equivalent to:

return filter(predicate, gen', param', state'),
filter(function(...) return not predicate(...) end, gen, param, state);

The function make a clone of the source iterator. Iterators especially returned in tables to work with zip() and other functions.

Examples:

> each(print, zip(partition(function(i, x) return i % 3 == 0 end, range(10))))
3       1
6       2
9       4

Note

gen, param, state must be pure functional to work properly with the function.

See also

span()