foreach(i,ref val; ndim_arr)??

9il via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue May 10 08:26:22 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

This example is form documentation:
http://dlang.org/phobos/std_experimental_ndslice_selection.html#.byElement

import std.experimental.ndslice.slice;
auto slice = new long[20].sliced(5, 4);

for(auto elems = slice.byElement; !elems.empty; elems.popFront)
{
     size_t[2] index = elems.index;
     elems.front = index[0] * 10 + index[1] * 3;
}
assert(slice ==
     [[ 0,  3,  6,  9],
      [10, 13, 16, 19],
      [20, 23, 26, 29],
      [30, 33, 36, 39],
      [40, 43, 46, 49]]);

ndslice does not have opApply, because it overrides range 
primitives.
Iteration with byElement may be a little bit slower then 
iteration with common foreach loops. A method, say, `forElement` 
for `foreach` loops may be added, thought.

Best regards,
llya



More information about the Digitalmars-d-learn mailing list