nicbet

nicbet

Collection of Elixir Language Idioms

Introduction

Now that the language is picking up support and maturing nicely, I’d like to start a collection of common and recurring Elixir language idioms - recurring expressions or constructs in Elixir code that are not built-in features of the language.

What is a Language Idiom?

Consider this: in English one would usually say "the small black cat", even though grammar rules definitely deem "the black small cat" a fully valid language construct.

When studying a language you may be good at learning the vocabulary (syntax) and catching on with the grammar, but you may fail to properly express yourself in that language. For instance, many English speakers learning Polish struggle trying to use English thoughts and then translating them to Polish. Even though they know all the right translations of those words, and the rules in which to order them, the end result is incomprehensible. Polish is a highly idiomatic language.

In a similar fashion, programming languages tend to have idioms - commonly used patterns that form an implicit agreement of the community on how to describe certain expressions/task/algorithms in a particular programming language.

Language idioms are commonly an indicator of the flexibility of a language - the more flexible a language is and the less rigid the rules are, the more equivalent ways tend to exist in coding tasks, algorithms and data structures that achieve the same result. For instance, languages like Perl, or Scala are sometimes called idiomatic languages - there exist dozens of idioms within these languages, whereas a language like Java does not seem to have many idioms.

Like many functional languages, Elixir has a surprising level of expressive power, allowing for high levels of succinctness in the programs we write. At the same time, it makes learning the language harder for new-comers as Elixir forces us to adopt Elixir’s way of expressing our thoughts and ideas in code.

Why should I care?

There are several benefits in establishing language idioms within a community, and being familiar with language idioms as learners of the language.

  • When reading code that someone else wrote, recognizing several lines of code as an idioms allows the reader to mentally file those lines into a particular corner (“loop iterating over all elements and calculating a sum!”). This allows us to reason and understand code on a more abstract level, without having to carefully read and comprehend each single line of code individually.

  • When writing code on your own, you won’t have to re-invent the wheel. Familiarity with Elixir idioms will allow you to be productive quickly without having to put in too much thought on how to achieve the same re-curring tasks over and over again. (“Uhm, how did I sort that Map last time by key-order?”).

Elixir Language Idiom Collection

Function calls return Tuples indicating Success/Error

  • Functions return tuples based on success status
  • The first element of this tuple is usually :ok on success, and :error on (any kind of) failure.
  • Callers pattern-match against the success and error cases

Example

defmodule HelloPhoenix.RoomChannel do
  use Phoenix.Channel

  def join("room:lobby", _message, socket) do
    {:ok, socket}
  end
  def join("room:" <> _private_room_id, _params, _socket) do
    {:error, %{reason: "unauthorized"}}
  end
end

Other Languages with this idiom

Python, Swift


What other Elixir idioms did you observe in your own code, or in others’ Elixir code so far?

Most Liked

AstonJ

AstonJ

Nice - I’ve made this into a wiki for you :023:

You can find more details about our wikis here: How to post or use our wikis

But in short,

  • Anyone at Trust Level 1 can edit a wiki (wiki owner gets notified of edits)
  • People usually post stuff as normal in the thread - and the wiki owner usually decides whether to include it in the first post (the ‘wiki’). However as mentioned anyone at TL1 can add stuff to the wiki if they feel it should be included.

Any questions just let me know.

nicbet

nicbet

Great suggestion! I’ve expanded the introduction section with some examples.

sashaafm

sashaafm

May I suggest you add a section where you explain what an idiom is? I think it makes it friendlier and more accessible to beginners :slight_smile:

Where Next?

Popular in Wikis Top

swelham
Introduction After some discussion with a few other members we thought it might be an idea to start a thread where we can post about libr...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41454 115
New
gmile
:httpc HTTP client is part of Erlang standard library, and as such can be easily used in Elixir code too. One particular advantage of usi...
New
shavit
To transcode the video there is FFMPEG. On Demand When a user uploads a video, the app renames and copy the file to a path, then call F...
New
anildigital
Here is list of plugins for different editors Sublime Text 3 — Elixir.tmbundle - https://github.com/elixir-editors/elixir-tmbundle/#co...
New
georgeguimaraes
Hi people, since the new year is coming, I’d like to plan my travels for events in 2017. So, what events (Elixir or FP related) that you...
New
Eiji
At start some definitions: HTTPS (is a protocol for secure communication over a computer network which is widely used on the Internet) -...
New
blackode
Hi and Hello Every Elixirian. After Learning the Elixir basics, I struck then. I have no idea of how to put my Elixir Knowledge in pract...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
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 47849 226
New

Other popular topics Top

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
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43591 214
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement