Skip to content
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