# SEE: `sort_with` for details
# REQUIRE: self isa Iterator[Comparable]
#
- # [1,3,2].sort_filter #=> [1,2,3]
+ # assert [1,3,2].sort_filter.to_a == [1,2,3]
fun sort_filter: Collection[E]
do
assert self isa Collection[Comparable]
# Important: rely on `==` and `hash`
# Important: require O(m) in memory, where m is the total number of uniq items.
#
- # [1,2,1,1,1,3,2].uniq #=> [1,2,3]
+ # assert [1,2,1,1,1,3,2].uniq.to_a == [1,2,3]
#
# REQUIRE: self isa Iterator[Object]
fun uniq: Collection[E]
#
# Important: rely on `==`.
#
- # [1,2,1,1,1,3,2].seq_uniq #=> [1,2,1,3,2]
+ # assert [1,2,1,1,1,3,2].seq_uniq.to_a == [1,2,1,3,2]
fun seq_uniq: Collection[E]
do
return new PipeSeqUniq[E](self)
# The view of two combined collections.
#
- # ([1..20[ + [20..40[) #=> [1..40[
+ # assert ([1..20[ + [20..40[).to_a == ([1..40[).to_a
fun +(other: Collection[E]): Collection[E]
do
return new PipeJoin[E](self, other)
# Alternate each item with `e`.
#
- # [1,2,3].alternate(0) #=> [1,0,2,0,3]
+ # assert [1,2,3].alternate(0).to_a == [1,0,2,0,3]
fun alternate(e: E): Collection[E]
do
return new PipeAlternate[E](self, e)
# Filter: reject a given `item`.
#
- # [1,1,2,1,3].skip(1) #=> [2,3]
+ # assert [1,1,2,1,3].skip(1).to_a == [2,3]
fun skip(item: E): Collection[E]
do
return new PipeSkip[E](self, item)
# Filter: keep only the first `length` items.
#
- # [1,2,3,4,5].head(2) #=> [1,2]
+ # assert [1,2,3,4,5].head(2).to_a == [1,2]
fun head(length: Int): Collection[E]
do
return new PipeHead[E](self, length)
# Filter: reject the first `length` items.
#
- # [1,2,3,4,5].skip_head(2) #=> [3,4,5]
+ # assert [1,2,3,4,5].skip_head(2).to_a == [3,4,5]
#
# ENSURE: self == return
fun skip_head(length: Int): Collection[E]
# Filter: keep only the last `length` items.
#
- # [1,2,3,4,5].tail(2) #=> [4,5]
+ # assert [1,2,3,4,5].tail(2).to_a == [4,5]
#
# Important: require O(length) in memory
fun tail(length: Int): Collection[E]
# Filter: reject the last `length` items.
#
- # [1,2,3,4,5].skip_tail(2) #=> [1,2,3]
+ # assert [1,2,3,4,5].skip_tail(2).to_a == [1,2,3]
#
# Important: require O(length) in memory
fun skip_tail(length: Int): Collection[E]
### Specific private collection and iterator classes
private class PipeUniq[E]
- super NaiveCollection[E]
+ super Collection[E]
var source: Collection[E]
redef fun iterator do return new PipeUniqIterator[E](source.iterator)
end
end
private class PipeSeqUniq[E]
- super NaiveCollection[E]
+ super Collection[E]
var source: Collection[E]
redef fun iterator do return new PipeSeqUniqIterator[E](source.iterator)
end
end
private class PipeJoin[E]
- super NaiveCollection[E]
+ super Collection[E]
var source1: Collection[E]
var source2: Collection[E]
redef fun iterator do return new PipeJoinIterator[E](source1.iterator, source2.iterator)
end
private class PipeAlternate[E]
- super NaiveCollection[E]
+ super Collection[E]
var source: Collection[E]
var odd_item: E
redef fun iterator do return new PipeAlternateIterator[E](source.iterator, odd_item)
end
private class PipeSkip[E]
- super NaiveCollection[E]
+ super Collection[E]
var source: Collection[E]
var skip_item: E
redef fun iterator do return new PipeSkipIterator[E](source.iterator, skip_item)
end
private class PipeSkipIterator[E]
- super NaiveCollection[E]
+ super Collection[E]
super Iterator[E]
var source: Iterator[E]
end
private class PipeHead[E]
- super NaiveCollection[E]
+ super Collection[E]
var source: Collection[E]
var pipe_length: Int
redef fun iterator do return new PipeHeadIterator[E](source.iterator, pipe_length)
end
private class PipeSkipHead[E]
- super NaiveCollection[E]
+ super Collection[E]
var source: Collection[E]
var pipe_length: Int
redef fun iterator
end
private class PipeTail[E]
- super NaiveCollection[E]
+ super Collection[E]
var source: Collection[E]
var pipe_length: Int
redef fun iterator
end
private class PipeSkipTail[E]
- super NaiveCollection[E]
+ super Collection[E]
var source: Collection[E]
var pipe_length: Int
redef fun iterator do return new PipeSkipTailIterator[E](source.iterator, pipe_length)