1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
Filtering
=========
.. module:: fun
This section contains functions to filter values during iteration.
.. function:: filter(predicate, gen, param, state)
iterator:filter(predicate)
:param param: an predicate to filter the iterator
:type param: (function(...) -> bool)
Return a new iterator of those elements that satisfy the **predicate**.
Examples:
.. code-block:: lua
> 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.
.. seealso:: :func:`take_while` and :func:`drop_while`.
.. function:: remove_if(predicate, gen, param, state)
iterator:remove_if(predicate)
An alias for :func:`filter`.
.. function:: 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 :func:`filter`.
Equivalent to:
.. code-block:: lua
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:
.. code-block:: lua
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
.. function:: partition(predicate, gen, param, state)
iterator:partition(predicate)
:param 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:
.. code-block:: lua
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 :func:`zip` and other functions.
Examples:
.. code-block:: lua
> 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.
.. seealso:: :func:`span`
|