www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - histogram [last thread contd]

reply "ddos" <oggs gmx.at> writes:
i wrote a histogram algorithm
i tried to write it the shortest and most "D" way possible ... 
please tell me if you see any simpler way of doing it

is there a simpler way of getting the minimum of a range? (by 
intuition i tried range.min)

auto numbers = iota(0,10000).map!(_ => uniform(0.0,1.0)).array;
auto nmin = numbers.reduce!((a,b) => min(a,b));
auto nmax = numbers.reduce!((a,b) => max(a,b)) + double.epsilon;

int bins = 100;
auto bin = iota!float(0, bins).map!(a => 
tuple((nmax-nmin)/bins*a+nmin, (nmax-nmin)/bins*(a+1)+nmin));

auto bincount = bin.map!(a => numbers.map!(b => b >= a[0] && b < 
a[1] ? 1 : 0).sum);

bincount.writeln;
Jan 22 2015
parent "bearophile" <bearophileHUGS lycos.com> writes:
ddos:

 auto numbers = iota(0,10000).map!(_ => uniform(0.0,1.0)).array;
Better: const numbers = 10_000.iota.map!(_ => uniform01).array;
 auto nmin = numbers.reduce!((a,b) => min(a,b));
Better: immutable nMin = numbers.reduce!min; You can also combine both (untested): immutable nMinMax = numbers.reduce!(min, max);
 auto nmax = numbers.reduce!((a,b) => max(a,b)) + double.epsilon;
Bye, bearophile
Jan 22 2015