Skip to content

Instantly share code, notes, and snippets.

@haampie
Created June 6, 2018 15:24
Show Gist options
  • Save haampie/c4eeb6fa1f6c0a48f44d7f14e010737e to your computer and use it in GitHub Desktop.
Save haampie/c4eeb6fa1f6c0a48f44d7f14e010737e to your computer and use it in GitHub Desktop.
otherzip.jl
module Zipperino
import Base: iterate, tail, @nexprs, @ntuple
using Test, Base.Iterators
struct LinearZip{N,T}
t::T
end
zip(a...) = LinearZip{length(a),typeof(a)}(a)
@generated function iterate(z::LinearZip{N}) where {N}
quote
@nexprs $N j -> begin
y_j = iterate(z.t[j])
y_j === nothing && return nothing
end
values = @ntuple $N j->y_j[1]
states = @ntuple $N j->y_j[2]
values, states
end
end
@generated function iterate(z::LinearZip{N}, states) where {N}
quote
@nexprs $N j -> begin
y_j = iterate(z.t[j], states[j])
y_j === nothing && return nothing
end
values = @ntuple $N j->y_j[1]
states = @ntuple $N j->y_j[2]
values, states
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment