Skip to content

Commit 051cb97

Browse files
committed
Merge branch '__rultor'
2 parents 9a3facd + 7b14709 commit 051cb97

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

lib/fromarray.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ def map(&function)
7070
FromArray.new(mapped)
7171
end
7272

73+
# Remove all the duplicates from the stream.
74+
def distinct
75+
unique = []
76+
@array.each do |val|
77+
unique.push(val) unless unique.include? val
78+
end
79+
FromArray.new(unique)
80+
end
81+
7382
# Skip the first n elements of the stream.
7483
# +count+:: Number of elements to skip from the beginning of the stream.
7584
def skip(count)

test/fromarray_test.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,5 +197,37 @@ def test_maps_no_elements
197197
collected = stream.map { |val| val.to_s }.collect
198198
assert(collected.empty?)
199199
end
200+
201+
# FromArray.distinct removes all duplicates.
202+
def test_removes_duplicates
203+
stream = FromArray.new([2, 2, 3, 4, 1, 1, 2, 5, 4, 3, 6])
204+
collected = stream.distinct.collect
205+
assert(collected == collected.uniq)
206+
assert(collected.length == collected.uniq.length)
207+
end
208+
209+
# FromArray.distinct works when the stream is empty.
210+
def test_empty_distinct
211+
stream = FromArray.new([])
212+
collected = stream.distinct.collect
213+
assert(collected.length.zero?)
214+
end
215+
216+
# FromArray.distinct works when there are no duplicates in the array.
217+
def test_distinct_no_duplicates
218+
stream = FromArray.new([1, 2, 3, 4, 5])
219+
collected = stream.distinct.collect
220+
assert(collected == collected.uniq)
221+
assert(collected.length == collected.uniq.length)
222+
end
223+
224+
# FromArray.distinct works when there is only 1 element with
225+
# its duplicate
226+
def test_distinct_one_duplicate_element
227+
stream = FromArray.new([1, 1])
228+
collected = stream.distinct.collect
229+
assert(collected.length == 1)
230+
assert(collected == collected.uniq)
231+
end
200232
end
201233
end

0 commit comments

Comments
 (0)