Entropy
editable example
Click the pencil to open this in an editor, change it, and run it in your browser. The same solution is posted on Rosetta Code.
ghul
use IO.Std.write_line
use Collections.MAP
use Ghul.Pipes
use System.Math
entropy(text: string) -> double is
let counts = MAP[char, int]()
for character in text do
let seen mut = 0
counts.try_get_value(character, seen ref)
counts[character] = seen + 1
od
let total = cast double(text.length)
return counts.values
|> map(count => cast double(count) / total)
|> reduce(0.0D, (bits, probability) =>
bits - probability * Math.log2(probability))
si
let text = "1223334444"
write_line("{text}: {entropy(text)} bits/symbol")
1223334444: 1.8464393446710154 bits/symbol