digitalmars.D.learn - most elegant functional way to make a histogram
- Laeeth Isharc (17/17) Aug 21 2015 I have four arrays of ints, each array representing a kind of
- Laeeth Isharc (4/21) Aug 21 2015 I guess this kind of thing will do:
- yawniek (7/9) Aug 24 2015
- Laeeth Isharc (3/12) Aug 24 2015 Thanks v much for suggestions, everyone. In the end I just went
- BBasile (23/41) Aug 21 2015 loop-less approach, it consumes an InputRange in a recursive
- Meta (3/51) Aug 21 2015 I believe in your example the compiler will perform TCO so you
I have four arrays of ints, each array representing a kind of event associated with that int (they all map to the same space). Each array might have the same number multiple times and each array will be of different length. So I would like to plot the int line on x axis and show number of times that the number occurs for each array (4 bars for each int). It's easy to do with loops, but what's best functional/algorithmic way, please? Brain tired today. I am trying to use these opportunities to learn the algorithmic way even if loop is more natural. I could just make four new arrays of ints and use each instead of a loop. Any better way? Also, for bucketizing, any thoughts on best way to do using phobos? (Cos probably I have too many ints and need to bracket them to plot a histogram). Sorry if this is unclear. Laeeth.
Aug 21 2015
On Friday, 21 August 2015 at 20:09:22 UTC, Laeeth Isharc wrote:I have four arrays of ints, each array representing a kind of event associated with that int (they all map to the same space). Each array might have the same number multiple times and each array will be of different length. So I would like to plot the int line on x axis and show number of times that the number occurs for each array (4 bars for each int). It's easy to do with loops, but what's best functional/algorithmic way, please? Brain tired today. I am trying to use these opportunities to learn the algorithmic way even if loop is more natural. I could just make four new arrays of ints and use each instead of a loop. Any better way? Also, for bucketizing, any thoughts on best way to do using phobos? (Cos probably I have too many ints and need to bracket them to plot a histogram). Sorry if this is unclear.I guess this kind of thing will do: upRangeHighs.each!((ref a)=>(++histogram[a][0]));
Aug 21 2015
On Friday, 21 August 2015 at 21:08:25 UTC, Laeeth Isharc wrote:I guess this kind of thing will do: upRangeHighs.each!((ref a)=>(++histogram[a][0]));int[] arr = [5,1,2,2,3,4,5,5,5]; int[int] histo; arr.each!( a => ++histo[a] ); writeln(histo); this works
Aug 24 2015
On Monday, 24 August 2015 at 09:50:41 UTC, yawniek wrote:On Friday, 21 August 2015 at 21:08:25 UTC, Laeeth Isharc wrote:Thanks v much for suggestions, everyone. In the end I just went with something like the above.I guess this kind of thing will do: upRangeHighs.each!((ref a)=>(++histogram[a][0]));int[] arr = [5,1,2,2,3,4,5,5,5]; int[int] histo; arr.each!( a => ++histo[a] ); writeln(histo); this works
Aug 24 2015
On Friday, 21 August 2015 at 20:09:22 UTC, Laeeth Isharc wrote:I have four arrays of ints, each array representing a kind of event associated with that int (they all map to the same space). Each array might have the same number multiple times and each array will be of different length. So I would like to plot the int line on x axis and show number of times that the number occurs for each array (4 bars for each int). It's easy to do with loops, but what's best functional/algorithmic way, please? Brain tired today. I am trying to use these opportunities to learn the algorithmic way even if loop is more natural. I could just make four new arrays of ints and use each instead of a loop. Any better way? Also, for bucketizing, any thoughts on best way to do using phobos? (Cos probably I have too many ints and need to bracket them to plot a histogram). Sorry if this is unclear. Laeeth.loop-less approach, it consumes an InputRange in a recursive function. Assuming histogramData is a custom InputRange: --- void upperLevel() { //histogramData = ... proc(histogramData); // continue once the range is consumed } void proc(ref histogramData) { //something with front histogramData.popFront; if (!histogramData.empty) proc(histogramData); } --- I don't know if this approach can be used but this is an alternative to loops. Not necessarily the best because local variables in proc() can lead to a stack overflow.
Aug 21 2015
On Saturday, 22 August 2015 at 02:12:41 UTC, BBasile wrote:On Friday, 21 August 2015 at 20:09:22 UTC, Laeeth Isharc wrote:I believe in your example the compiler will perform TCO so you should be okay.I have four arrays of ints, each array representing a kind of event associated with that int (they all map to the same space). Each array might have the same number multiple times and each array will be of different length. So I would like to plot the int line on x axis and show number of times that the number occurs for each array (4 bars for each int). It's easy to do with loops, but what's best functional/algorithmic way, please? Brain tired today. I am trying to use these opportunities to learn the algorithmic way even if loop is more natural. I could just make four new arrays of ints and use each instead of a loop. Any better way? Also, for bucketizing, any thoughts on best way to do using phobos? (Cos probably I have too many ints and need to bracket them to plot a histogram). Sorry if this is unclear. Laeeth.loop-less approach, it consumes an InputRange in a recursive function. Assuming histogramData is a custom InputRange: --- void upperLevel() { //histogramData = ... proc(histogramData); // continue once the range is consumed } void proc(ref histogramData) { //something with front histogramData.popFront; if (!histogramData.empty) proc(histogramData); } --- I don't know if this approach can be used but this is an alternative to loops. Not necessarily the best because local variables in proc() can lead to a stack overflow.
Aug 21 2015