Skip to content

Instantly share code, notes, and snippets.

@mbauman
Created April 16, 2015 14:12
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 mbauman/8826ffc49ff631dbc008 to your computer and use it in GitHub Desktop.
Save mbauman/8826ffc49ff631dbc008 to your computer and use it in GitHub Desktop.
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