foreach(i,ref val; ndim_arr)??

ZombineDev via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue May 10 03:21:30 PDT 2016


On Monday, 9 May 2016 at 18:50:32 UTC, Jay Norwood wrote:
> I noticed some discussion of Cartesian indexes in Julia, where 
> the index is a tuple, along with some discussion of optimizing 
> the index created for cache efficiency.  I could find 
> foreach(ref val, m.byElement()), but didn't find an example 
> that returned a tuple index.   Is that supported?
>
> http://julialang.org/blog/2016/02/iteration
>
> http://julialang.org/blog/2016/03/arrays-iteration

I guess you are talking about ndslice 
(http://dlang.org/phobos/std_experimental_ndslice).

There are various functions that can take static arrays, as well 
as tuples as parameters that are both cache-efficient. For 
example:

http://dlang.org/phobos-prerelease/std_experimental_ndslice_slice.html#.makeSlice and

http://dlang.org/phobos-prerelease/std_experimental_ndslice_slice.html#.Slice.opIndex

I also found the need for a byElement function that also provides 
the index. I guess it is a good idea for a pull request.
In the mean time, you can use the following:

auto indexed_range = lockstep(
     ndslice.byElement,
     ndslice.shape.indexSlice
);

foreach (ref elem; indexes; indexed_range)
     writefln("Element at %s = %s", indexes, elem);


More information about the Digitalmars-d-learn mailing list