Skip to content

Adding recursive generators section #53

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,24 @@ def count(start, step):
>>> next(counter), next(counter), next(counter)
(10, 12, 14)
```
### Recursive generator
* **Generators can be recursive using the yield from statement.**

```python
def depth_first(trie, current_word = ''):
if trie is None:
yield current_word
else:
for letter, new_trie in trie.items():
yield from depth_first(new_trie, current_word + letter)
```

```python
>>> trie={'b': {'a': {'b': {'y': None}, 'd': None,'n': {'k': None}}, 'o': {'x': None}},
... 't': {'e': {'a': None,'d': None,'n': None}, 'o': None}}
>>> [w for w in depth_first(trie)]
['baby', 'bad', 'bank', 'box', 'tea', 'ted', 'ten', 'to']
```

Type
----
Expand Down