Just realized the mapslices isn't really equivalent, as it assumes each
iteration of the do block will return something. Perhaps there's something
like an "iterslices"?

Trying to use a function that returns nothing in mapslices throws an error:

julia> m = [1 2 3; 4 5 6; 7 8 9; 10 11 12]'
3x4 Array{Int64,2}:
 1  4  7  10
 2  5  8  11
 3  6  9  12

julia> mapslices(m, 2) do v
           print(v)
       end
1
4
7
10
ERROR: no method size(Nothing)
 in mapslices at abstractarray.jl:1619
 in mapslices at abstractarray.jl:1590



On Mon, Feb 10, 2014 at 9:18 PM, Spencer Russell <[email protected]> wrote:

> Often I'm using matrices as collections of vectors, for example each
> column of the matrix is a vector.
>
> If I want to iterate through the vectors, I find myself doing:
>
> for i in 1:size(mat, 2)
>     # do stuff with mat[:, i]
> end
>
> Is there a way to treat the matrix as an iterable, something like
>
> for v in cols(mat)
>     # do stuff with v
> end
>
> or more generally
>
> for v in slices(mat, 2)
>     # do stuff with v
> end
>
> I could use mapslices with the do notation like
>
> mapslices(mat, 2) do v
>     # do stuff with v
> end
>
> but that creates an anon function, which I've heard is somewhat slow, is
> that still correct?
>
> I'm just looking for feedback on what the most Julian thing to do here is,
> as I find I'm doing this quite a bit in my code.
>
> -s
>

Reply via email to