Installing Elixir on Ubuntu via Vagrant on Windows

Installing Elixir, Hiding Windows

I’m heading off to OSCon and Elixir Conference next month. These will be my first tech conferences ever, and I’m very excited.

One small program is that I don’t have a laptop of my own anymore. My old Mac laptop (pre-unibody construction) failed last year. My only choice is to bring my work machine, which is a Thinkpad Windows box.

So as not to be the laughing stock of either conference, particularly OSCon, I decided to install Vagrant and run that at full screen size at all times and pretend I have a Linux box. Also, this gives me Elixir access at work, where I plan to someday introduce the language.

For posterity’s sake, here’s how I did it:

Warning: This is something of a down and dirty guide. For the sake of a temporary installation, it works. If I were setting up a new server that I’d keep up permanently in the long term for production purposes, I’d likely make very different decisions.I wouldn’t take some of the shortcuts I take here.

First, install Vagrant. That’s outside the scope of this write up, but it’s very easy. Follow the link for details.

I installed the basic Ubuntu machine that the on-line docs use as their “Get Started” example. Once I set up Putty to be my client to ssh into my new Vagrant machine, I got to work on installing Erlang and Elixir.

Start with Erlang, Of Course

First, you’ll need some basic tools to set up Erlang. Install a bunch:

sudo apt-get install build-essential libncurses5-dev openssl libssl-dev fop xsltproc

This will give you little things you’ll need, like make. I still can’t believe the default Ubuntu box doesn’t come with that, but who am I to judge?

Now, bring down the latest Erlang source code. As I write this, it’s at 17.0:

wget http://erlang.org/download/otp_src_17.0.tar.gz

Then unzip it:

tar zxvf otp_src_R17.0.tar.gz

Change directories and install away:

cd otp_src_R17.0
./configure && make && sudo make install

If the box still complains that MAKE isn’t installed, try this:

sudo apt-get install build-essential 

That ought to take care of it. Then try again.

Go to the Erlang REPL to make sure it’s installed:

$ erl
Erlang/OTP 17 [erts-6.0] [source] [async-threads:10] [kernel-poll:false]

Eshell V6.0  (abort with ^G)
1>

Success!

Time for Elixir

I went back to my home directory and downloaded the latest and greatest version of Elixir. In this case, that’s Elixir 0.14.1-dev:

cd
wget https://github.com/elixir-lang/elixir/archive/master.zip

This is when I discovered the hard way that Ubuntu also doesn’t come with zip installed, so:

sudo apt-get install unzip

Elixir relies on UTF8. Go ahead and type ‘locale’ to see what locale your box is set to. The Vagrant Ubuntu box is not set up as UTF8, which will lead to a bunch of installation errors. So let’s cover our tracks here first. I went a little overboard, probably, but I did this to set it up for my vagrant user, who is the only user who will ever use this system:

vi /home/vagrant/.pam_environment

And put this in that file:

LANG="en_US.utf8"
LANGUAGE="en_US.utf8"
LC_CTYPE="en_US.utf8"
LC_NUMERIC="en_US.utf8"
LC_TIME="en_US.utf8"
LC_COLLATE="en_US.utf8"
LC_MONETARY="en_US.utf8"
LC_MESSAGES="en_US.utf8"
LC_PAPER="en_US.utf8"
LC_NAME="en_US.utf8"
LC_ADDRESS="en_US.utf8"
LC_TELEPHONE="en_US.utf8"
LC_MEASUREMENT="en_US.utf8"
LC_IDENTIFICATION="en_US.utf8"
LC_ALL=en_US.utf8

You’ll have to log out and back in again for the new location to set in. After that, change directories back to your Elixir directory and run away:

cd elixir-master
make clean
make test

Then you need to make sure the iex REPL is in your PATH:

PATH="$PATH:/home/vagrant/elixir-master/bin"

And you’re all set:

$ iex
Erlang/OTP 17 [erts-6.0] [source] [async-threads:10] [kernel-poll:false]

Interactive Elixir (0.14.1-dev) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)>

Away you go!

Some resources I used in setting this all up:

Twitter Hates Lists

It’s bad enough that apps like TweetBot don’t allow you to post a new tweet while reading in List view now, but even Twitter’s own web interface has minimized Lists.

Check out this bit of crazy web design:

Get a load of that — they hide “Lists” under “More” (with a down arrow chevron thing), which saves absolutely no space on the page at all. It just makes an extra step for the user to get to the list. Weird.

Sideways Elixir: Double Ampersand

Sometimes, you can learn new things about a language when you squint your eyes just so hard, tilt your head at a jaunty angle, and see things sideways. Today’s tutorial is right up that alley.

Now straighten your neck. You’ll give yourself a pinched nerve like that…

Your Friend, Double Amp

This week, let’s look at a basic part of Elixir that involves everyone’s favorite and most beautiful typographic symbol, the ampersand. Yes, even more people love the pipe operator, but that’s two characters and doesn’t count. Nyah! (Shut up, you freaky interrobang people…)

Everyone loves the ampersand. It might be hidden all the way down over the “7” key on your keyboard, but we love it anyway. Font folks go nuts over them. Hell, Hoefler & Frere-Jones referred to the ampersand as their middle name, and you see how well THAT turned out. Hoefler renamed the company to preserve this blog post, no doubt. “Hoefler & Co.”? Yes, a “Co.” dedicated to getting royally screwed over by his lies and lawyers.

But, really, the greatest Ampersand is the monkey from “Y the Last Man.” Go read that book.

But Wait! One ampersand isn’t good enough for this blog. No, let’s talk about two of them. One might look at && and think “short-circuiting operator.” If so, one has probably programmed before, and good for that one.

When used with two statements, Double Amp (as I like to call him) will return the first value if that value is false. The second one only returns when the first is true. This is as confusing and as mind-warping as that first time you didn’t pay attention and starting coding “unless not” conditionals and your brain burst.

Any Good Tutorial Will Have Examples. And So Will This One.

Take this ridiculously contrived example. It’s so ridiculous and rudimentary and ugly that it’s the kind of thing only an insane tutorial writer could come up with. Nobody in their right mind would ever write something like this.

No, they’d probably make it into a macro.

    iex(14)> equals_five? = fn(x) -> x == 5 end
    #Function<6.106461118/1 in :erl_eval.expr/5>

Look, Mom! I just wrote a lambda! Or a closure! Or an anonymous function! I bet it’s a monoid. Nobody ever can explain those, so who’s to tell me I’m wrong?

These Functional Folks are crazy.

Let’s test it out:

    iex(15)> equals_five?.(5)
    true

    iex(16)> equals_five?.(6)
    false

See? It works! It does what it says, so it doesn’t need documentation! TEST-DRIVEN DEVELOPMENT FOR THE WIN! (Except I wrote the tests after the code. And DHH still hates it.)

While I’m full of self-loathing, let’s see this function-so-ugly-not-even-Haskell-could-love-it in action:

    iex(17)> does_five_equal_five = equals_five?.(5) && IO.puts "This equals five!"
    This equals five!
    :ok

    iex(16)> does_five_equal_five
    :ok

Because the function is true, it checks the second part after the double-ampersand and executes it. In this case, it’s not a boolean expression. It’s just a command to print something out. That command evaluates to being :ok after it’s completed, though, so there’s that.

    iex(18)> does_five_equal_five = equals_five?.(4) && IO.puts "This equals five!"
    false

    iex(18)> does_five_equal_five
    false

When the function is false, it never crosses the double ampersands to run anything.

This is an optimization. Why run the second part if you don’t have to? Both halves have to be true for the overall thing to be true, so once you’ve failed on the first part, why bother looking at the second? It’s a trick as old as time itself. Even Perl does it.

The Double Amp allows you to basically guard the thing you really want to happen. Rather than if true then do, you have true && do or false && never_do.

So the next time you’re thinking that you want your code to look clean and follow logically and take great advantage of the wonderful pipe operator, think about Double Amp. Then stop drinking and use the pipe operator. It’s a completely different thing.

Really? You thought about using Double Amp like that? I’m taking your Microsoft Certification away from you!

Next time: The Single Ampersand: For when writing fn(x) -> is just too much damned work….

Throwback Thursday

I recently found this picture in the archives from right after a trip to the San Diego Comic-Con (as it was then called) in July 2000. This is the stack of comics I picked up there. You’ll see a few notable books, including Brian Bendis’ “Fortune and Glory,” Mark Evanier and Sergio Aragones’ “The Life of Groo,” the limited edition hardcover “Daredevil” book (including a CD with audio commentary!) collecting the run by Kevin Smith, Joe Quesada, and Jimmy Palmiotti. Rick Geary’s “Jack the Ripper” is in there, too, and lots of other cool books.

But let’s take a look at that computer set-up I had at the time. First, the comics sit on my original flatbed scanner. That was the big one I had, capable of scanning a 14 x 17 inch comic original art page in one go. I believe it was from Mustek. On the far side of the computer, out of sight but no doubt covered in a stack of comics would have been my previous smaller scanner. It did letter sized fine, but I was more interested in scanning large art boards by then.

Standing tall in the pic is the Gateway 2000 computer, one of two or three I owned before switching to the Mac. You can see the Mandrake Linux sticker on the front of it. I was using Linux part time by then. I wouldn’t go full time Linux for another couple or three years, before switching over to the Mac.

That sticker sits just below the DVD drive, which was my first DVD drive. I got that in 1998, and used it to watch movies on my 15 inch (or was it 17”?) LCD computer monitor. You haven’t see “The Usual Suspects” or “The Rock” until you’ve watched it letterboxed on a 4:3 computer screen, even with the great surround sound set-up I had with it. (Seriously, I had 5.1 plugged in.) Sitting three feet away from the screen was a big help. That DVD drive probably cost me $250 at CompUSA at the time, which is what I bought myself with my first real paycheck after college and getting full time job. I would buy my first set-top DVD player to attach to the TV a few months later for nearly the same price. This was before DVD players were under $100.

Just below the Mandrake Linux sticker you’ll see this computer’s crowning achievement: a built-in Zip Disk drive. It was the awesomest. Gone was the bulky blue box with its thick parallel port-connecting wire. Those wonderful cartridges could plug directly into the desktop tower. They were awesome for backups, because JPGs were tens of thousands of bytes large still, and video was postage stamp size and not something you had lots of.

I also see in the bottom right corner some packaging from “Delta Force 2,” a fun and strategic FPS game that I believe was ported to Linux. That’s the cardboard template that you could put over the function keys at the top of the keyboard to remind yourself of what each function key did.

Fun times, those were!

Swift Factorial

When learning a new language, it’s a good time to go back to the computer programming classics and see what you can do with them.

The first thing I went for was Factorial. Here’s my first script:

    func factorial(var value: Int, var result: Int = 1) -> Int {

        if (value == 0) {
                return result
        }

        result *= value
        value--

        return factorial(value, result: result)
    }

    > factorial(3)
    6

There’s some stuff to cover in here. Take the function declaration for starters:

    func factorial(var value: Int, var result: Int = 1) -> Int {

Coming from Perl and Elixir and Ruby, it feels crowded. It is, however, useful. It defines a lot of stuff ahead of time for the compiler and IDE. Xcode can sense it a mile away now when you’re using the function and auto-completes stuff for you. (Unlike Objective-C, you don’t need a separate header file now.) The compiler can test stuff along the way, too, like making sure you’re sending in the correct type, number of parameters, etc.

So, you start with func as the function keyword. Each parameter has a name and type specified. I’m using the ‘var’ in front of their names because I’m mutating their state inside the function. I need to define them as new variables. Without that var, Swift assumes these are constants. An error message will tell you that these are ‘let’ values, which is Swift-ese for “constants”, which is to say immutable.

With the second parameter, I thought I’d make it easy on the user and define a default value. result is the accumulator. Its state will change along the way as you multiply the numbers together. Since this is planned to be a recursive function, we can’t ignore it completely. It has to be there. So it gets both the var tag and the = 1 part at the end to define a default value.

When you call the function this way, you only need to supply one value, like so:

    > factorial(3)
    6

(The Swift playground displays the results off on the right sidebar of the window, not below it. But I don’t want to insert a bunch of screen grabs here, so I’ll stick to a more commmand line-looking style to show things off like this. The Xcode REPL is pretty cool, though, and worth using.)

You can be more explicit, of course, and get the same answer:

    > factorial(3, 1)
    6

And you can be flat out silly:

    > factorial(3, 0)
    0

    > factorial(3, 2)
    12

Getting back to the code, the if statement is pretty obvious. I didn’t even look at the Swift book to program this part. It’s fairly standard across multiple modern languages. No semi-colons. Use curly braces. Double equals for testing equality. This is the base case. Once we’ve counted back to 0, we return the value.

After that, we mutate our variables. Again, standard coding stuff. Double minus for decrement and star-equals to multiply a variable by some other value.

Finally we have the recursive call, where we need to specify the accumulator now. If your function provides a default value, you have to name that parameter. Xcode is pretty good about pointing that out, but it’s worth emphasizing here.

This works, but it’s not elegant. First, there’s mutable state in there. That’s bad. Second, the user should never have to type in the default value or even have the option to. This is Factorial. The accumulator should always start at 1. I’m also not a big fan of naming the parameters if you don’t have to or in very simple examples like this one. (For the rest of the Apple frameworks, they’re very handy.) So let’s hide all that stuff separately. In Swift, functions are defined by their name and arity. (Arity means the number of parameters.) This is just like in Elixir. This makes me happy and makes for even cleaner code:

    func factorial(value: Int) -> Int {
            return factorial(value, 1)
    }

    func factorial(value: Int, result: Int) -> Int {

        if (value == 0) {
            return result
        }

        return factorial(value-1, result * value)
    }

    > factorial(3)
    6

    > factorial(3, 1)
    6

So the user has the option of putting in a value for the accumulator, but they don’t have to. I haven’t read all the documentation yet, so I’m not sure if there’s an option to make a function private. If there is, I’d hide the factorial/2 function in there, and only allow the user to use the one parameter version of the function (factorial/1), which would then make the call to the two parameter version.

The function is now also immutable. No variable state is ever changed. This is enforced by creating the function with parameters that are constants. That’s a default and unseen let before each in there. There’s no assignment operator (=) anywhere to be seen inside the code, either. That will make testing much easier as well as making my code more portable.

The more I look at the code, the more it bothers me that there’s an if statement inside the function at all. But since there’s no pattern matching for the parameters in Swift, we have to live with it. This is the extra code I’d love to write to get rid of that if statement all together:

    func factorial(0, result: Int) -> Int {
        return result
    }

Place that between the two functions and it’ll match the base case and take care of returning the final value for you, if your language defaults to the first-matching function it finds, top down. Elixir does that. I like it.

New Beginnings with Swift

Various thoughts after spending a little time with Swift:

  • Ruby Motion is putting on a brave face and saying they’re not worried. There are enough differences between Apple’s Ruby-like scripting language for iOS/OS X programming to distinguish it from their own Ruby scripting for iOS/OS X. Plus, they have Android support. This is true, but how many Android developers can afford to pay for Ruby Motion? I think a lot of people won’t be updating their copy of Ruby Motion in the next year or so. Others are so through-and-through Ruby that that’s more important and they’ll stick around. But I wouldn’t put bets on Ruby Motion being a growing platform anymore.

  • Swift’s function definitions annoyed me at first. They’re very verbose, defining all their specific arguments and types along with more syntax to define a return type right at the top. I understand that structure is good and takes a lot of guess work and errors out of the game, but it struck me as being too wordy. Then I remembered that this is a language meant to work with Apple’s frameworks like Cocoa. I pictured how Objective-C worked and instantly realized what a necessity this format is. It’s not the square brackets that’ll drive you nuts in Objective-C; it’s all the ridiculously long names in function calls and parameters. That’s why XCode is a necessity, and such a valuable tool.

  • Swift is the oddest looking Perl variant I’ve ever seen. Of course, the kids these days will compare it to Rust and Ruby and Python — all of which are next generation Perl wannabes, anyway. So, perhaps Swift is just not your grandfather’s scripting language of choice.

  • It’s funny to see the Open Source zealots complaining that Swift isn’t open source while using their Android phones, which are Open Sourced long after their programmed and pretty much useless to anyone else anyway.

  • I’m glad to have been doing a lot of functional programming lately, because Swift is very functional. I recognize a lot of the same tricks here. It’s funny because I was learning Objective-C and Ruby at around the same time and learned a lot of object oriented programming lessons from doing two different OO languages at the same time. Now, I have Elixir and Swift both working on similar concepts. Maybe the two will feed into each other for me. Here’s hoping…

  • In the end, the most important thing about Swift is the same as with Objective-C: It’s just syntax to deliver the frameworks. The real knowledge you need isn’t in how to set up a loop or increment a variable. It’s in knowing what all those APIs do, and which ones are available to you, and how to efficiently look them up when you need the help.

  • It still blows my mind that they spent four years developing Swift and word never leaked out about it. And they even have the bible of the programming language written up and available on the first day of its release. It’s a well-written guide. I wish it had a couple tutorials in there, but I know one of those is available already on the Developers site, and I’m sure more will come. It’s impressive.