Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
August 21, 2015 most elegant functional way to make a histogram | ||||
---|---|---|---|---|
| ||||
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. |
August 21, 2015 Re: most elegant functional way to make a histogram | ||||
---|---|---|---|---|
| ||||
Posted in reply to Laeeth Isharc | 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]));
|
August 22, 2015 Re: most elegant functional way to make a histogram | ||||
---|---|---|---|---|
| ||||
Posted in reply to Laeeth Isharc | 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.
|
August 22, 2015 Re: most elegant functional way to make a histogram | ||||
---|---|---|---|---|
| ||||
Posted in reply to BBasile | 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 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.
I believe in your example the compiler will perform TCO so you should be okay.
|
August 24, 2015 Re: most elegant functional way to make a histogram | ||||
---|---|---|---|---|
| ||||
Posted in reply to Laeeth Isharc | 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
|
August 24, 2015 Re: most elegant functional way to make a histogram | ||||
---|---|---|---|---|
| ||||
Posted in reply to yawniek | 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:
>> 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
Thanks v much for suggestions, everyone. In the end I just went with something like the above.
|
Copyright © 1999-2021 by the D Language Foundation