dangro

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

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.map apart. You could collapse those to a single one and safe yourself from iterating the list twice for those calculations.
ken-kost

ken-kost

or collapse them all into a single reduce? :japanese_ogre:

|> Enum.reduce(0, &(&2 + (&1 * :math.log2(input_length / &1)) / input_length))

Where Next?

Popular in Questions Top

sergio_101
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
tduccuong
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
Werner
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
polypush135
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
nsuchy
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
vertexbuffer
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
lastday4you
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
freewebwithme
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
makeitrein
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
fireproofsocks
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 Top

Brian
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
senggen
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
axelson
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...
239 45766 226
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
polypush135
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
lk-geimfari
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
mgjohns61585
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
danschultzer
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...
548 27727 240
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
belgoros
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

We're in Beta

About us Mission Statement