Code you love - share yours!

Thought this would be a nice way to start the year - have you seen or written an Erlang or BEAM language function or piece of code that you are particularly fond of? Perhaps you admired its simplicity (or complexity!) or maybe your surprised yourself or surpassed your own expectations of what is possible?

Whatever the reason, please share! :blush:

(If sharing code that is not in Erlang, please state which BEAM language it is)

4 Likes

I have recently written a bit of code for ReVault:

This isn’t particularly fancy or impressive code (and the module at large is full of TODOs), but I was real glad to be able to use maybe expressions in my own projects in a way that simplified the validation workflow a bunch.

The long time and lot of community work behind getting that feature into the language is part of the feeling, of course.

7 Likes

I have mine:

The config data structure is a TOML tree, stored in a persistent_term. Those few lines of code are all I need to traverse the tree by a path and get the desired subtree or final leaf. Love the simplicity :smile:

3 Likes

One of my favorites last year was:

Used counters and persistent terms to implement a replacement metrics system (counters, gauges, histograms) for Apache CouchDB. It also removed a significant concurrency bottleneck.

The histogram implementation was the most interesting I thought:

2 Likes

Here’s a little binary log parser that I made a long time ago. I often refer to it when telling other people about how powerful the binary notation (and pattern matching) is in Erlang. I mostly love just how compact and readable it is.

2 Likes

I really like Joe’s “Universal Server”:

universal_server() ->
    receive
       {become, F} ->
           F()
    end.

It’s not exactly complicated to understand but for me the way Joe takes this simple function in his post sells it for me :slight_smile: And using this simple function, you can introduce people to some concepts of Erlang but also to change the way some people think about processes and Erlang systems as a whole: Joe wrote this post about Erlang / Elixir web servers where he said that “We do not have ONE web-server handling 2 millions sessions. We have 2 million webservers handling one session each.”. That is what really made processes click for me :slight_smile:

4 Likes