Erlang MOOC

I just completed An Introduction to Functional Programming with Erlang, a mini-MOOC at the University of Kent. It was terrific!

Screen shot of master class

Erlang is a functional programming language created at Ericsson in the 1980s. In 1998, it was open-sourced. Erlang’s claim to fame is massive scalability with high reliability. Ericsson designed it for their telephony system. More recently, WhatsApp used it for their messaging system (you may have heard of this last year, when Facebook paid a huge sum of money for Whatsapp). I’m interested in Erlang because I’m excited about Elixir, a relatively new programming language (version 1.0 was released just last year) which runs on the Erlang virtual machine (BEAM). I figure everything I learn about Erlang will help my understanding of Elixir.

I installed the Erlang compiler and documentation on my Debian laptop with

sudo apt-get install erlang erlang-doc

Easy peasy! Now I can create hello.erl

-module(hello).
-export([hello/0]).

hello() -> io:fwrite("Hello, World!\n").

and compile and run it in the Erlang REPL

$ erl
Erlang/OTP 17 [erts-6.2]  [64-bit] [smp:2:2] [async-threads:10] [kernel-poll:false]

Eshell V6.2  (abort with ^G)
1> c(hello).
{ok,hello}
2> hello:hello().
Hello, World!
ok

Lemon squeezy! Now I’m ready to follow along with the course.

The mini-MOOC is a work in progress, but it’s very well done. It’s essentially the first three weeks of what will become a six week MOOC. It uses a University message board called Moodle now, but I think they intend to move to a proper MOOC system for the full course. As such, this three week pilot was limited to some 500 students.

The course is taught by Professor Simon Thompson. There are videos of him lecturing

Screen shot of Prof Thompson

presenting with slides

Screen shot of Prof Thompson with slide

and live coding

Screen shot of Prof Thompson live coding

There was also a “Master Class” segment filmed in a fancy studio (that’s the first photo at the top).

Additionally, there were quizzes and exercises. And every page had a discussion section where you chatted with other students. There was also a teaching assistant, Stephen Adams, who would show up there and post his solutions to the exercises, along with his comments about why he did things a certain way.

Erlang is kind of an odd language and takes some getting used to. I thought Prof Thompson did a good job of explaining the weirder parts, like what Erlang means by variable, assignment, and pattern-matching.

I enjoyed that Erlang was weakly typed, which let us concentrate on things like recursion, testing, and higher-order functions. Contrast this with the Haskell course, which would have us believe that functional programming is all about algebraic data types. Towards the end of the course, Prof Thompson hints that a stronger type system could be helpful for larger projects, but it was nice to not have to mess with it right from the start. That point came up again in a discussion video with Joe Armstrong (creator of Erlang) and Francesco Cesarini (Erlang Solutions Ltd).

Screen shot of Cesarini, Thompson, and Armstrong

Overall, I thought it was time well spent. Based on this pilot, whenever the full six-week course is ready, I would definitely recommend it.

Erlang MOOC

Go diamond

Lately, I’ve been using Go for things that I used to use Perl, Python or Ruby for. This includes quick and dirty scripts for filtering text.

Perl’s “diamond” operator (<>) encapsulates all of this one fell swoop.

#!/usr/bin/env perl

use v5.20;
use warnings;

while (<>) {
    # do something with $_ here
    print;
}

Without any arguments, this will read from stdin line by line. If there are arguments, it will treat them as filenames and read from each of them line by line. If any of those arguments is “-“, it will take that to mean stdin. It’s the perfect thing for the Unix command line. Indeed, we can write one-liners that do all of the above with just a -p flag (or a -n flag, without the print).

In Python, we have a similar capability with the fileinput module.

#!/usr/bin/env python

import fileinput

for line in fileinput.input():
    # do something with line
    print(line, end="")

In Ruby, we iterate through ARGF

#!/usr/bin/env ruby

ARGF.each do |line|
    # do stuff with line
    print line
end

In short, Perl, Python, and Ruby each make it super easy to write command line utilities that just do the right thing. How do we do something similar in Go?

Well, none of it is hard, but there really is quite a lot going on those tiny little snippets above. That becomes apparent when you write it all out “by hand” in a language like Go. Here’s what I came up with.

package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {

    filenames := []string{"-"}

    if len(os.Args) > 1 {
        filenames = os.Args[1:]
    }

    for _, filename := range filenames {

        var file *os.File
        var err error

        if filename == "-" {
            file = os.Stdin
        } else {
            if file, err = os.Open(filename); err != nil {
                fmt.Fprintln(os.Stderr, err)
                continue
            }
            defer file.Close()
        }

        scanner := bufio.NewScanner(file)
        for scanner.Scan() {

            line := scanner.Text()

            // do something with line here

            fmt.Println(line)
        }
        if err := scanner.Err(); err != nil {
            fmt.Fprintln(os.Stderr, err)
            continue
        }
    }
}

But wait, there’s more! Perl and Ruby are keeping track of the line numbers already too. So is Python’s fileinput. If we wanted to print those out, we’d just print out “$.” in Perl and Ruby and “fileinput.lineno()” in Python. In Go, we’d have to create a variable to keep track of those as well.

But doing so, we’d know exactly whether we had the line number for each file or for the total. In Perl, Python, and Ruby, we have to take some care to figure out whether it’s per file or not. I think it’s little things like this that cause me to not miss the brevity of Perl, Python, and Ruby when I’m writing Go.

Go diamond

Beethoven Coffee

This morning, my nephew posted a link to this visualization of the daily routines of famous creative people on Facebook. The thing that jumped out at me was one of the mouseovers for Beethoven: “Having coffee, exactly 60 beans per cup.” Naturally, I had to try it!

20150108_094325

Sixty coffee beans might sound like a lot, but once you grind them up, you find they pretty much fill an average coffee scoop (30 ml).

20150108_095439

That doesn’t even fill a K-cup.

20150108_095828

But Beethoven was right, it makes a fine cup of joe (I’m drinking it as I type this)!

20150108_100245

Beethoven Coffee

Emacs 24.4 in Debian jessie

Hey, Emacs 24.4 is in Debian jessie…I’m typing on a Debian jessie laptop right now! Let’s upgrade!

$ emacs --version
GNU Emacs 24.3.1
Copyright (C) 2013 Free Software Foundation, Inc.
GNU Emacs comes with ABSOLUTELY NO WARRANTY.
You may redistribute copies of Emacs
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING.

$ sudo apt-get update
...

$ sudo apt-get upgrade
...

$ emacs --version
GNU Emacs 24.4.1
Copyright (C) 2014 Free Software Foundation, Inc.
GNU Emacs comes with ABSOLUTELY NO WARRANTY.
You may redistribute copies of Emacs
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING.

Sweet! Now we have to re-install the use-package package using the Emacs package manager and re-install all of the Cask packages.

$ cd ~/.emacs.d
$ cask install
...

Restart Emacs and we’re back in business!

I’ve been using the fancy rectangle mode hidden inside of CUA mode for a while now, but I don’t want the rest of CUA mode, so I have these lines in my init file.

'(cua-enable-cua-keys nil)
'(cua-mode t nil (cua-base))

Emacs 24.4 has its own fancy rectangle mode using C-x SPACE. That seems to work fine, so I removed those cua-mode lines.

Rats! Now when I try to post this blog, I just get

Wrong type argument: stringp, netrc-get

in the minibuffer. It seems that netrc.el is busted in Emacs 24.4!

Emacs 24.4 in Debian jessie

Emacs 24.4 released

Emacs 24.4 was released today! Let’s try it

wget http://ftpmirror.gnu.org/emacs/emacs-24.4.tar.xz
tar xf emacs-24.4.tar.xz
cd emacs-24.4
./configure
make
make check
sudo make install
/usr/local/bin/emacs &

Among other things, it has a new web browser…eww. No, I don’t mind web browsers…that’s its name! Type M-x eww RET to try it. Here’s what the Elixir home page looks like in eww:

screenshot of eww in Emacs 24.4

Keen!

As I understand it, this will be the last release of Emacs 24. Next up, Emacs 25!

Emacs 24.4 released

FP101x – Introduction to Functional Programming

This week, I started the Functional Programming course on edX! Almost all of my recreational programming time has been devoted to Go lately, which is decidedly not functional. But I’m excited about the release of Elixir 1.0 and I’ve been meaning to do more with Clojure, so I think I’ve got a lot more functional programming in my future. Perhaps this course will give me a boost!

Haskell

keep calm and curry onand ghci in terminal

The course is not a Haskell course per se, but that’s what it uses for all the examples. I installed the Glasgow Haskell Compiler with

sudo apt-get install ghc

That was easy. Thanks, Debian!

$ cat hello.hs
module Main
    where
      main=putStrLn "Hello, World!"
$ ghc -o hello hello.hs
[1 of 1] Compiling Main             ( hello.hs, hello.o )
Linking hello ...
$ ./hello 
Hello, World!

Emacs

Next I configured Emacs for Haskell by adding

(depends-on "haskell-mode")

to my Cask file and

(use-package haskell-mode
  :init
  (progn
    (add-hook 'haskell-mode-hook 'haskell-indent-mode)
    (add-hook 'haskell-mode-hook 'interactive-haskell-mode)))

to my Emacs init file. Now I’m ready to try the first set of exercises!

screen shot of Emacs in haskell-mode and ghci in terminal

Figure 2: Emacs in haskell-mode and ghci in terminal

FP101x – Introduction to Functional Programming