Skip to content

Instantly share code, notes, and snippets.

@mbauman mbauman/EachDim.jl Secret

Created Apr 16, 2015
Embed
What would you like to do?
julia> immutable EachDim{N, T<:AbstractArray}
A::T
end
Base.call{N}(::Type{EachDim{N}}, A::AbstractArray) = EachDim{N,typeof(A)}(A)
call (generic function with 931 methods)
julia> Base.start(::EachDim) = 1
Base.next(itr::EachDim{1}, s) = (itr.A[s,:], s+1)
Base.next(itr::EachDim{2}, s) = (itr.A[:,s], s+1)
Base.done{N}(itr::EachDim{N}, s) = s > size(itr.A,N)
done (generic function with 48 methods)
julia> AA = reshape(1:20, 4, 5)
4x5 Array{Int64,2}:
1 5 9 13 17
2 6 10 14 18
3 7 11 15 19
4 8 12 16 20
julia> for ii in EachDim{1}(AA)
println(ii)
end
[1 5 9 13 17]
[2 6 10 14 18]
[3 7 11 15 19]
[4 8 12 16 20]
julia> for ii in EachDim{2}(AA)
println(ii)
end
[1,2,3,4]
[5,6,7,8]
[9,10,11,12]
[13,14,15,16]
[17,18,19,20]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.