Skip to content

Commit cb7d53b

Browse files
committed
Added iterability
1 parent 3834471 commit cb7d53b

3 files changed

Lines changed: 20 additions & 8 deletions

File tree

stream/iterators.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def generate(generator):
2222
@staticmethod
2323
def concat(*iterables):
2424
for iterable in iterables:
25-
for elem in iterable.iterator():
25+
for elem in iterable:
2626
yield elem
2727

2828
"""
@@ -42,7 +42,7 @@ def map(iterable, mapper):
4242
@staticmethod
4343
def flatMap(iterable, mapper):
4444
for elem in iterable:
45-
for internal in mapper(elem).iterator():
45+
for internal in mapper(elem):
4646
yield internal
4747

4848
@staticmethod

stream/stream.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
class Stream():
88

99
"""
10-
A sequence of elements supporting sequential operations.
10+
A sequence of elements supporting sequential operations.
1111
12-
The following example illustrates an aggregate operation using
12+
The following example illustrates an aggregate operation using
1313
Stream:
1414
1515
result = Stream(elements)
@@ -323,13 +323,18 @@ def toSet(self):
323323
'''
324324
return set(self.__iterable)
325325

326-
def iterator(self):
326+
def __iter__(self):
327327
'''
328328
Returns an iterator over the elements in this stream.
329329
330330
:return: the iterator over the elements in this stream
331331
'''
332-
return self.__iterable
332+
return iter(self.__iterable)
333333

334334
def __eq__(self, value):
335+
'''
336+
Check if this stream is equal to the specified stream
337+
338+
:return: True if the streams match, False otherwise
339+
'''
335340
return self.__iterable == value.__iterable

test.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_of(self):
2222

2323
def test_iterate(self):
2424
index = 0
25-
s = Stream.iterate(index, lambda i: i + 1).iterator()
25+
s = Stream.iterate(index, lambda i: i + 1)
2626

2727
for elem in s:
2828
self.assertEqual(elem, index)
@@ -32,7 +32,7 @@ def test_iterate(self):
3232

3333
def test_generate(self):
3434
index = 0
35-
s = Stream.generate(lambda: 1).iterator()
35+
s = Stream.generate(lambda: 1)
3636

3737
for elem in s:
3838
self.assertEqual(elem, 1)
@@ -157,6 +157,13 @@ def test_count(self):
157157
self.assertEqual(Stream.empty().count(), 0)
158158
self.assertEqual(Stream.generate(lambda: 1).limit(10).count(), 10)
159159

160+
def test_iter(self):
161+
index = 1
162+
s = Stream.of(1, 2, 3)
163+
for elem in s:
164+
self.assertEqual(elem, index)
165+
index += 1
166+
160167

161168
if __name__ == '__main__':
162169
unittest.main()

0 commit comments

Comments
 (0)