Comparing Cons

In my other life, I’m a commentator on all sorts of things in the comic book industry. I’ve written the same column every week for something like 17 years now. It means that there are very few things I do in life that I don’t wind up comparing to something in the comics industry.

Last week, while San Diego Comic-Con was happening, I was at two tech conferences, OSCON and ElixirConf. So I wrote a column comparing the two different kinds of tech conferences.

You can read that here, or I’ll spoil the summary for you now:

What do we take away from this? First, that no matter what I’m doing, it all circles back to comics somehow. Second, con badges that don’t turn around and have names printed in large type are awesome. Third, recording con panels is something that should be done. Fourth, power strips are awesome and a lifesaver.

But all of this is helped by bigger ticket prices that the comics community has proven unwilling to pay. In the end, they’re two wildly different communities. That’s not to say we can’t learn from one another. Let’s start with those nametags…

Origin of the Quote

It’s a number that comes up frequently on the internet, in sample code, in slides, in life. Sometimes I worry that people might have forgotten its origins.

From “Hitchhiker’s Guide to the Galaxy”:

“The Answer to the Great Question… Of Life, the Universe and Everything… Is… Forty-two,’ said Deep Thought, with infinite majesty and calm.”

There. Now you’ve read it and not just quoted it. Mind you, the rest of “The Hitchhiker’s Guide to the Galaxy” is well worth reading, too.

This is me killing time while I try to finish off the multiple projects I’ve begun in the wake of ElixirConf… More to come!

Grasping Structs

A blind spot in my Elixir programming experience to date has been structs. I read a lot about them on-line, but didn’t really get why I’d want to use them or how they’re any better. Coincidentally, I started to reread Dave Thomas’ “Programming Elixir” book, which I hadn’t done in about five or six major revisions. A lot has changed in it. He’s written entirely new sections, replaced examples, and a whole lot more.

I happened to trip across the struct section and it finally hit me why they’re cool and useful. They’re like class-specific maps that come with their own names and an easy initial syntax. They are maps that are their own type. It’s pretty cool.

That led me to do what I always do when I learn something new with Elixir: Rewrite my deck of cards routine.

Yes, that old thing.


I’m as sick of it as you are at this point, but it’s handy for learning lots of basic syntax and Elixir theory, so here I go again.

I’m not sure I have much to explain here, so I’ll just throw out the code here and give a couple annotations afterwards.


    defmodule Card do

        defstruct suit: "", rank: "", value: ""

        def describe(c), do: "#{c.rank} of #{c.suit} (#{c.points})"

        def init_(rank) when rank > 1 and rank < 11, do: rank
        def init_points('Ace'), do: 1
        def init_points(_), do: 10


I recognized something incredibly stupid in my original “Card” code. On the “init_points(‘Ace’)” line, I was using a guard clause instead of just pattern matching on the argument. Silly me.

Also, I’m all for writing clear and expressive and verbose syntax, but something drives me nuts about code that names variables the same as the class name. That’s why the describe function’s argument is a “c” instead of “Card.”


    defmodule Deck do

      def new do
        for rank <- ['Ace', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King'],
            suit <- ['Hearts', 'Diamonds', 'Spades', 'Clubs'] 
          %Card{rank: rank, suit: suit, value: Card.init_points(rank)}

      def shuffle(deck) do
        deck |> Enum.shuffle

      def count_suit(deck, suit) do
        Enum.count(deck, fn(x) -> x.suit == suit end)

      def count_rank(deck, rank) do
        Enum.count(deck, fn(x) -> x.rank == rank end)


Remember what I said above and then see where I use “rank: rank, suit: suit” in this code. I am not a consistent person, sometimes. I think it drives me the most nuts in pattern matching, though, where at first glance I can’t be sure if, for example, “rank” were referring to something of a rank type, or a rank, itself. Does that make sense? It’s probably just a style thing I need to get used to and then get over.

Those last two functions are there for future reference and for testing purposes. When my program needs to figure out how many hearts are left in the deck, or how many jacks there still are, I can use that code.

One last word of warning: I once started writing a deck of cards Elixir module. I started it as an experiment in using different data types and syntax construction. Then I realized I was overthinking things to a ridiculous degree, and abandoned it. One of these days, I want to write up why we can something overthink modelling a real world scenario in code, using that dead program as an example.

Elixir: Making Apple Developers Feel At Home

Welcome, all you fancy iOS and Mac programmers. Elixir wants you! Why bother with Swift when Elixir already has the pipeline operator that so many are filing Radars requesting? (Or hack one of your own.)

Here, sit right back and I’ll make you feel at home.

Elixir has structs. You’re C-type folks, so you have to love that. They’re pretty easy to implement:

    $ vi comics.exs

    defmodule Comics do
        defstruct title: "", issue: 1, price: 3.99

To create a comic, just give it whatever details you have, and the defaults will fill in the rest:

    $ iex comics.exs
    Erlang/OTP 17 [erts-6.1] [source] [async-threads:10] [kernel-poll:false]

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

    iex> c1 = %Comics{title: "Asterix", issue: 10, price: 9.95}
    %Comics{issue: 10, price: 9.95, title: "Asterix"}

    iex> %Comics{title: "Asterix", issue: 10, price: price} = c1
    %Comics{issue: 10, price: 9.95, title: "Asterix"}

    iex> price

See? So simple. And pattern matching is awesome. But where, you might ask, does the Objective-C part come in? Watch this:

    defmodule Comics do
            @derive Access
            defstruct title: "", issue: 1, price: 3.99

Wait till I show you what that @derive doo-hicky does!

    $ iex comics.exs

    iex> c1 = %Comics{title: "Asterix", issue: 10, price: 9.95}
    %Comics{issue: 10, price: 9.95, title: "Asterix"}

    iex> c1[:title]

Yes! Elixir has Square Brackets! While Swift moves away from them, you still have a home in Elixir! Welcome!

(And the associative arrays — we call ‘em “maps” — do square brackets, too, but that’s a lesson for another time…)