How to make generators like this?