libmir / mir

Mir (backports): Sparse tensors, Hoffman
http://mir.libmir.org
Boost Software License 1.0
209 stars 20 forks source link

cannot infer argument types, expected 1 argument, not 2 in opApply on Slice #379

Closed timotheecour closed 6 years ago

timotheecour commented 8 years ago
void test(){
  import mir.ndslice;
  import std.range;
  auto temp=iota(2*3).sliced();
  foreach(ai;temp){}
  foreach(i,ai;temp){} //Error: cannot infer argument types, expected 1 argument, not 2
}

this should be supported IMO ?

or should we just use:

import std.range:enumerate; foreach(i,ai;temp.enumerate){}

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/39730018-cannot-infer-argument-types-expected-1-argument-not-2-in-opapply-on-slice?utm_campaign=plugin&utm_content=tracker%2F18251717&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F18251717&utm_medium=issues&utm_source=github).
PetarKirov commented 8 years ago

Yes, foreach (idx, elem; ndslice) is not yet supported. See this forum post and the next two for more information on how to do this.

9il commented 8 years ago

Alternatives:

// two integer additions
// per iteration
size_t i;
foreach(ai; temp) { i++; }

Slower alternative:

// two integer additions
// one multiplication
// per iteration
foreach(i; 0..temp.length) {auto ai = temp[i]; }

enumerate can be used too, but I expect it is the slowest way.

If temp has original structure (strides and lengths) then assumeSameStructure can be used, the following way is fast too:

// two integer additions
foreach(tup; assumeSameStructure!("i", "ai")(iotaSlice(temp.shape), temp))
{
    auto i = tup.i;
    auto ai = tup.ai;
}

We can implement them now, but I expect one to create a DIP (D Improvement Proposal) to do it a proper way without workarounds that may be deprecated in the future.

I a interesting if your algorithm can be implemented in terms of mir.ndslice.algorithm? (BTW, starting from 2.072 Mir ndslice is compatible with Phobos ndslice).