This past weekend was great weather-wise. Sunny and 70. Summer's around the corner but in Ohio there are never any guarantees... the weekend before it snowed 4 inches. We had places to go and errands to run (as always), but I got some work done outside, took the kids on a bike ride, and met some new neighbors down the street who were cutting up trees they'd taken down (free firewood!). Sunday ended with a nice cookout, a good vanilla stout, and watching "Fantastic Beasts and Where to Find Them" with the kids. All-in-all, it was pretty awesome. This is always the lull for us, between Christmas and Easter... then the holidays, picnics and birthdays start up again in full force.
I always wish I could get more done though. Who wouldn't like to be able to do several things at once? But where people generally stink at multitasking, computers shouldn't. I'm taking a class right now called Concurrent Programming in Erlang, all about how to do many things (potentially thousands or even millions) at once, and how it's built into the language. If you've already got some Erlang experience, jump in. Week 2 (of 3) started, but they'll probably leave the course open for a few weeks past the finish.
We've only scraped the tip of the iceberg so far, but here's some of what I've learned.
Concurrency in Erlang (the basics)
Take a very simple function like the following, which accepts one parameter (a message), and then pattern-matches on it to determine what action to perform.
-module(talk). -export([parrot/0]). parrot(Message) -> case Message of hello -> io:format("Hi to you too!~n"); Msg -> io:format("Your message: ~p~n", [Message]) end.
But that method is running in the same process that called it. How do we take advantage of concurrency and run it in its own thread/process? Replace the
case with a
receive and remove the parameter.
-module(talk). -export([parrot/0]). parrot() -> receive hello -> io:format("Hi to you too!~n"); Message -> io:format("Your message: ~p~n", [Message]) end, parrot().
Then how does it get the message? When a message is sent to the process executing this function, it's placed in a mailbox (you get one per process for free). Once it's running (I'll get to that in a sec), the above function is suspended at the
receive, waiting for a message to get delivered to the mailbox, and when it does it matches the pattern and performs an action. (The last call to
parrot() just makes sure it's waiting for the next message again.)
All that's left is to start the function in a separate process - spawn it - and send it messages:
Parrot = spawn(talk, parrot, ). Parrot ! "Testing 1.. 2.. 3"
It gets more complicated than that of course, but I was impressed with how easy it was to get moving out of the gate. Instead of just displaying messages, the "action" could be to do some calculation, call another function, or even spawn another process that waits for its own messages.
If you sign up for the course, be sure to leave some time to skim through the comments. There are some people taking the course who seem to have a lot of previous functional programming experience, and some of the discussions they're having and resources they're sharing have been helpful in addition to the course itself.
Here's some resources that have been passed around in the forum (some are from last month's class). There's a lot so I kinda haphazardly threw them into categories. Mostly I just wanted to make a note of them somewhere.
- Erlang shell cheat sheet
- Erlang Programming Rules and Conventions
- Why Functional Programming Matters
- A History of Erlang
- Why The Cool Kids Don't Use Erlang
- Which choice is better? Func or Case?
- Static vs. dynamic typing of programming languages
- Running out of Ammo. When you can't bang!
- The Eight Myths of Erlang Performance
- Making reliable distributed systems in the presence of software errors
- As bad as anything else: On Erlang's Syntax
- Solving Embarrassingly Obvious Problems in Erlang
- The Actor Model (everything you wanted to know...)
Testing is good
- Unit Testing with EUnit, by Richard Carlsson
- Getting started with EUnit, part 1 (no idea if part 2 ever made it)
- Testing functional programs
- Spawned Shelter, a collection of the best articles, videos and books for learning Erlang
- Erlang Solutions YouTube Channel
Other tools and related
The University of Kent has three classes that teach progressively harder concepts, and they've posted the channels to their videos.
- Erlang Master Class 1: Functional Programming (Simon Thompson)
- Erlang Master Class 2: Concurrent Programming (Joe Armstrong)
- Erlang Master Class 3: OTP Behaviours and Releases (Francesco Cesarini)