swh:1:snp:a72e953ecd624a7df6e6196bbdd05851996c5e40
Raw File
Tip revision: 1f101b3a169670e6c6735cbbd53952519bae1ee9 authored by Tim Holy on 06 March 2016, 13:54:26 UTC
Add shareindexes macro for faster iteration
Tip revision: 1f101b3
generator.jl
"""
    Generator(f, iter)

Given a function `f` and an iterator `iter`, construct an iterator that yields
the values of `f` applied to the elements of `iter`.
The syntax `f(x) for x in iter` is syntax for constructing an instance of this
type.
"""
immutable Generator{I,F}
    f::F
    iter::I
end

start(g::Generator) = start(g.iter)
done(g::Generator, s) = done(g.iter, s)
function next(g::Generator, s)
    v, s2 = next(g.iter, s)
    g.f(v), s2
end

collect(g::Generator) = map(g.f, g.iter)
back to top