-
-
Save mbauman/8826ffc49ff631dbc008 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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