11 lines
414 B
Plaintext
11 lines
414 B
Plaintext
fcn entropy(text){
|
|
text.pump(Void,fcn(c,freq){ c=c.toAsc(); freq[c]+=1; freq }
|
|
.fp1( (0).pump(256,List,0.0).copy() )) // array[256] of 0.0
|
|
.filter() // remove all zero entries from array
|
|
.apply('/(text.len())) // (num of char)/len
|
|
.apply(fcn(p){-p*p.log()}) // |p*ln(p)|
|
|
.sum(0.0)/(2.0).log(); // sum * ln(e)/ln(2) to convert to log2
|
|
}
|
|
|
|
entropy("1223334444").println(" bits");
|