Hello!
I heard about Elixir a few months ago (a Honeypot documentary on Elixir), and I got curious. Unfortunately, I have not had a chance to try it out until today.
Today, I finally took the first step in writing a program in Elixir: a way to compute the Shannon entropy of a word or phrase. I think I got the job done, but I wonder in what ways I can improve this implementation.
Perhaps I’m missing some key feature or concept that makes implementing this function more… Elixir-ly? (By the way, is there a term used to describe ideal elixir style in the same way that the Python community has pythonic
?)
defmodule Entropy do
def prepare(input) do
[
input |> String.graphemes |> Enum.frequencies,
input |> String.length
]
end
def calc(input) do
[letter_frequency, input_length] = Entropy.prepare(input)
letter_frequency
|> Map.values
|> Enum.map(fn x -> x / input_length end)
|> Enum.map(fn x -> x * :math.log2(1 / x) end)
|> Enum.sum
end
end