dangro
Computing Shannon entropy as my first program in Elixir. What concepts should I learn to improve the implementation?
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
Most Liked
LostKobrakai
Generally this looks fine. Maybe two points for small improvements:
- I’d use a tuple as the return value for
prepare/1. It’s the more ideomatic type for returning two distinct values. - I don’t see a good reason to keep the two
Enum.mapapart. You could collapse those to a single one and safe yourself from iterating the list twice for those calculations.
2
ken-kost
or collapse them all into a single reduce? ![]()
|> Enum.reduce(0, &(&2 + (&1 * :math.log2(input_length / &1)) / input_length))
1
Popular in Questions
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Hi,
is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”:
14:57:30.512 [warn] ...
New
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project.
Baby step #1 is extracting the number ...
New
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Other popular topics
What is the proper way to load a module from a file in to IEX?
In the python world, doing something like this pretty standard:
from ....
New
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!)
This post collects co...
New
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
What is most correct way to open, read and parse JSON file with poison?
For example if we have example.json file in root of some projec...
New
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch.
This project took far...
New
I will often find my self writing things similar to:
case some_value do
nil -> something()
"" -> something()
_ -> someth...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New







