Command Palette

Search for a command to run...

Hectal
Phase 12: Concurrency and Async
Python · Exercise 5 of 6 PreviousNext
Advanced

Transform a token stream with an async generator

words(text) is an async generator that yields the words of a text, one at a time, like a model streaming tokens. Write two things: shout(stream), an async generator that yields each item of another async stream in upper case, and async def collect(stream) that returns all items of an async stream as a list.

The program should print ['HELLO', 'BIG', 'WORLD'].

What Check tests

  1. collect(shout(words(...))) gives the upper-case words
  2. An empty text gives an empty list
  3. shout is an async generator function
  4. shout is lazy: the first item arrives before the stream ends
  5. The program prints the list

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.