talkative, friendly, programmer

Simple Piglow Snap for Raspberry Pi 2

| Comments

Using a Raspberry Pi 2 with Ubuntu Snappy installed we’re going to write a simple snap that will flash a piglow attached to the gpio pins.

If you want to skip these steps you can download the snap itself from here

1. A simple piglow flashing binary

To make things simple we’ll use go to produce a simple binary that will flash the legs of the piglow. We’re using go because the static compilation means our snap will have no dependencies to deal with. The code itself is small. I modified the piglow library I was using to get a bit more feedback about piglow errors, the change is trivial to revert should you need to.

2. Create the snap

The package structure is simple

├── bin
│   └── flasher
└── meta
    ├── package.yaml

flasher is the binary I produced in step 1. The package.yaml just contains information about the snap:

name: hello-piglow
version: 1.0.0
vendor: me
 - name: bin/flasher

Once you’ve got all this in place build it and send it over to your pi.

snappy build .
snappy-remote --url=ssh://<ip-address>:22 install <snap-name>

3. On the Pi

You need to give your snap permission to access the i2c device

sudo snappy hw-assign hello-piglow.sideload /dev/i2c-1

4. You’re done

sudo hello-piglow.flasher

If all goes well you should have a flashing piglow

Generics and Monads in Good Time

| Comments

In this blog post I attempt to connect monads in haskell with generics in go, avoiding flame wars as I go. If I’ve made mistakes please let me know and I’ll gladly fix them.

The Thought

I’ve just finished listening to episode 10 of the bikeshed which is largely a discussion about haskell, and well worth a listen to. However the conversation that starts at around 29:40 is what triggered this thought.

Haskell without monads

If you want to do IO in Haskell you need to use monads. But it’s possible to write a number of programs without IO(1).

According to “A history of haskell, being lazy with class” 6 years passed between the first release of haskell, and monadic IO being introduced. Why so long?

The language designers were under no pressure, they had time to think about how IO should be dealt with in the language.

The question of Go Generics

Go doesn’t support generics, and it’s often cited as a big downside or even a failing of the language. However you can still write very useful programs in go without generics(1). But here’s the thing:

The language designers are under no pressure, they have time to think about how generics should be dealt with in the language.

This is important. Just because a language doesn’t do something now, doesn’t mean it never will. If you’ve not taken a look at go previously because you’ve been swayed by the “no generics” argument I recomend taking a look at go again.

(1) An infinite number for maths fans

What Have You Learned From Language X?

| Comments

Alan Perlis wrote

A language that doesn't affect the way you think about programming, is not worth knowing.

That’s an interesting statement, it made me wonder what people think they’ve learned from certain programming langues, and how much it differs between people.

If you have a moment, I’d love to know: Survey

Getting Software Done

| Comments

I’ve recently finished reading Getting Things Done. And I’m trying it out as my method for organising all of my work. Regardless of how it goes there are several key ideas which I think transfer directly onto the world of software development. These are (in no particular order):

  • If something takes less than 2 minutes do it,
  • Stuff that needs doing is defined as something that isn’t how or where is should be.
  • A project (something that needs planning) is something that requires more than one action to complete
  • Always think in terms of “What’s the next action?”

This last one I consider crucial especially when it comes to learning new things. I’ve been guilty of having goals such as “learn functional programming”. This type of thing is far too broad, you need to think it through until you end up on the next thing that needs doing to reach your goal. All of us are guilty of having a number of projects “on the go” as well. I’ve found forcing myself to think about the next action has really helped me make progress with them. In some cases I’ve made progress on projects that haven’t made progress in >2 years. Like everything, GTD is no silver bullet, and isn’t for everyone, but I think the book is worth reading, if you don’t find it useful, at least you know.

Vim Survey Results

| Comments

I don’t normally find programming koans enlightening or amusing, there is one execption – which pretty much sums up my approach to vim:

Master Wq and the Markdown acolyte

A Markdown acolyte came to Master Wq to demonstrate his Vim plugin.

"See, master," he said, "I have nearly finished the Vim macros that translate Markdown into HTML.
My functions interweave, my parser is a paragon of efficiency, and the results nearly flawless.
I daresay I have mastered Vimscript, and my work will validate Vim as a modern editor for the enlightened developer!
Have I done rightly?"

Master Wq read the acolyte's code for several minutes without saying anything. Then he opened a Markdown document, and typed:

HTML filled the buffer instantly. The acolyte began to cry.

But I was starting to get the feeling that most vim users had loads of plugins installed – so I decided I wanted to find out if this was true.

20 minutes later I had hacked my way through a basic survey – which was full of holes and typos. Despite this I had over 1000 responses in 24 hours – which gives us our first ‘statistic’:

Vim users love talking about vim

So, here are the results. 1300 of you responded to the survey. Percentages have been rounded down to the nearest integer. Because I said so. Questions where the answers were freeform I’ve done my best to group. But it might not be perfect. Each question was optional so each question had a number of empty responses, I’ve left these out. This is why most percentages do not add up to 100%

I welcome others to take a look at the raw results: Link to Raw Data

Do you spend most of your time in vim or gvim?

I forgot about macvim, and had never heard of vim-qt – but thankfully most of you seemed to work out that my intention was – gui or terminal.

vim 946 72%

gvim 347 26%

How often do you use vim?

daily 1227 94%

weekly 54 4%

monthly 11 0.8%

The obligatory operating system question – Which do you use most?

The world is full of programmers using macs in coffee shops right?

linux 807 62%

os x 347 26%

windows 131 10%

freebsd 19 1%

One person said they used “your face” which is an odd choice as my face has very poor memory management.

What key do you have mapped as your leader key?

I’ve always just stuck with the default, althought I was aware that alot of people use ‘,’. Using space was new to me, but according to this it’s one of the most popular keys

comma 470 36%

backslash 309 23.7

none/ don’t use, never heard of leader 220 17%

space 169 13%

semicolon 13 1%

forward slash 12 0.9%

esc 11 0.8%

colon 8

underbar 5

ctrl 4

alt/ caps lock 2

How often do you make changes to your vimrc file?

montly 603 46%

weekly 459 35%

yearly 138 10%

daily 60 4%

no customisations 21 1%

When did you first use vim?

2010’s 613 47%

2000’s 505 38%

1990’s 162 12%

How many plugins do you have installed at the moment?

This is the question I really wanted answered. Thankfully it seems like most of us would rather have less plugins

Your favourite plugin?

ctrlp 171 13%

syntastic 59 4%

nerdtree 59 4%

youcompleteme 50 3%

fugitive 41 3%

airline 39 3%

surround 30 2%

vundle 30 2%

pathogen 24 1%

Do you remove plugins you no longer user?

yes 1009 77%

no 229 17%

What is your average typing speed?

61 – 80 391 30%

41 – 60 360 27%

> 80 344 26%

20 – 40 wpm 121 9%

< 20 wpm 8 1%

Roughly how many different computers will you use in a particular week?

6 – 10 143 11%

2 – 5 877 67%

> 10 122 9%

1 132 10%

Have you written a plugin?

no 1006 77%

yes – publicly available 121 9%

yes – kept it private 148 11%

Give me a single piece of advice

Some of it is great, some is crazy, you’re welcome to take a look at the raw results:

It’s all here

Your view of emacs?

never used emacs – don’t want to 498 38%

never used emacs – but willing to try 418 32%

i use it every now and again 221 17%

it’s another tool in my toolbox 113 8%

I spent a while trying to work out how to construct this question, what I was trying to work out is if the editor war really existed, I don’t use emacs, but I did, and there are some things I think it does better than vim, and some things it doesn’t. I spent ~2 years using emacs before moving to vim, I wanted to get a broad idea of how emacs is view by the vim community. The results were a suprise

That’s it

That’s all there is nothing more. I’m not great with statistics so I encourage others to take a look at the raw data and come up with a much better ‘analysis’. If anyone feels that the above is in some way worthy of a small tip here’s my btc address:


Go Playground as a Service

| Comments

Update: So I’ve learned that there’s actually some work to be done here around sandboxing before it’s actually ready for public use. Until then I’ve removed the server, but the code is still available if you want to play around with it in a trusted setting.

One of the great things about go is the whole environment you get with it, gofmt, godoc, go get. It’s all good stuff. There’s also a playground for you to try things out on. The docs also have runnable examples in them based off a similar idea:

It’s a great idea, it means you can actually try the code out while you’re reading the docs. But it only works on code from the stdlib. But that makes sense doesn’t it? You don’t want to allow anyone to import some arbitrary package.

But it occured to me that when someone develops a package, the author might want to include runnable examples for their own code.

The source for something similar to the playgrond is incuded in the go release (misc/goplay) I’ve modified it very slightly to allow /compile to be called from any location. I’ve called it goplaas (It’s not really as a service, but that’s the standard suffix these days isn’ it? And it got your attention!)

The idea is you install it on your own server, and it will have access to anything installed on your GOPATH. This means you can control what packages can be imported, and also the version.

All you need to do is put some javascript and a textarea into a webpage, point it to your server and you’re done.

Here’s an example using the loggo package from Try it

I've removed it - sorry

I’ll try to keep my server running with goplaas for the next few weeks. If it suddenly stops working, it’s likely that I’ve pulled the server down. You can experiment yourself by running

go install
./goplaas -http :9999 #Listen on 9999

goplaas includes a -compile flag for sending compilation instructions to another server, full instructions are in github.

A good next step would be to package this all up into a docker container to allow anyone to install it with relative ease

Clojure Programming in Acme

| Comments

Today I finally got round to trying out something i’d been meaning to look into for nearly a year, and seeing as clojure and editors seems to be a topic again I thought it would help add some diversity.

Acme is an editor unlike any other. It was designed by Rob Pike for Plan9 (although it’s available for unix-like systems using Plan9 from User Space). It’s designed to be an editor and shell. Unlike emacs and vim which encourage you to keep your hands on the keyboard, acme requires use of a 3-button mouse, and it supports something called mouse chording. A rough guide would be:

  • Button 1: Select Text
  • Button 2: Execute Command
  • Button 3: Search/ acquire text (or load file)
  • Button 1-2 Cutting
  • Button 1-3 Pasting
  • Button 1-2,3 Cut+Paste (Copy)

My introduction to acme was via Russ Cox’ video, which is a great starting point:

You can probably summarise the whole thing by saying that in acme – text is data. Once you type some text, it can be saved into a file, executed as a command or passed as an argument to a command.

Obviously this resonated with the clojure part of my brain. It occured to me that being able to select some clojure code in acme and evaluate it would make acme a great editor for clojure.

Thanks to nrepl the job is really quite easy. I wrote a very very simple nrepl client in go. You can find it on my github

All it does is read clojure code from stdin and send it to nrepl. This is really all there is to do. In acme you just need to type your command in one of the scratch areas:

Take a look at the below screenshot. You can see:

  1. In yellow: selected clojure code with mouse button 1
  2. In red: selected command with mouse button two
  3. Release the buttons and the window in the bottom right contains the evalutated code

Here’s a video of me doing it:

Clojure in Acme from Matthew Williams on Vimeo.

To get this setup yourself:

  1. Install plan9 from user space to get acme
  2. run lein repl in some terminal
  3. You’ll need a command line tool that can take clojure code on stdin and send it to nrepl. I used my own gonrepl tool- To use that you need to install go then run:
go install

That’s it. Thanks for reading!

Why Juju Means I Don’t Need a VPS

| Comments

tl;dr: Juju is aimed at deploying and scaling hundreds of services. But it’s also a handy alternative to buying a VPS.

I don’t have a VPS, I have in the past but I’ve never used them enough for me to be able to justify the cost long term. Juju provides a perfect solution to this use case.

I have a very simple charm I call devenv, it doesn’t do much more than install stuff I use for doing development, like installing tmux and grabbing my configuration files. I’ve blogged about it previously here.

This week I was having trouble getting a client library to connect to a simple server program I’d written, I was lost with debugging but luckily the library author volunteered to help me out. I already had a binary of the server that could be used to generate the error in the client library, but I don’t have a VPS, so what’s the quickest way for me to make the server public so the library author can play around with it?


All I needed to do was to add the following line to the start hook of my devenv charm:

./app &

And copy the app binary to my charm directory.

It’s sort of an anti pattern – but because juju copies the whole charm dir when it deploys, it will grab any binaries in the charm dir as well, it’s not a great idea to make use of this for serious deployments, but for me it was great

With that done all I had to do was deploy it:

juju bootstrap
juju deploy --repository=charms local:precise/devenv

From deciding I wanted to deploy the binary – to having it deployed was under 5 minutes. 20 minutes later the library was fixed and the server was taken down

juju destroy-environment

That was it. All in all it cost around $0.02 (that’s just over 1p). Much better than having a VPS that would largely sit idle.

Of course the other advantage with using juju is that I’m not tied to a particular cloud provider, so I can choose which one I want depending on the pricing at that moment. And since juju now supports local mode you can do your charm development offline to save even more money!

Great, how does this help me?

If you’re thinking of getting a VPS – but don’t feel you can justify the cost because of the amount of use you’d get out of it, then you really should try out juju. Once you’ve got it installed there’s a handy charm that just deploys ubuntu server, it’s a great starting point for writing a charm to setup your own development environment.

juju deploy ubuntu

Here’s another link to my post about writing a charm to build a development machine:

Simple Macros in Clojure and Elixir

| Comments

Don’t write macros is a pretty good rule, but when you’re learning a language it’s quite a fun little exercise. One such exercise I like to try is this:

Write a function so that I can call system commands without having to wrap them in quotes.

So rather than doing this:

cmd([“grep”, “-rin”, “foo”, “./”])

I can do this:

cmd([grep, -rin, foo, ./])


In lisps like clojure, the solution is very elegant, thanks to the minimal syntax, you end up with something like:

(call grep -rin foo "./")

And the macro to do it is also pretty elegant:

(defmacro mcall [& args]
  (let [sym (gensym)]
  `(apply sh (map str '~args))))

Not too much magic (as far as macros are concerned) here. Make a unique symbol with (gensysm) to keep our macro hygienic. We syntax quote our whole list using ` to not evaluate anything unless we say so. That means we have to do some magic with our args parameter: ‘~args. ~ here means evaluate args (so we get the symbols we passed in) but then we quote with ’ so that we don’t evaluate these symbols. That’s the magic that lets us not need quotes around our arguments. The rest of the code just puts them in the right place by mapping our args list against the str function and applying it to sh. This is how you use it:

(prn (mcall ls -l))
(prn (mcall grep -rin not "./")) ; We still need quotes around ./ because it's not valid syntax

By coincidence I blogged about clojure macros almost exactly one year ago:


Elixir is a lovely little language I’ve been playing with recently, so naturally I wanted to try out the macros. I needed to ask a few questions on the irc channel (thanks ericmj and true_droid!), but I got it cracked:

defmacro mcall(args) do
    call = Enum.map_join(args, " ", Macro.to_string(&1))
    quote do

Again, not much magic, map_join maps our list using the Macro.to_string function then joins at the end, this is done outside of the quote since we will work it out at compile time. We then pass this to System.cmd in a quote block – which means don’t evaluate this yet, that will happen when we call mcall at runtime.

IO.inspect MacroTest.mcall([ls, -l])
IO.inspect MacroTest.mcall([grep, -rin, def, "./"]) # We still need quotes around ./ because it's not valid syntax

Taming the Cloud With Juju and Raspberry Pi

| Comments

The idea

We’re going to use a raspberry pi and juju to setup a wordpress blog on aws.

The raspberry pi is an amazing little gadget. Low power enough that I don’t feel guilty about leaving it turned on overnight, but flexible enough that I can shove leds into it to grab my attention if needed.

Juju is ubuntu’s answer to setting up services in the cloud. Once installed you simply setup your credentials with an existing cloud provider like aws or hp cloud and then deploy things into it. Setting up wordpress for example is as simple as

juju bootstrap # Setup the cloud environment
juju deploy wordpress # Start an instance and install wordpress on it
juju deploy mysql # Start an instance and install mysql on it
juju add-relation wordpress mysql # Connect wordpress and mysql together
juju expose wordpress #Allow wordpress to be accessed from a public ip

The Plan

  1. Install go onto the raspberry pi
  2. Install juju
  3. Deploy a wordpress blog

We’re going to be installing the latest version of juju which is written in go.

Installing Go

Dave Cheney has written an excellent article on Installing go on the raspberry pi. On my model B (1.0 revision) I had to update raspi-config so that I could select the 240M/16M memory split, but you might have to see how you get on here. I choose to skip the tests and just ran ./make.bash to build go rather than all.bash.

I was unable to build juju using go 1.0.3. I ended up building go 1.1.1 from source, which seemed to work fine. From memory I believe you need to setup a folder to be your GOPATH before you can install the latest version of go.

Installing Juju

Getting the latest tip of juju-core is easy using the go get command, it grabs all the dependencies for you, it does mean we’re grabbing tip, but there are ways to fix that later if we need to. juju-core is hosted on which uses bazaar for source control. This means you need to install it before you can run go get. I was able to install it on raspian using

sudo apt-get update
sudo apt-get bzr

Without doing the update I was unable to grab all of bzr’s dependencies, so I recommend running the update first.

Once bzr is installed you can install juju using go get:

sudo apt-get install libcurl4-gnutls-dev
go get

When it’s finished you should find a juju binary installed. Mine ended up in my GOPATH, I think because I had something setup wrong somewhere. But if you’ve got this far I’m sure you’ll be able to find it.

Setting up Juju

There’s much better instructions for setting up juju here The basic steps come down to

  1. Setting up a public key
  2. Generating an environments.yaml file
  3. Filling the file in with the data for your cloud provider

I configured juju for aws. My ~/.juju/environments.yaml file looked like this:

default: amazon
    type: ec2
      access-key: MY AWS ACCESS KEY
      secret-key: MY AWS SECRET KEY
      control-bucket: mattyw-ec2-bucket
      admin-secret: mattyw-ec2-admin

Once you’ve done this, try running juju bootstrap. If it finishes without error wait a minute or so and run juju status, If all goes well you should see something like this

$ juju status
    agent-state: started
    agent-version: 1.11.0
    instance-id: i-0bfc3d64
    series: precise
services: {}

Now we can start the business of getting a blog up and running:

juju deploy wordpress
juju deploy mysql
juju add-relation wordpress mysql
juju expose wordpress

You might have to wait a few minutes for everything to start up, keep taking a look at the output of juju status until you see all the agent-states running and expose true on wordpress.

When it’s done you should see something like this in your juju status output:

    charm: cs:precise/wordpress-15
    exposed: true
      - mysql
      - wordpress
        agent-state: started
        agent-version: 1.11.0
        machine: "1"

Now point your web browser to the url in public-address and there you go, you’re deploying stuff from your pi.

Juju doesn’t stop at just deploying blogs. Check out Juju to see what else it can do for you