www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why doesn't this chain of ndslices work?

reply Stiff <stiff example.com> writes:
Here's the code that doesn't compile:

import std.stdio, std.experimental.ndslice, std.range, 
std.algorithm;

void main()
{
	auto alloslice = [1, 2, 3, 4].sliced(1,4);
	auto sandwich = chain(alloslice,
		(0).repeat(8).sliced(2,4),
		alloslice);
	writeln(sandwich);
}

If I comment out the line with the repeat, or allocate the repeat 
with .array(), everything is fine. I get that the types are 
incompatible in some way, but it seems like I should be able to 
lazily instantiate those zeros whenever I need to (later). Should 
this work? Is there a different way to set up all of the ranges 
without allocating everything up front?

And yeah, resources aren't particularly limited for my 
application, so allocating everything wouldn't hurt, but I'm 
trying to really understand all of these little details about 
ranges. I love them when they work, but the learning curve has 
been steep.
May 14 2016
next sibling parent Seb <seb wilzba.ch> writes:
On Saturday, 14 May 2016 at 21:59:48 UTC, Stiff wrote:
 Here's the code that doesn't compile:

 import std.stdio, std.experimental.ndslice, std.range, 
 std.algorithm;

 void main()
 {
 	auto alloslice = [1, 2, 3, 4].sliced(1,4);
 	auto sandwich = chain(alloslice,
 		(0).repeat(8).sliced(2,4),
 		alloslice);
 	writeln(sandwich);
 }

 If I comment out the line with the repeat, or allocate the 
 repeat with .array(), everything is fine. I get that the types 
 are incompatible in some way, but it seems like I should be 
 able to lazily instantiate those zeros whenever I need to 
 (later). Should this work? Is there a different way to set up 
 all of the ranges without allocating everything up front?

 And yeah, resources aren't particularly limited for my 
 application, so allocating everything wouldn't hurt, but I'm 
 trying to really understand all of these little details about 
 ranges. I love them when they work, but the learning curve has 
 been steep.
Your problem is that the slices don't have the same type. If you allocate the array, the slice has the type int*, whereas iota and repeat are different types - see this example: ``` auto a = iota(1, 8).sliced(2,4); auto b = (0).repeat(8).sliced(2,4); pragma(msg, CommonType!(typeof(a), typeof(b))); // void ``` ``` auto a = iota(1, 8).array.sliced(2,4); auto b = (0).repeat(8).array.sliced(2,4); pragma(msg, CommonType!(typeof(a), typeof(b))); // Slice!(2LU, int*) ```
 Is there a different way to set up all of the ranges without 
 allocating everything up front?
The newest version of mir (dev version of ndslice) supports sparse slices - see e.g.: http://docs.mir.dlang.io/latest/mir_sparse.html I also opened an issue to support chain/concatenate for normal slices (https://github.com/libmir/mir/issues/213). Feel free to post further questions about mir directly on our issue tracker on Github or on the Gitter chat.
May 14 2016
prev sibling parent reply 9il <ilyayaroshenko gmail.com> writes:
On Saturday, 14 May 2016 at 21:59:48 UTC, Stiff wrote:
 Here's the code that doesn't compile:

 import std.stdio, std.experimental.ndslice, std.range, 
 std.algorithm;

 [...]
Coming soon https://github.com/libmir/mir/issues/213#issuecomment-219271447 --Ilya
May 15 2016
parent reply Stiff <stiff example.com> writes:
On Sunday, 15 May 2016 at 08:31:17 UTC, 9il wrote:
 On Saturday, 14 May 2016 at 21:59:48 UTC, Stiff wrote:
 Here's the code that doesn't compile:

 import std.stdio, std.experimental.ndslice, std.range, 
 std.algorithm;

 [...]
Coming soon https://github.com/libmir/mir/issues/213#issuecomment-219271447 --Ilya
Awesome, thanks to both of you. I think I can get what I want from mir.sparse. I was aware of it, but hadn't made the connection to what I'm trying to do for some reason. More generally, I'm eagerly looking forward to a more complete Mir. I've been using bindings to GSL for non-uniform (and quasi-) random number generation, and a cleaner interface would be great. I'll be watching on github.
May 15 2016
parent Ilya Yaroshenko <ilyayaroshenko gmail.com> writes:
On Sunday, 15 May 2016 at 12:30:03 UTC, Stiff wrote:
 On Sunday, 15 May 2016 at 08:31:17 UTC, 9il wrote:
 On Saturday, 14 May 2016 at 21:59:48 UTC, Stiff wrote:
 Here's the code that doesn't compile:

 import std.stdio, std.experimental.ndslice, std.range, 
 std.algorithm;

 [...]
Coming soon https://github.com/libmir/mir/issues/213#issuecomment-219271447 --Ilya
Awesome, thanks to both of you. I think I can get what I want from mir.sparse. I was aware of it, but hadn't made the connection to what I'm trying to do for some reason. More generally, I'm eagerly looking forward to a more complete Mir. I've been using bindings to GSL for non-uniform (and quasi-) random number generation, and a cleaner interface would be great. I'll be watching on github.
Done: Sorry for the huge delay!
Mar 06 2017