Skip to content

Instantly share code, notes, and snippets.

@pabloferz
Last active January 31, 2017 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pabloferz/4de498d212b0556d5f98a14f54c72dd9 to your computer and use it in GitHub Desktop.
Save pabloferz/4de498d212b0556d5f98a14f54c72dd9 to your computer and use it in GitHub Desktop.
function unique_recursive(itr)
T = Base._default_eltype(typeof(itr))
out = Vector{T}()
seen = Set{T}()
i = start(itr)
if done(itr, i)
return out
end
x, i = next(itr, i)
if !isleaftype(T)
T = typeof(x)
return unique_recursive(itr, Set{T}(), Vector{T}(), x, i)
end
push!(seen, x)
push!(out, x)
while !done(itr, i)
x, i = next(itr, i)
S = typeof(x)
if !(S === T || S <: T)
T = typejoin(S, T)
return unique_recursive(itr, convert(Set{T}, seen), convert(Vector{T}, out), x, i)
end
if !in(x, seen)
push!(seen, x)
push!(out, x)
end
end
return out
end
function unique_recursive{T}(itr, seen, out::Vector{T}, x, i)
if !in(x, seen)
push!(seen, x)
push!(out, x)
end
while !done(itr, i)
x, i = next(itr, i); S = typeof(x)
if !(S === T || S <: T)
R = typejoin(S, T)
return unique_recursive(itr, convert(Set{R}, seen), convert(Vector{R}, out), x, i)
end
if !in(x, seen)
push!(seen, x)
push!(out, x)
end
end
return out
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment