Skip to content

Commit

Permalink
Allow 1-D Generators to have an optional condition when generating el…
Browse files Browse the repository at this point in the history
…ements
  • Loading branch information
quinnj committed Feb 10, 2016
1 parent c19e917 commit 4bcbcc7
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions base/essentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,27 @@ const (:) = Colon()
immutable Val{T}
end

immutable Generator{F,I}
immutable Generator{F,I,C}
f::F
iter::I
cond::C
end

Generator{F,I}(f::F,iter::I) = Generator(f,iter,x->true)

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
v2 = g.f(v)
while !g.cond(v2) && !done(g.iter, s2)
v, s2 = next(g.iter, s2)
v2 = g.f(v)
end
v2, s2
end

collect(g::Generator) = map(g.f, g.iter)
function collect(g::Generator)
result = map(g.f, g.iter)
filter!(g.cond, result)

This comment has been minimized.

Copy link
@nalimilan

nalimilan Feb 10, 2016

Member

Can't you fall back on the most general collect method? Since next already takes care of checking cond, this sounds like it would be more efficient (no need to allocate result and then filter it).

end

0 comments on commit 4bcbcc7

Please sign in to comment.