mojo icon indicating copy to clipboard operation
mojo copied to clipboard

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

Open martinvuyk opened this issue 9 months ago • 2 comments

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

martinvuyk avatar May 05 '24 16:05 martinvuyk

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

soraros avatar May 05 '24 16:05 soraros

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)

martinvuyk avatar May 05 '24 17:05 martinvuyk