modularml / mojo

The Mojo Programming Language
https://docs.modular.com/mojo/manual/
Other
22.92k stars 2.58k forks source link

[Feature Request] Better support for generators and iterators #1432

Open gabrieldemarmiesse opened 9 months ago

gabrieldemarmiesse commented 9 months ago

Review Mojo's priorities

What is your request?

Currently, Mojo supports iterators, but it does require them to declare a __len__ method, which is not something found in python. Furthermore, there is no trait associated to an iterator.

Also said in the docs: https://docs.modular.com/mojo/roadmap.html#no-python-style-generator-functions Mojo doesn't support generators yet (with yield), but it's a extremely useful feature when creating libraries (especially the stdlib).

What is your motivation for this change?

I'm trying to replicate the Python's stdlib in Mojo and those features would make this task much much easier. The stdlib has iterators pretty much everywhere.

Any other details?

Congrats on the 0.6.0 which is awesome :)

laszlokindrat commented 9 months ago

Thanks for opening the ticket! We will eventually get to this!

jmc-strider commented 1 week ago

This is something that is a blocker for my team to adopt mojo for some of our python modules.

We will have to stick to Rust until this is implemented but would love to use mojo!

adsharma commented 20 hours ago

Implementing this will also help a python -> mojo transpiler, which could assist in implementing python stdlib in mojo via automated code translation.