Introducing Elixir Maps

Just up on now is my introduction to the Map module and maps, in general. Not exhaustive, but a good intro to most of what you need to get started.

This is projected to be the first of three parts in the series. For part two, I’ll be rewriting some previous Elixir code to move from records to maps. And in the third part, I should be talking about more Map functionality.

Why Perl Is Great

This week’s Accidental Tech Podcast featured two unrelated things that I want to patch together now:

First came an email from a listener who thought John Sirucusa hated everything. Siracusa, in response, provided a list of things he liked.

Near the end of the episode, Siracusa explained to Casey Liss why he’d like Perl. I’m pretty sure the Internet standard laugh about Perl came from Marco Arment at that point.

It’s funny to me how many people look down their nose at Perl while programming in languages that are so influenced by it. (Take Perl’s issues, multiply them by three and hello, PHP!) As I’ve said before, Ruby is just Perl with great object orientation. Perl’s regular expression engine is the standard of the programming world, including Erlang/Elixir.

I remember an episode of Hypercritical where Siracusa gave an all-too short explanation of why Perl is such a great language and Dan Benjamin couldn’t hold back his laughter. (Wish I had the episode number handy…)

People just don’t like the truth, I guess, because their vision of the language is so short-sighted.

So here’s what I suggest: A “Why Perl Is Great” episode of ATP in which Marco and Casey mute their microphones to hide their derisive laughter. I know John might want to prepare for that one, but I also think he could probably give an hour long lecture on this spontaneously. We’ll get him links to back it up afterwards. I want an hour of John pointing out all the Perl modules that did things five or more years before modern languages got credit for creating them. I want John to talk about the Perl Community and how YAPC and Perl Mongers and all the rest led the charge for modern “language communities.”

I’m curious what John thinks about things like “Modern Perl” or MOOSE, in general, also. Is he using OO Perl in his daily work? A specific Perl MVC framework?

What does he think about Perl 6? I’m sure that will get hypercritical, but I’m curious…

Nobody is giving Perl a voice in the podcast world today. The Perlcast has been dead for years now. I’d like to hear Siracusa give it a shot.


C=64 App of the Year

We take a brief break from higher matters of computer science now to bring to you the app of the year for the Commodore 64: Flappy Bird!

This is beyond awesome for a generation of us…

Bonus Double Extra Points for the Hacker News thread linking to the page, which contains this conversation about the difference between loading a program with “,8,1” and just plain “,8”. These are all the details I didn’t understand as a kid using a C=64, but which fascinate me today.

Installing Elixir 0.13

If you want to play with maps in Elixir, you need to install the bleeding edge of both Elixir and Erlang. I did it this weekend. Here’s how I did it.

First, There Shall Be An Erlang

First, you need to install Erlang/OTP 17-RC1. It took some digging, but here’s the link.

(Release Candidate 2 is due out this week, so that link might be outdated by the time you read this. I’ll update this post when RC-2 comes out.)

Go to that directory and unzip it:

    $ tar -xvf otp_src_17.0-rc1.tar.gz

Change into the directory that was just created for you:

    $ cd otp_src_17

— and install Erlang:

    $ ./configure  [options are available, but I didn't use any]
    $ make
    $ make install

You may need to be installing this as root. It will install files to parts of your system your user ID might not have access to.

I’m running OS X Mavericks. I was able to get away with no parameters. There are options you can use during configuration if you need to install things in specific places or if your libraries are elsewhere. Read the documentation in the HOWTO directory to find out.

To test that Erlang is installed, run its REPL, erl:

    $ erl
    Erlang/OTP 17 [RELEASE CANDIDATE 1] [erts-6.0] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]

    Eshell V6.0  (abort with ^G)


And Then There Was Elixir

Now, install Elixir from the v0.13 branch off Github. I’m not an advanced Git user, so I consulted StackOverflow and found the answer to cloning a specific branch of a repository. Pretty simple:

    $ mkdir elixir13
    $ cd elixir13
    $ git clone -b v0.13
    Cloning into 'elixir'...
    remote: Reusing existing pack: 56247, done.
    remote: Counting objects: 339, done.
    remote: Compressing objects: 100% (337/337), done.
    remote: Total 56586 (delta 172), reused 0 (delta 0)
    Receiving objects: 100% (56586/56586), 21.30 MiB | 1.76 MiB/s, done.
    Resolving deltas: 100% (28061/28061), done.
    Checking connectivity... done

(You can do this from any directory you have access to, really. It’s up to you. I’d name it something sane, though.)

That puts all the files into an “elixir” subdirectory, so go one more level deep and let ‘er rip:

    $ cd elixir
    $ make clean test

Mine tested cleanly. The README suggests running make clean and make test if yours has issues.

Spin up the Elixir REPL while you’re there to see where everything is at:

    $ bin/iex

    Erlang/OTP 17 [RELEASE CANDIDATE 1] [erts-6.0] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]

    Interactive Elixir (0.12.5-dev) - press Ctrl+C to exit (type h() ENTER for help)

Text Expander for Me, Maybe BashRC Alias for You?

At this point, though, just running iex would run the old 0.12.3-dev version I had installed first. I think as a temporary fix, I’ll invoke Text Expander and not start remapping things in any dot files. I mapped ;iex and ;elixir to the files of the same semi-colon-less name in the bin directory of Elixir v.0.13. This way it’s only one extra character to get there and I don’t have to think about it again. If I need to find it, I can look it up in Text Expander.

Sure enough, it works and I’m updated. I’ll change directories into a current Elixir project I’m working on and run Mix. (What you see here is how the line looks after Text Expander fills out my ;iex shortcut. Yes, my directory structure is a little haywire now. I can deal with that for a month or so until things are more automated with Homebrew.)

    $ /Users/augiedb/erlang/elixir13/elixir/bin/iex -S mix

    Erlang/OTP 17 [RELEASE CANDIDATE 1] [erts-6.0] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]

    Compiled lib/shuffle_step/supervisor.ex

    lib/shuffle_step.ex:175: using // for default arguments is deprecated, please use \\ instead
    lib/shuffle_step.ex:175: using // for default arguments is deprecated, please use \\ instead
    lib/shuffle_step.ex:201: using // for default arguments is deprecated, please use \\ instead
    lib/shuffle_step.ex:201: using // for default arguments is deprecated, please use \\ instead
    Compiled lib/shuffle_step.ex
    Interactive Elixir (0.12.5-dev) - press Ctrl+C to exit (type h() ENTER for help)

Deprecations ahoy! It’s a good sign that it’s running, though. Let me run the mix test on Exercism’s “Bob” program:

    [AugieDB] ~/exercism2/elixir/bob $ /Users/augiedb/erlang/elixir125/elixir/bin/elixir  bob.exs
    /Users/augiedb/exercism2/elixir/bob/bob.exs:31: warning: using % for sigils is deprecated, please use ~ instead

That was what I expected. So that’s good news!

Now I can start figuring out maps:

    iex(1)> user = %{ :name => "Augie" }
    %{name: "Augie"}

I’ll start by changing my Deck definition from records to maps. Stay tuned for that…

Baseball Processes

Here’s Augie’s Quick And Easy Guide to Running Elixir Processes:

* You need to create a process.
* That process will instantly send back its address (PID).  Remember it.  You'll need it later.
* But that process won't send back a value yet.
* You must then run the process. Pass any info it might need to its PID to get it running.
* When the process is done, it sends the return value back.
* Your main routine will then grab the return value and move along its merry way.

Let’s strain an analogy to the breaking point:

It’s like playing catch in the backyard. You don’t want to play by yourself. So you tell a friend to stand about 20 paces away from you. Once they’re there, they wave their arms to be sure you can see them. Having seen them, you throw them the ball. They throw it back as a solid line drive right at your mitt. End of process.

Unless, of course, your friend is recursive. And wants another ball…

Maybe you want to practice catching pop flies, though. Tell another friend to stand over there somewhere. Call him “La Lob”. Have him let you know where he is. Throw him the ball. He’ll throw it back in a graceful arc. And, assuming you made him to be recursive, he’ll keep standing there, waiting for more, ready to send any balls back from whence they came in a graceful arc.

Wait, you want to field a ground ball? Call up another friend. Tell him you want him to return all throws with a hard throw right at the ground towards your feet. Send him 20 feet away. Then toss him the ball. He’ll turn around and throw it back at your feet.

Good news: You can throw three balls at once. All three catchers will catch the balls and throw them right back at you in three different ways. You’ll pick up that list of balls on the ground and you’ll know which one is which because that catcher’s name will be on their ball.

You could also set it up so the balls lines up in the order they were thrown, in case order is important.

That’s the fun of creating processes. In baseball terms. And a very stretched analogy.

Problem is, you’ve created three different friends. Wouldn’t it be easier if you only needed one friend who could throw a line drive, a pop up, and a ground ball? And when you throw them the ball, you also tell them how you want it thrown back? Sure, you could do that, too. Enumerate over your baseball friends.

Once the team gets too big and you can’t keep track of them all, draft a manager and let him throw the balls for you. You only wanted to catch the ball anyway, right? Give him ten balls and tell him to surprise you with ten different return throws. Hell, let him determine how many friends to create. And if one trips and breaks a leg, tell him to bring in a new friend.

You’re your own supervisor at this point.

And at some point, this all makes the Office of the Commissioner of Major League Baseball into OTP.

Play ball!

Elixir Regexes

I’m up to the OTP chapter in Dave Thomas’s Elixir book now, but am taking a small break to tighten up my Elixir style. I haven’t read enough of other people’s code. I want to learn more of the idiomatic style of Elixir and functional languages, in general.

Among the tricks I have up that particular sleeve is I’ve written about before, but just to recap: You are presented a series of programming challenges with all the tests laid out for you. You can use TDD to develop the program, then upload it and have others comment on it.

In other words, here’s a chance to see a lot of other people’s Elixir code and have mine looked at by others. Exciting.

The first programming exercise, named “Bob”, deals with regular expressions. Great, I think. I’m a Perl programmer. I’ve got regexes in the bag. Yet I’m barely 3 tests into the 12 test exercise, and I’ve already learned a lot about pattern matching, case, cond, and regexes in Elixir.

The first tip to know is this: The latest and greatest releases of Elixir use a tilde (~), not the percent sign. If you’re using 0.12.3 on your computer and the documentation on-line is for 0.12.5-dev, you’re a couple of versions behind and might want to catch up. The percent was needed elsewhere for the soon-to-come map functionality that will wipe records from the face of the language.

For example, what once was this:\w.*/, string)

is now this:\w.*/, string)

One character, a world of difference. This is the fun of learning a language as it’s being developed. You think things are settled, but then a single character change has you spinning in circles for far too long.c

Elixir Katas

Over on YouTube, Nhu Nguyen has posted a few coding katas he’s done with Elixir. They’re the first and only such katas I’ve seen on there so far, and they’re worth a viewing. I’ve learned a couple of nice tricks on TDD in there, as well as a couple Elixir pointers.

As someone with a bowling background, I naturally gravitated to the bowling kata first:

There’s a coin changer and a roman numeral translator you might enjoy in there, too.

My Writings at

While this blog was off-line for a couple months, I picked up a new gig. I’m editing an Elixir blog, Created by Riza Fahmi, the site is a weekly tutorial on some aspect of Elixir coding. They’re usually short and focused, until I step in and start writing something for the site. Then, all bets are off. Here’s a list of the articles I’ve contributed in addition to my normal editing duties:

I do plan on more. I recently finished changing the code of that last project to make it run in multiple processes. I’m exciting to have gotten that code to run, but now I need to figure out how to explain it in a tutorial. Easier said than done…

Is Immutability Just a Fancy Word for Pass-By Value?

I’m new to functional programming. I had an Artificial Intelligence course in college where we did a little Scheme. Our final project was to program something related to the board game “Clue.” As I recall, I did a relatively simple program that made guesses as to the killer’s identity based on the cards in the players hand. Something like that.

That’s been it, really.

But after plunging head first into the world of FP recently, something finally dawned on me:

Isn’t “Immutability” just enforced “Pass By Value” on the part of the programming language? Everything you send out to a function is a copy of the values, not the values themselves. You don’t pass pointers to variables, but rather a copy of the values represented by the variable, right? When the value is returned, “immutability” just states that you can’t set the new value equal to the old value’s variable host name, right?