1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Pipelined filters and operations on iterators.
17 # This module enhances `Iterator` with some methods that enable a pipeline-like programing.
18 # The processing of elements in a pipeline is done trough connected filters that are implemented with reasonable memory constraints.
21 redef interface Iterator[E
]
22 # Filter: sort with `default_comparator`.
23 # SEE: `sort_with` for details
24 # REQUIRE: self isa Iterator[Comparable]
26 # assert [1,3,2].iterator.sort.to_a == [1,2,3]
29 assert self isa Iterator[Comparable]
31 default_comparator
.sort
(a
)
35 # Filter: sort with a given `comparator`.
36 # Important: require O(n) memory.
38 # assert ["a", "c", "b"].iterator.sort_with(alpha_comparator).to_a == ["a", "b", "c"]
39 fun sort_with
(comparator
: Comparator): Iterator[E
]
46 # Filter: reject duplicates.
47 # Elements already seen are rejected.
49 # Important: rely on `==` and `hash`
50 # Important: require O(m) in memory, where m is the total number of uniq items.
52 # assert [1,2,1,1,1,3,2].iterator.uniq.to_a == [1,2,3]
54 # REQUIRE: self isa Iterator[Object]
57 assert self isa Iterator[Object]
58 return new PipeUniq[E
](self)
61 # Filter: reject continuous sequences of duplicates
63 # Important: rely on `==`.
65 # assert [1,2,1,1,1,3,2].iterator.seq_uniq.to_a == [1,2,1,3,2]
66 fun seq_uniq
: Iterator[E
]
68 return new PipeSeqUniq[E
](self)
71 # Combine two iterators.
73 # When the first iterator is terminated, the second is started.
75 # assert ([1..20[.iterator + [20..40[.iterator).to_a == ([1..40[).to_a
76 fun +(other
: Iterator[E
]): Iterator[E
]
78 return new PipeJoin[E
](self, other
)
81 # Alternate each item with `e`.
83 # assert [1,2,3].iterator.alternate(0).to_a == [1,0,2,0,3]
84 fun alternate
(e
: E
): Iterator[E
]
86 return new PipeAlternate[E
](self, e
)
89 # Filter: reject a given `item`.
91 # assert [1,1,2,1,3].iterator.skip(1).to_a == [2,3]
92 fun skip
(item
: E
): Iterator[E
]
94 return new PipeSkip[E
](self, item
)
97 # Filter: keep only the first `length` items.
99 # This filter does not always consume `self'.
101 # var i = [1,2,3,4,5].iterator
102 # assert i.head(2).to_a == [1,2]
103 # assert i.to_a == [3,4,5]
104 fun head
(length
: Int): Iterator[E
]
106 return new PipeHead[E
](self, length
)
109 # Filter: reject the first `length` items.
111 # assert [1,2,3,4,5].iterator.skip_head(2).to_a == [3,4,5]
113 # ENSURE: self == return
114 fun skip_head
(length
: Int): Iterator[E
]
116 while length
> 0 and self.is_ok
do
123 # Filter: keep only the last `length` items.
125 # assert [1,2,3,4,5].iterator.tail(2).to_a == [4,5]
127 # Important: require O(length) in memory
128 fun tail
(length
: Int): Iterator[E
]
130 var lasts
= new List[E
]
132 while lasts
.length
>= length
do lasts
.shift
133 lasts
.push
(self.item
)
136 return lasts
.iterator
139 # Filter: reject the last `length` items.
141 # assert [1,2,3,4,5].iterator.skip_tail(2).to_a == [1,2,3]
143 # Important: require O(length) in memory
144 fun skip_tail
(length
: Int): Iterator[E
]
146 return new PipeSkipTail[E
](self, length
)
149 # Filter: reject items that does not meet some criteria.
151 # class IsEvenFunction
152 # super Function[Int, Bool]
153 # redef fun apply(i) do return i % 2 == 0
155 # assert [1,2,3,4,8].iterator.select(new IsEvenFunction).to_a == [2,4,8]
156 fun select
(predicate
: Function[E
, Bool]): Iterator[E
]
158 return new PipeSelect[E
](self, predicate
)
162 # Interface that reify a function.
163 # Concrete subclasses must implements the `apply` method.
165 # This interface helps to manipulate function-like objects.
167 # The main usage it as a transformation; that takes an argument and produce a result.
168 # See `map` for example.
170 # Another usage is as a predicate, with `Function[E, Bool]`.
171 # See `Iterator::select` for example.
173 # Function with more than one argument can be reified with some uncurification.
174 # Eg. `Function[ARG1, Function[ARG2, RES]]`.
176 # NOTE: Nit is not a functionnal language, this class is a very basic way to
177 # simulate the reification of a simple function.
178 interface Function[FROM, TO]
179 # How an element is mapped to another one.
180 fun apply
(e
: FROM): TO is abstract
182 # Filter: produce an iterator which each element is transformed.
184 # var i = [1,2,3].iterator
185 # assert fun_to_s.map(i).to_a == ["1", "2", "3"]
187 # Note: because there is no generic method in Nit (yet?),
188 # there is no way to have a better API.
189 # eg. with the Iterator as receiver and the function as argument.
190 # (see `Iterator::select`)
191 fun map
(i
: Iterator[FROM]): Iterator[TO]
193 return new PipeMap[FROM, TO](i
, self)
197 private class FunctionToS
198 super Function[Object, String]
199 redef fun apply
(e
) do return e
.to_s
202 ### Specific private iterator classes
204 private class PipeUniq[E
]
207 var source
: Iterator[E
]
209 var seen
= new HashSet[Object] # FIXME HashSet[E]
211 redef fun is_ok
do return source
.is_ok
213 redef fun item
do return source
.item
217 self.seen
.add
(self.item
.as(Object))
219 while source
.is_ok
and self.seen
.has
(source
.item
.as(Object)) do
225 private class PipeSeqUniq[E
]
228 var source
: Iterator[E
]
230 redef fun is_ok
do return source
.is_ok
232 redef fun item
do return source
.item
238 while source
.is_ok
and seen
== source
.item
do
244 private class PipeJoin[E
]
246 var source1
: Iterator[E
]
247 var source2
: Iterator[E
]
251 return source1
.is_ok
or source2
.is_ok
256 if source1
.is_ok
then return source1
.item
else return source2
.item
261 if source1
.is_ok
then source1
.next
else source2
.next
265 private class PipeAlternate[E
]
268 var source
: Iterator[E
]
272 redef fun is_ok
do return source
.is_ok
292 private class PipeSkip[E
]
295 var source
: Iterator[E
]
302 while source
.is_ok
and source
.item
== skip_item
do source
.next
305 redef fun is_ok
do return source
.is_ok
307 redef fun item
do return source
.item
316 private class PipeHead[E
]
319 var source
: Iterator[E
]
323 redef fun is_ok
do return length
> 0 and source
.is_ok
325 redef fun item
do return source
.item
334 private class PipeSkipTail[E
]
337 var source
: Iterator[E
]
341 var lasts
= new List[E
]
345 var lasts
= self.lasts
346 while source
.is_ok
and lasts
.length
< length
do
347 lasts
.push
(source
.item
)
352 redef fun is_ok
do return source
.is_ok
354 redef fun item
do return lasts
.first
359 lasts
.push
(source
.item
)
364 private class PipeSelect[E
]
367 var source
: Iterator[E
]
369 var predicate
: Function[E
, Bool]
375 while source
.is_ok
and not predicate
.apply
(source
.item
) do source
.next
378 redef fun is_ok
do return source
.is_ok
380 redef fun item
do return source
.item
389 private class PipeMap[E
, F
]
392 var source
: Iterator[E
]
393 var function
: Function[E
, F
]
395 var item_cache
: nullable F
= null
396 var item_cached
= false
398 redef fun is_ok
do return source
.is_ok
401 if item_cached
then return item_cache
402 item_cache
= function
.apply
(source
.item
)
413 # Stateless singleton that reify to the `to_s` method.
415 # assert fun_to_s.apply(5) == "5"
416 fun fun_to_s
: Function[Object, String] do return once
new FunctionToS