RosettaCodeData/Task/Entropy/D/entropy.d

17 lines
352 B
D
Raw Permalink Normal View History

2013-04-10 16:57:12 -07:00
import std.stdio, std.algorithm, std.math;
double entropy(T)(T[] s)
2015-02-20 00:35:01 -05:00
pure nothrow if (__traits(compiles, s.sort())) {
immutable sLen = s.length;
2013-04-10 16:57:12 -07:00
return s
.sort()
.group
2015-02-20 00:35:01 -05:00
.map!(g => g[1] / double(sLen))
2014-04-02 16:56:35 +00:00
.map!(p => -p * p.log2)
.sum;
2013-04-10 16:57:12 -07:00
}
void main() {
"1223334444"d.dup.entropy.writeln;
}