www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Simple casting?

reply Taylor R Hillegeist <taylorh140 gmail.com> writes:
I'm attempting to do a segment group.

details:
alias ProbePoint[3]=triple;
triple[] irqSortedSet = UniqueTriples.keys
				.sort!("a[1].irqid < b[1].irqid",SwapStrategy.stable)
				.array;		
83:triple[][] irqSortedSets = irqSortedSet.chunkBy!((a,b) => 
a[1].irqid == b[1].irqid);	


GetAllTriplesExtractFileIrqSplit.d(83): Error: cannot implicitly 
convert expression `chunkBy(irqSortedSet)` of type 
`ChunkByImpl!(__lambda4, ProbePoint[3][])` to `ProbePoint[3][][]`

I have something that looks like a triple[][] but I can't seem to 
get that type out.
when I add .array it converts to a Group which doesn't make sense 
to me because I'm not using a unary comparison. Any thought?
Nov 25 2019
next sibling parent reply Taylor R Hillegeist <taylorh140 gmail.com> writes:
On Tuesday, 26 November 2019 at 05:05:48 UTC, Taylor R Hillegeist 
wrote:
 I'm attempting to do a segment group.

 details:
 alias ProbePoint[3]=triple;
 triple[] irqSortedSet = UniqueTriples.keys
 				.sort!("a[1].irqid < b[1].irqid",SwapStrategy.stable)
 				.array;		
 83:triple[][] irqSortedSets = irqSortedSet.chunkBy!((a,b) => 
 a[1].irqid == b[1].irqid);	


 GetAllTriplesExtractFileIrqSplit.d(83): Error: cannot 
 implicitly convert expression `chunkBy(irqSortedSet)` of type 
 `ChunkByImpl!(__lambda4, ProbePoint[3][])` to 
 `ProbePoint[3][][]`

 I have something that looks like a triple[][] but I can't seem 
 to get that type out.
 when I add .array it converts to a Group which doesn't make 
 sense to me because I'm not using a unary comparison. Any 
 thought?
a simpler example: import std.algorithm.comparison : equal; import std.array; // Grouping by particular attribute of each element: uint[3][] data = [ [1, 1,0], [1, 2,0], [2, 2,0], [2, 3,0] ]; uint[3][][] r1 = data.chunkBy!((a,b) => a[0] == b[0]); fails in the same way.
Nov 25 2019
parent reply Alex <sascha.orlov gmail.com> writes:
On Tuesday, 26 November 2019 at 05:17:54 UTC, Taylor R Hillegeist 
wrote:
 On Tuesday, 26 November 2019 at 05:05:48 UTC, Taylor R 
 Hillegeist wrote:
 I'm attempting to do a segment group.

 details:
 alias ProbePoint[3]=triple;
 triple[] irqSortedSet = UniqueTriples.keys
 				.sort!("a[1].irqid < b[1].irqid",SwapStrategy.stable)
 				.array;		
 83:triple[][] irqSortedSets = irqSortedSet.chunkBy!((a,b) => 
 a[1].irqid == b[1].irqid);	


 GetAllTriplesExtractFileIrqSplit.d(83): Error: cannot 
 implicitly convert expression `chunkBy(irqSortedSet)` of type 
 `ChunkByImpl!(__lambda4, ProbePoint[3][])` to 
 `ProbePoint[3][][]`

 I have something that looks like a triple[][] but I can't seem 
 to get that type out.
 when I add .array it converts to a Group which doesn't make 
 sense to me because I'm not using a unary comparison. Any 
 thought?
a simpler example: import std.algorithm.comparison : equal; import std.array; // Grouping by particular attribute of each element: uint[3][] data = [ [1, 1,0], [1, 2,0], [2, 2,0], [2, 3,0] ]; uint[3][][] r1 = data.chunkBy!((a,b) => a[0] == b[0]); fails in the same way.
What exactly is the problem, as this works for me if I understood your goal correctly: ´´´ void main() { import std.algorithm.comparison : equal; import std.array; import std; // Grouping by particular attribute of each element: uint[3][] data = [ [1, 1,0], [1, 2,0], [2, 2,0], [2, 3,0] ]; auto r1 = data.chunkBy!((a,b) => a[0] == b[0]); } ´´´ If it is the type of the return value --> the return value of chunkBy has a different one compared to the input. Instead, you get an abstracted range whereas the input data serves as a source.
Nov 25 2019
parent Taylor R Hillegeist <taylorh140 gmail.com> writes:
On Tuesday, 26 November 2019 at 06:45:19 UTC, Alex wrote:
 On Tuesday, 26 November 2019 at 05:17:54 UTC, Taylor R 
 Hillegeist wrote:
 [...]
What exactly is the problem, as this works for me if I understood your goal correctly: ´´´ void main() { import std.algorithm.comparison : equal; import std.array; import std; // Grouping by particular attribute of each element: uint[3][] data = [ [1, 1,0], [1, 2,0], [2, 2,0], [2, 3,0] ]; auto r1 = data.chunkBy!((a,b) => a[0] == b[0]); } ´´´ If it is the type of the return value --> the return value of chunkBy has a different one compared to the input. Instead, you get an abstracted range whereas the input data serves as a source.
I like auto and all. But I wanted the return of the respective type. I can't figure out how to get the type uint[3][][]; that is the type my function takes but I can't figure out how to get it converted.
Nov 26 2019
prev sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 26.11.19 06:05, Taylor R Hillegeist wrote:
 I'm attempting to do a segment group.
 
 details:
 alias ProbePoint[3]=triple;
 triple[] irqSortedSet = UniqueTriples.keys
                  .sort!("a[1].irqid <
b[1].irqid",SwapStrategy.stable)
                  .array;
 83:triple[][] irqSortedSets = irqSortedSet.chunkBy!((a,b) => a[1].irqid 
 == b[1].irqid);
 
 
 GetAllTriplesExtractFileIrqSplit.d(83): Error: cannot implicitly convert 
 expression `chunkBy(irqSortedSet)` of type `ChunkByImpl!(__lambda4, 
 ProbePoint[3][])` to `ProbePoint[3][][]`
 
 I have something that looks like a triple[][] but I can't seem to get 
 that type out.
 when I add .array it converts to a Group which doesn't make sense to me 
 because I'm not using a unary comparison. Any thought?
import std; void main(){ int[] x=[1,1,2,3,4,4]; int[][] y=x.chunkBy!((a,b)=>a==b).map!array.array; writeln(y); }
Nov 26 2019
next sibling parent reply Taylor R Hillegeist <taylorh140 gmail.com> writes:
On Tuesday, 26 November 2019 at 16:33:06 UTC, Timon Gehr wrote:
     int[][] y=x.chunkBy!((a,b)=>a==b).map!array.array;
how did you know to do that?
Nov 26 2019
next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 11/26/19 2:08 PM, Taylor R Hillegeist wrote:> On Tuesday, 26 November 
2019 at 16:33:06 UTC, Timon Gehr wrote:
     int[][] y=x.chunkBy!((a,b)=>a==b).map!array.array;
how did you know to do that?
Ranges don't have elements. They either generate elements according to an algorithm, provide access to elements (or copies of elements) that belong to other containers. In this case, chunkBy() is like an engine that knows how to present the input range in chunks but does not start working automatically. This is a great feature because you can start accessing chunks, deciding it's enough, and stop; potentially avoiding a lot of eager work (potentially infinite). std.array.array pulls all elemenst of a range and places them inside an array. That is eager but sometimes necessary work. For example, std.algorithm.sort cannot sort just any range because it needs the elements to be layed out as array elements: someAlgorithmRange.sort; <-- Does not work someAlgorithmRange.array.sort <-- Works Ali
Nov 26 2019
parent Taylor R Hillegeist <taylorh140 gmail.com> writes:
On Tuesday, 26 November 2019 at 23:29:12 UTC, Ali Çehreli wrote:
 On 11/26/19 2:08 PM, Taylor R Hillegeist wrote:> On Tuesday, 26 
 November 2019 at 16:33:06 UTC, Timon Gehr wrote:
     int[][] y=x.chunkBy!((a,b)=>a==b).map!array.array;
how did you know to do that?
std.array.array pulls all elemenst of a range and places them inside an array. That is eager but sometimes necessary work. For example, std.algorithm.sort cannot sort just any range because it needs the elements to be layed out as array elements: someAlgorithmRange.sort; <-- Does not work someAlgorithmRange.array.sort <-- Works Ali
I suppose I'm asking here how did he know to use: .map!array.array I in my mind I thought that .array would have been enough, it seems like it when looking at the original error: GetAllTriplesExtractFileIrqSplit.d(83): Error: cannot implicitly convert expression `chunkBy(irqSortedSet)` of type `ChunkByImpl!(__lambda4, ProbePoint[3][])` <<Its almost to the final form to `ProbePoint[3][][]` does this have to do with the collection depth?
Nov 26 2019
prev sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 26.11.19 23:08, Taylor R Hillegeist wrote:
 On Tuesday, 26 November 2019 at 16:33:06 UTC, Timon Gehr wrote:
     int[][] y=x.chunkBy!((a,b)=>a==b).map!array.array;
how did you know to do that?
chunkBy with a binary predicate returns a range of ranges. So if I want an array of arrays I have to convert both the inner ranges and the outer range.
Nov 26 2019
prev sibling parent reply ixid <adamsibson gmail.com> writes:
On Tuesday, 26 November 2019 at 16:33:06 UTC, Timon Gehr wrote:
 import std;
 void main(){
     int[] x=[1,1,2,3,4,4];
     int[][] y=x.chunkBy!((a,b)=>a==b).map!array.array;
     writeln(y);
 }
This stuff is a nightmare for less experienced users like myself, I wish there were a single function that would make any data obkect eager, no matter how convoluted its arrays of arrays of arrays.
Nov 27 2019
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 27.11.19 11:43, ixid wrote:
 On Tuesday, 26 November 2019 at 16:33:06 UTC, Timon Gehr wrote:
 import std;
 void main(){
     int[] x=[1,1,2,3,4,4];
     int[][] y=x.chunkBy!((a,b)=>a==b).map!array.array;
     writeln(y);
 }
This stuff is a nightmare for less experienced users like myself, I wish there were a single function that would make any data obkect eager, no matter how convoluted its arrays of arrays of arrays.
import std; auto eager(T)(T r){ static if(isInputRange!T) return r.map!eager.array; else return r; } void main(){ int[] x=[1,1,2,3,4,4]; int[][] y=x.chunkBy!((a,b)=>a==b).eager; writeln(y); }
Nov 27 2019
parent ixid <adamsibson gmail.com> writes:
On Wednesday, 27 November 2019 at 14:40:56 UTC, Timon Gehr wrote:
 On 27.11.19 11:43, ixid wrote:
 On Tuesday, 26 November 2019 at 16:33:06 UTC, Timon Gehr wrote:
 import std;
 void main(){
     int[] x=[1,1,2,3,4,4];
     int[][] y=x.chunkBy!((a,b)=>a==b).map!array.array;
     writeln(y);
 }
This stuff is a nightmare for less experienced users like myself, I wish there were a single function that would make any data obkect eager, no matter how convoluted its arrays of arrays of arrays.
import std; auto eager(T)(T r){ static if(isInputRange!T) return r.map!eager.array; else return r; } void main(){ int[] x=[1,1,2,3,4,4]; int[][] y=x.chunkBy!((a,b)=>a==b).eager; writeln(y); }
Thank you, this is great but it should be in Phobos!
Nov 27 2019