Monday, January 7, 2013

Using vim's path to speed up your Go project

If you're working on a project in Go, and you're using vim, then you really need to check out path:
:help path
Here's a very quick tour.

Add this to you .vimrc:
set path +=/your/projects/gopath/src/**
** tells path to include all subdirectories. By adding your projects GOPATH entry it opens up access to two rather cool features that are useful in navigating your project.

:find

You can use :find to open a file instead of using :e. But :find will look in all of your project's directories to find the file. So rather than having to do:
:e /package/v10/another-dir/god.go
You just need:
:find god.go
You also get tab-completion for free - just in case you can't remember the file's name.

gf

gf is the useful little command that goes to the file under the cursor. Well providing you've set your path correcly you can navigate your project's imports with ease:
package main

import (
 "code.google.com/p/go.net/websocket"
)
By "gf"ing over the import vim will open the directory, giving you the list of files in that package.

Just don't dare navigate the directory using the arrow keys!

Update: 8th Jan 2012

On the subject of vim and Go it's worth remembering this little gem:
au BufWritePost *.go !gofmt -w %
Whenever a Go file is saved gofmt will be run against it.

Sunday, December 30, 2012

go(ing) under

Lots of people like go and this year I began to find it unavoidable - I'm even ending the year with go being the main language that helps pay my bills!

One of the things that go lets you do is pass functions around.
I started the year talking about under from the j language. Which lets you 'bookend' some function with another function (a verb) and a function which undoes that function (obverse).
A great example is Pythagoras' theorem:
  1. You perform some 'pre-processing' on each value (squaring)
  2. You add all the values up
  3. You square root the result
I started the year talking about under in clojure. Here it is implemented in go. Complete with functions being passed around, closures, and functions returning functions. Happy new year!

Monday, October 22, 2012

Running juju locally on 12.04 (or getting over agent-status pending)

I've just tried following the instructions here to get juju running locally on my brand new Ubuntu 12.04 (desktop) system.
No matter what I tried my wordpress and mysql charms seemed stuck in agent-status pending and ip address null. Even waiting a couple of hours.

I needed to take to a couple of extra steps to get it working that didn't seem to be documented in one place. So I thought I'd put them both here.

All it took was to add a rule to ufw:
sudo ufw allow from 192.168.122.0/24 to any
Hope this helps someone who was stuck in the same way I was.

Saturday, September 15, 2012

Clojure, Vim and the delay sending to screen

So, long story short: I'm use vim for writing clojure (and everything else for that matter)

If you're following the setup How I develop Clojure with Vim on the :wq blog then you might encounter a problem where it takes a few seconds for code you send from vim to appear in your screen session. Here's the solution.

Add the following lines to you ~/.screenrc

msgwait 0
msgminwait 0

And there you have it, instant code from vim to your REPL!

Tuesday, August 14, 2012

Another clojure macro tutorial (that no one should follow)

Disclaimer: This post shows you something you can do with macros. Not something you should do.

I like python.

You define functions

def something(x, y):
    return x + y

And you can document functions
def something(x, y):
"""Adds two things together"""
    return x + y

I also like clojure.

You can define functions
(defn something [x y]
  (+ x y))

And you can document functions
(defn something 
  "Adds to things together"
  [x y]
  (+ x y))

Documentation before the arguments? Despicable! If only there was a way of putting them in the right order.

Well, for the sake of argument let's try

Remember that in clojure code is data. A function is just a list, and we want to be able to define functions with some of the items of the list in a different order. At the moment a function definition list looks like this:

(function-name doc-string args function-body)

and we want to be able to make a function using the argument order

(function-name args doc-string function-body)

The first rule of matco club is "Don't write macros". So lets try:

First, how do we want our function (let's call it defndoc) to work? We want it to behave just like a normal function definition but with the docstring after the args.

(defndoc something [x y]
  "Adds to things together"
  (+ x y))

Now let's try to write it. We want to call our defndoc function and have that call defn with the arguments in the correct order.

(defn defndoc [fname args docs & body]
  (defn fname docs args body))

But this isn't going to work as our arguments are going to get evaluated. But this isn't what we want, looks like we will have to write a macro. This is how it looks

(defmacro defndoc [fname args docs & body]
    `(defn ~fname ~docs [~@args] ~@body))

Let's discuss the differences between this and our non-macro attempt.

First we use a syntax-quote (`). This is going to allow us to choose which bits of our list our evaluated and which are not. For example, the defn we want to be evaluated but the other parts we don't.

The next symbol is unquote (~) which tells clojure to evaluate these symbols.

The last symbol is unquote-split (~@) which tells clojure that there is a list of things here that needs to be expanded in place.

now if you do call macroexpand-1 using our defndoc macro on our function with the docstring following the arguments you will get the following

(clojure.core/defn something "Adds two things together" [x y] (+ x y))

Perfect, now we can sprinkle defndoc all over our code and have the docstring in the place we want it, but also keep clojure happy.

Now don't let me ever catch you doing this!

Saturday, June 30, 2012

Synchronized (promise) queues in Clojure

I've mentioned to a few people at work recently that my favourite built in library in python is the queue. The reasons being that the idea is simple, the interface is simple, and you can do all sorts of amazing things with it. We use it as the basis for turning our asynchronous automation interface into a synchronous one and it essentially provides the entire basis for our regression test suite.

In fact, I like it so much I've decided to write it in clojure

sync-q

There's instructions there about how to use it in your own applications

The idea started back when I was reading Clojure Programming and discovered promises that came in with clojure 1.3.0.

Promises are essentially empty boxes for you to put stuff in. When you want to read from a promise you will block until the promise has a value

(def p (promise))

@p ;; This will block until we have a value

(deliver p :hello)

@p
:hello

(realized? p) ;; has the promise got a value? 
true 

From this point on that promise isn't available for delivering to as it already has a value.

This makes it very easy to implement python's queue class. You just need something that behaves like a queue and fill it with promises.

creating a new queue becomes creating a queue with one promise in it.

putting an item onto the queue becomes delivering to the item at the front of the queue then adding a new promise to the end.

getting is just as simple as getting the item at the front of the queue.

The downside is you need special functions to get the size of the queue and emptiness as you only count promises that have been realised

The first version took a couple of hours and was fun, it will be great to know if anyone makes use of it.

Friday, June 22, 2012

NoClassDefFoundError: scala/ScalaObject

I thought I'd start the weekend by trying to call scala code from a java project in eclipse.

I wanted to try it without having to compile the scala code to a jar first. The whole exercise was very painless.

Define some class in scala:


class Cell(val row: Int, val col: Int, val slot: Int) {

}

Then call it in your java project:


import cell.Cell;

public class JCell {
 public static void main(String[] args) {
  Cell c1 = new Cell(2, 4, 6);
  System.out.println(c1.row() + " " + c1.col() + " " + c1.slot());
}
}


The only sticking point was my build path class order needed to have the scala stuff first - which makes perfect sense when you think about it.