Latest blog entries

The Art of Writing a Blogpost

The Art of Writing a Blogpost

Mar 09 2017 : Matias Vera

SpellingCI: No more spelling mistakes in your markdown flies!

Feb 14 2017 : Felipe Ripoll

Fast reverse geocoding with offline-geocoder

Do you need a blazing fast reverse geocoder? Enter offline-geocoder!

Jan 18 2017 : Roberto Romero

Using Jayme to connect to the new MongooseIM REST services

MongooseIM has RESTful services!! Here I show how you can use them in an iOS application.

Dec 13 2016 : Sergio Abraham

20 Questions, or Maybe a Few More

20 Questions, or Maybe a Few More

Nov 16 2016 : Stephanie Goldner

The Power of Meeting People

Because conferences and meetups are not just about the technical stuff.

Nov 01 2016 : Pablo Villar

Finding the right partner for your app build

Sharing some light on how it is to partner with us.

Oct 27 2016 : Inaka

Just Play my Sound

How to easily play a sound in Android

Oct 25 2016 : Giaquinta Emiliano

Opening our Guidelines to the World

We're publishing our work guidelines for the world to see.

Oct 13 2016 : Brujo Benavides

Using NIFs: the easy way

Using niffy to simplify working with NIFs on Erlang

Oct 05 2016 : Hernan Rivas Acosta

Function Naming In Swift 3

How to write clear function signatures, yet expressive, while following Swift 3 API design guidelines.

Sep 16 2016 : Pablo Villar

Jenkins automated tests for Rails

How to automatically trigger rails tests with a Jenkins job

Sep 14 2016 : Demian Sciessere

Erlang REST Server Stack

A description of our usual stack for building REST servers in Erlang

Sep 06 2016 : Brujo Benavides

Replacing JSON when talking to Erlang

Using Erlang's External Term Format

Aug 17 2016 : Hernan Rivas Acosta

Gadget + Lewis = Android Lint CI

Integrating our Android linter with Github's pull requests

Aug 04 2016 : Fernando Ramirez and Euen Lopez

Passwordless login with phoenix

Introducing how to implement passwordless login with phoenix framework

Jul 27 2016 : Thiago Borges

Beam Olympics

Our newest game to test your Beam Skills

Jul 14 2016 : Brujo Benavides


Three Open Source Projects, one App

Jun 28 2016 : Andrés Gerace


Running credo checks for elixir code on your github pull requests

Jun 16 2016 : Alejandro Mataloni

Thoughts on rebar3

Thoughts on rebar3

Jun 08 2016 : Hernán Rivas Acosta

See all Inaka's blog posts >>

Weird List Comprensions in Erlang

A photo of Brujo Benavides wrote this on January 13, 2015 under erlang, inaka .


So, today I found myself writing a typical recursive function…

my_fun([], Acc) -> Acc;
my_fun([Thing|Things], Acc) ->
  NewAcc =
    case check_thing(Thing) of
      {good, ParsedThing} ->
        [ParsedThing || Acc];
      bad -> Acc
  my_fun(Things, NewAcc).

If you spotted the issue with that code, that's awesome! You can skip this paragrah. For other readers: That function always returns [], even if check_thing always returns {good, _}. Why? Because I made a typo and instead of a list constructor I used a List Comprehension here:

{good, ParsedThing} -> [ParsedThing || Acc];

But then I wondered: Why did that even compile in the first place? Instead of reading the documentation (and loosing all the fun in the process) I decided to try things out on a console…


My first assumption was Acc must act as a generator there. And I was amused, since more than once I saw myself writing things like:

1> [ random_thing() || _ <- lists:seq(1, 100)].

And I would love not to have to discard that variable. Let's try it out!

1> [x || []].

Looks good so far…

2> [x || [a]].
3> [x || [a, b]].

Eeeeehm… now it doesn't look right at all. What's going on here?

4> [x || lists:seq(1,3)].
** exception error: bad filter [1,2,3]

Ooooh!!! It's a filter, not a generator!


But what's the meaning of a LC that has no generator? Let's try it out…

5> [x || true].
6> [x || false].

So, If I create a LC with just filters it can either have one element (if the filters are met) or zero elements (otherwise). Interesting! And it works pretty much like andalso in the sense that once a filter returned false no subsequent filter is evaluated… right?

if_long_enough(X) ->
  [X || is_list(X), length(X) > 0].
1> my_mod:if_long_enough(a).
2> my_mod:if_long_enough("x").
3> my_mod:if_long_enough("xx").
4> my_mod:if_long_enough("").

The Docs

Now that we had enough fun, I wanted to see if this feature was documented somewhere. I found nothing about LCs without generators neither on the documentation nor the best Erlang online book ever, where it only states that You can have more than one!


To be fair, it doesn't seem like a really useful feature and I would be skeptical to let a code like this pass a code review, but it's probably a funny thing to play with :) Enjoy it!!


I shouldn't be writing this, but… If you are one of those devs that uses andalso to implement elseless-ifs, like this one:

is_awake(User) andalso say_hello(User).

Then you should know that tools like dialyzer won't be happy with your code unless say_hello returns boolean(). But you can avoid that problem (while keeping your code dirty) by doing:

[say_hello(User) || is_awake(User)].

Which is, in fact, even uglier than the original version, ¯\(ツ)