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

[stdlib] Add FileHandle.__iter__() with newline as separator #2533

Draft
wants to merge 2 commits into
base: nightly
Choose a base branch
from

Conversation

martinvuyk
Copy link

To add pythonesque functionality

For now it returns a List[String] which doesn't implment the __next__ method so its __iter__ needs to be manually called

var f = open("/tmp/example.txt", "r")
for line in f.__iter__().__iter__():
    print(line[])

needs generators to be able to be called directly once.

The future implementation could look something like

alias eol = String("\n").as_bytes()[0]
var content = self.read_bytes()
var i = 0
while i < content.size:
    var new_i = content[i:].index(eol).or_else(content.size)
    yield StringRef(content[i:new_i])
    i = new_i

Signed-off-by: martinvuyk <[email protected]>
# i = new_i
return self.read().split("\n")

fn __reversed__(self) raises -> List[String]:

This comment was marked as outdated.

@soraros
Copy link
Contributor

soraros commented May 5, 2024

I think we should implement a iterator object like _Listiter instead of using List.

Signed-off-by: martinvuyk <[email protected]>
@martinvuyk
Copy link
Author

martinvuyk commented May 5, 2024

I don't think file should really have a bidirectional iterator.

deleted it

I think we should implement a iterator object like _Listiter instead of using List.

I'm trying to get it to work but I don't know how to "force" the association of the read content to the lifetime of the scope it's called in or the lifetime of the FileHandle itself

fn __iter__[
    mutability: __mlir_type.`i1`, self_life: AnyLifetime[mutability].type
](self) raises -> _FileIter[String, mutability, self_life]:
    """Iterate over lines in the file separated by the
    newline character: \\n.

    Returns:
        An iterator of immutable references to the file lines.

    Examples:

    ```mojo
    var f = open("/tmp/example.txt", "r")
    for line in f:
        print(line[])
    ```
    """
    var items = self.read().split("\n")
    var ref = Reference[List[String], mutability, self_life](items)
    return _FileIter[String, mutability, self_life](0, ref)

@ematejska ematejska added the mojo-repo Tag all issues with this label label May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mojo-repo Tag all issues with this label
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants