Uli Fahrenberg
Uli Fahrenberg

Reputation: 123

Julia iterator which parses each line in file

I'm new to Julia and have hit a rock with something I imagine should be a common scenario:

I would like an iterator which parses a text file one line at a time. So, like eachline(f), except a function parse is applied to each line. Call the result eachline(parse, f) if you wish (like the version of open with an extra function argument): mapping parse over eachline.

To be more specific: I have a function poset(s::String) which turns a string representation of a poset into a poset:

function poset(s::String)
    nums = split(s)
    ...
    return transitiveclosure(g)
end

Now I'd like to be able to say something like

open("posets7.txt") do f
    for p in eachline(poset, f)
        #something
    end
end

(and I specifically need this to be an iterator: my files are rather large, so I really want to parse them one line at a time).

Upvotes: 2

Views: 355

Answers (1)

I think I would (personally) use for Iterators.map in such a case:

open("posets7.txt") do f
    for p in Iterators.map(poset, eachline(f))
        # do something
    end
end

But, as the documentation notes (and as you discovered yourself), this is equivalent to using a generator expression:

open("posets7.txt") do f
    for p in (poset(x) for x in eachline(f))
        # do something
    end
end

Upvotes: 3

Related Questions