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 # Wraps an iterator to skip nulls.
165 # var i: Iterator[Int]
167 # i = new NullSkipper[Int]([null, 1, null, 2, null: nullable Int].iterator)
168 # assert i.to_a == [1, 2]
170 # i = new NullSkipper[Int]([1, null, 2, 3: nullable Int].iterator)
171 # assert i.to_a == [1, 2, 3]
173 class NullSkipper[E
: Object]
176 # The inner iterator.
177 var inner
: Iterator[nullable E
]
179 redef fun finish
do inner
.finish
188 return inner
.item
.as(E
)
196 private fun skip_nulls
do
197 while inner
.is_ok
and inner
.item
== null do inner
.next
201 # Interface that reify a function.
202 # Concrete subclasses must implements the `apply` method.
204 # This interface helps to manipulate function-like objects.
206 # The main usage it as a transformation; that takes an argument and produce a result.
207 # See `map` for example.
209 # Another usage is as a predicate, with `Function[E, Bool]`.
210 # See `Iterator::select` for example.
212 # Function with more than one argument can be reified with some uncurification.
213 # Eg. `Function[ARG1, Function[ARG2, RES]]`.
215 # NOTE: Nit is not a functionnal language, this class is a very basic way to
216 # simulate the reification of a simple function.
217 interface Function[FROM, TO]
218 # How an element is mapped to another one.
219 fun apply
(e
: FROM): TO is abstract
221 # Filter: produce an iterator which each element is transformed.
223 # var i = [1,2,3].iterator
224 # assert fun_to_s.map(i).to_a == ["1", "2", "3"]
226 # Note: because there is no generic method in Nit (yet?),
227 # there is no way to have a better API.
228 # eg. with the Iterator as receiver and the function as argument.
229 # (see `Iterator::select`)
230 fun map
(i
: Iterator[FROM]): Iterator[TO]
232 return new PipeMap[FROM, TO](i
, self)
236 private class FunctionToS
237 super Function[Object, String]
238 redef fun apply
(e
) do return e
.to_s
241 ### Specific private iterator classes
243 private class PipeUniq[E
]
246 var source
: Iterator[E
]
248 var seen
= new HashSet[Object] # FIXME HashSet[E]
250 redef fun is_ok
do return source
.is_ok
252 redef fun item
do return source
.item
256 self.seen
.add
(self.item
.as(Object))
258 while source
.is_ok
and self.seen
.has
(source
.item
.as(Object)) do
264 private class PipeSeqUniq[E
]
267 var source
: Iterator[E
]
269 redef fun is_ok
do return source
.is_ok
271 redef fun item
do return source
.item
277 while source
.is_ok
and seen
== source
.item
do
283 private class PipeJoin[E
]
285 var source1
: Iterator[E
]
286 var source2
: Iterator[E
]
290 return source1
.is_ok
or source2
.is_ok
295 if source1
.is_ok
then return source1
.item
else return source2
.item
300 if source1
.is_ok
then source1
.next
else source2
.next
304 private class PipeAlternate[E
]
307 var source
: Iterator[E
]
311 redef fun is_ok
do return source
.is_ok
331 private class PipeSkip[E
]
334 var source
: Iterator[E
]
341 while source
.is_ok
and source
.item
== skip_item
do source
.next
344 redef fun is_ok
do return source
.is_ok
346 redef fun item
do return source
.item
355 private class PipeHead[E
]
358 var source
: Iterator[E
]
362 redef fun is_ok
do return length
> 0 and source
.is_ok
364 redef fun item
do return source
.item
373 private class PipeSkipTail[E
]
376 var source
: Iterator[E
]
380 var lasts
= new List[E
]
384 var lasts
= self.lasts
385 while source
.is_ok
and lasts
.length
< length
do
386 lasts
.push
(source
.item
)
391 redef fun is_ok
do return source
.is_ok
393 redef fun item
do return lasts
.first
398 lasts
.push
(source
.item
)
403 private class PipeSelect[E
]
406 var source
: Iterator[E
]
408 var predicate
: Function[E
, Bool]
414 while source
.is_ok
and not predicate
.apply
(source
.item
) do source
.next
417 redef fun is_ok
do return source
.is_ok
419 redef fun item
do return source
.item
428 private class PipeMap[E
, F
]
431 var source
: Iterator[E
]
432 var function
: Function[E
, F
]
434 var item_cache
: nullable F
= null
435 var item_cached
= false
437 redef fun is_ok
do return source
.is_ok
440 if item_cached
then return item_cache
441 item_cache
= function
.apply
(source
.item
)
452 # Stateless singleton that reify to the `to_s` method.
454 # assert fun_to_s.apply(5) == "5"
455 fun fun_to_s
: Function[Object, String] do return once
new FunctionToS