Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iterators #3027

Merged
merged 15 commits into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Types of change:
### Changed
- [Html - Link Relative Paths - Change part of PQ as it wasn't worder properly](https://github.com/enkidevs/curriculum/pull/2985)
- [Python - Format Text Paragraphs With Textwrap - Make the fill method more clear](https://github.com/enkidevs/curriculum/pull/2981)
- [Python - Iterators - Move single-line commands to a single line, update indentation in codeblocks from 4 to 2 spaces](https://github.com/enkidevs/curriculum/pull/3027)

## January 4th 2022

Expand Down
28 changes: 14 additions & 14 deletions python/functional-programming/iterators/the-iteration-protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,22 @@ Iterators are always implemented as classes. Let's examine an iterator's code fo

```python
class Counter(object):
def __init__(self, start, finish):
self.current = start
self.finish = finish

def __iter__(self):
return self

def __next__(self):
if self.current > self.finish:
raise StopIteration
else:
self.current += 1
return self.current - 1
def __init__(self, start, finish):
self.current = start
self.finish = finish

def __iter__(self):
return self

def __next__(self):
if self.current > self.finish:
raise StopIteration
else:
self.current += 1
return self.current - 1
```

We're already familiar with the `iter` and `next` methods. The `init` method is what is called when the iterator is first created, however, it is not a constructor since, the object is already created when the code within `init` is executed. Instead, this is referred to as an initializer.
We're already familiar with the `iter` and `next` methods. The `init` method is what is called when the iterator is first created. However, it is not a constructor since the object is already created when the code within `init` is executed. Instead, this is referred to as an initializer.

In this `Counter` example, we can see that `init` takes the values defined by the creator of the iterator (the start and finish values) and keeps track of them. The `next` method checks to see if the iterator has gone beyond the defined `finish` value, and if not, increases the current value and returns the value before that. If the value has exceeded the `finish` value, a StopIteration exception is raised. Simple!

Expand Down
39 changes: 9 additions & 30 deletions python/functional-programming/iterators/the-itertools-module-ii.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ import itertools;
letters = ['a', 'b', 'c', 'd']
numbers = [1, 2, 3, 4]

print(
list(
itertools.chain(letters, numbers)))
print(list(itertools.chain(letters, numbers)))

# Result = ['a', 'b', 'c', 'd', 1, 2, 3, 4]
```
Expand All @@ -48,10 +46,7 @@ Next, `filterfalse` iterates through a collection of elements, and, given a bool
```python
numbers = [1, 2, 3, 4, 5, 6, 7, 8]

print(
list(
itertools.filterfalse(
lambda x: 2 < x < 7, numbers)))
print(list(itertools.filterfalse(lambda x: 2 < x < 7, numbers)))

# Result = [1, 2, 7, 8]
```
Expand All @@ -62,9 +57,7 @@ Finally, `compress()`, which takes two collections, a and b, and returns only th
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
boolean = [1, 0, 1, 0, 1, 0, 1, 0]

print(
list(
itertools.compress(numbers, boolean)))
print(list(itertools.compress(numbers, boolean)))

# Result: [1, 3, 5, 7]
```
Expand All @@ -86,26 +79,18 @@ discounts = [-30, -100, -35, -85, -15]
isInSale = [1, 0, 1, 1, 1]
salePrices = []

discountIterator =
iter(
itertools.???(
discounts, isInSale))
discountIterator = iter(itertools.???(discounts, isInSale))

fullPricesInSale =
itertools.compress(prices, isInSale)
fullPricesInSale = itertools.compress(prices, isInSale)

def f(x):
price = x + next(discountIterator)
salePrices.append(price)
return(price <= 0)

print(
list(
itertools.???(
lambda x: f(x), fullPricesInSale)))
print(list(itertools.???(lambda x: f(x), fullPricesInSale)))

print(
list(salePrices))
print(list(salePrices))
```

- `compress`
Expand All @@ -125,16 +110,10 @@ What is the result of the following code execution?
```python
import itertools;

names = ['Tom', 'Sadiq', 'Lars',
'Lee', 'Jean']
names = ['Tom', 'Sadiq', 'Lars', 'Lee', 'Jean']
boolean = [1, 0, 1, 1, 0]

print(
list(
itertools.islice(
itertools.cycle(
itertools.compress(
names, boolean)), 0, 6)))
print(list(itertools.islice(itertools.cycle(itertools.compress(names, boolean)), 0, 6)))
```

???
Expand Down