tag:blogger.com,1999:blog-5211333490490532282024-03-19T02:59:53.382+00:00mattywwhatever I've been playing around with latelyAnonymoushttp://www.blogger.com/profile/11476264728618297782noreply@blogger.comBlogger47125tag:blogger.com,1999:blog-521133349049053228.post-2786766255767480932013-06-03T11:24:00.001+01:002013-06-03T11:26:03.361+01:00Why I'm happy to live without generics in go (for now)<p dir=ltr>I was at GoSF last month (9th May) when Andrew Gerrand gave his talk on go 1.1. At the end of the talk a couple of questions came up on the subject of generics. You can see them on this video at 1:09:20 and 1:14:40. (The questions aren't audible, but the answers are)
</br>
<iframe width="560" height="315" src="http://www.youtube.com/embed/USr0Bvg1ZOo" frameborder="0" allowfullscreen></iframe>
</br>
If you don't want to watch the video (you should) the summary (and I'm paraphrasing alot here) was essentially that it's not that anyone is trying to keep generics out of go, but rather no one has found a nice way of putting them in. Andrew went on to mention that he didn't miss them at the moment as copying and pasting a few for loops and if statements was an ok alternative.
</br>
<br/>
I nodded in agreement at the time, but last week I proved it to myself in code, so I thought I'd blog about it. I found myself wanting to add up the value of some things in a list, here's a very trivial substitute for the actual code:
</br>
<script src="https://gist.github.com/mattyw/5697203.js"></script>
It's nothing ground breaking, you could say I'm folding (or reducing) over the list, you could also just say I'm adding up the price of all the things. It would be nice to have a generic fold that I could use to explicitly say that I'm folding over the list, but what advantage would this give me?
<br/>
<br/>
I probably wouldn't just call fold by iteself, i'd probably still put fold inside the TotalPriceOfBasket function so that I can state the purpose more explicitly, and the reader would be left with a much simpler looking function, but - and here's my point. fold, map, filter should be recognisable as patterns. You should be able to look at a function and say "Of yeah, a fold, ok"
<br/>
<br/>
I'm not making any statements here about how useful generics are or aren't. or how useful a fold/map/filter function would be in go, all I'm saying is that I can live without them for now. Especially as the reason they're not currently in the language is that the core contributors haven't found a good way of implementing them. There's a lot to be said for that approach.Anonymoushttp://www.blogger.com/profile/11476264728618297782noreply@blogger.com9tag:blogger.com,1999:blog-521133349049053228.post-55915224968115186632013-05-23T17:37:00.000+01:002013-05-23T17:39:20.727+01:00Deploying a development machine with jujuI've just been reading Stavros' blog post about provisioning and deploying virtual machines using ansible:
<a href="http://www.korokithakis.net/posts/example-provisioning-and-deployment-ansible">An example of provisioning and deployment with Ansible</a>.
</br>
It's worth a read, I've recently come round to the same way of thinking - keeping my development machine as clean as possible and using virtual box machines to install dependencies on, but there's another side to it. Sometimes I want a machine without having to divide my laptop's precious resources, using the cloud that type of thing is easy. But using ubuntu's juju it's even easier.
</br>
</br>
<a href="http://juju.ubuntu.com">juju.ubuntu.com</a> is ubuntu's answer to service orchestration. There's a good video of it in use at <a href="http://www.youtube.com/watch?v=W2LGmY796wQ">OSCON 2012</a>. It's great at the large stuff but it's also great at small stuff - which is where this post comes in. I find it helpful to just think of juju as a way of installing charms on a cloud machine, and charms are just scripts. That way it sounds less scary.
</br>
</br>
I've got some very simple charms for you to get started <a href="https://github.com/mattyw/charm_collection">here</a>. The one I'm going to cover in this post is called devenv. The purpose of devenv is to setup a simple development environment in the cloud. Sometimes I need to make use of mysql, mongo or postgres, so it covers those as well
</br>
</br>
<b>How to deploy devenv</b>
</br>
</br>
After you've followed the intructions on <a href="http://juju.ubuntu.com/get-started">http://juju.ubuntu.com/get-started</a> to get setup with your cloud provider it's as simple
as running the deploy_devenv.sh script in my charm_collection repo. Here's what's going on under the covers:
</br>
<pre class="prettyprint">
juju deploy --repository=charms --constraints "mem=8G" local:precise/devenv
</pre>
</br>
With juju deploy we're expecting to see our charm in the charms/precise/devenv folder below the current directory. Juju then zips the charm up and deploys it on a machine of at least 8GB memory, which we specified with out mem=8G constraint.
</br>
Deploying our charm means running a couple of hooks. install and start. Hooks are just scripts which get run on the deployed machine.
</br>
Install looks like this:
</br>
<pre class="prettyprint">
#!/bin/sh
apt-get -y install vim tmux git bzr mercurial
wget -O /home/ubuntu/.tmux.conf https://raw.github.com/mattyw/dotfiles/master/tmux.conf
wget -O /home/ubuntu/.vimrc https://raw.github.com/mattyw/dotfiles/master/vimrc
echo "set editing-mode vi" > /home/ubuntu/.inputrc
</pre>
</br>
There's nothing much going on here, make sure some packages are installed, copy some of my configuration files from github to the right places and set vi editing-mode in bash. I could, and probably will add to this by getting my install hook to clone the right repositories, install a few more languages and maybe copy around some of my ssh keys, but this is a good start.
</br>
Because all I want to do is install some applications and not run any services the start hook just echos that I'm now up and running.
</br>
<pre class="prettyprint">
#!/bin/sh
echo "Running"
</pre>
</br>
The interesting part is the relationships with databases, sometimes I need to be able to use a database for my development work. This can be done by making use of juju's relationships. For convenience I've got 3 hooks that point to the same python file:
<pre class="prettyprint">
mongo-relation-changed -> relation_hooks.py
mysql-relation-changed -> relation_hooks.py
pq-relation-changed -> relation_hooks.py
</pre>
for this relationship all I want to do is write a config file to my home directory, so that I can make use of the db. The database charms set a number of key/value pair using the relation-set command. all I need to do is call the relation-get <key> command when I want to get a particular value. Here's how it's done for mongo and mysql:
<pre class="prettyprint">
def mysql_relation_changed():
host = os.popen('relation-get host').read().strip()
user = os.popen('relation-get user').read().strip()
database = os.popen('relation-get database').read().strip()
password = os.popen('relation-get password').read().strip()
slave = os.popen('relation-get slave').read().strip()
with open('/home/ubuntu/mysql.conf', 'w') as mfile:
mfile.write('host=%s\n' % host)
mfile.write('user=%s\n' % user)
mfile.write('database=%s\n' % database)
mfile.write('password=%s\n' % password)
mfile.write('slave=%s\n' % slave)
print "Done!"
def mongo_relation_changed():
host = os.popen('relation-get hostname').read().strip()
port = os.popen('relation-get port').read().strip()
with open('/home/ubuntu/mongo.conf', 'w') as mfile:
mfile.write('host=%s\n' % host)
mfile.write('port=%s\n' % port)
print "Done!"
</pre>
To start create a relationship you just need to tell juju to make one:
<pre class="prettyprint">
juju add-relation devenv mongodb
</pre>
</br>
This will add a relation between my devenv machine and a machine running mongo (which I would have deployed previously)
From here, I have a cloud machine up and running with all the tools I need, and a mongo db ready to use.Anonymoushttp://www.blogger.com/profile/11476264728618297782noreply@blogger.com2tag:blogger.com,1999:blog-521133349049053228.post-20492583448652922412013-03-19T17:27:00.000+00:002013-03-19T17:28:35.969+00:00Some useful development patterns via the raspberry pi & minecraftProgramming minecraft on the raspberry pi is not just fun, it's also a great chance to learn some of the cool things that can be done from the linux command line.
</br>
We're going to look at two problems:
<ol>
<li>How can I edit files on my raspberry pi using an editor on my pc?</li>
<li>How can I run minecraft on a raspberry pi with a screen connected but without a mouse and keyboard connected?</li>
<li>Minecraft fails to start: something about a problem loading shared library libGLESv2.so</li>
</ol>
The Setup
<ul>
<li>1 raspberry pi connected to a network and a monitor</li>
<li>1 PC (I'm using linux, it should work equally well on osx, not sure about windows.)</li>
</ul>
<h3>How can I edit files on my raspberry pi using an editor on my pc?</h3>
</br>
Create an empty folder on your PC:
<pre class="prettyprint">
mkdir ~/pi
</pre>
Then use sshfs to mount it
<pre class="prettyprint">
sshfs pi@192.168.1.82: ~/pi/
</pre>
Where pi is my user name and 192.168.1.82 is the ip address of the pi.
</br>
Now, on my pc if I cd to ~/pi I can see the files in the home directory of my pi, which means I can open them on my pc using my editor of choice
</br>
</br>
<h3>How can I run minecraft on a raspberry pi with a screen connected but without a mouse and keyboard connected?</h3>
</br>
From your pc ssh into the pi specifying -X
<pre class="prettyprint">
ssh -X pi@192.168.1.82
</pre>
This turns on X11 forwarding - you don't need to know what that means for now, just start minecraft from inside ssh. You should see an empty minecraft window appear
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdUe6_gfIS_aJtbOZuTtiCsD0pspKc5Yl4j8ILDseOz6-ptItUGEIM82yhzK36OutY3v_YGMVpIsWkw7nVLlq6uBltKnAqERauXRtmSdQtgAkGL57Gqnfc1_tGiu8DNfgSlDTYqRStiR8/s1600/minecraft.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdUe6_gfIS_aJtbOZuTtiCsD0pspKc5Yl4j8ILDseOz6-ptItUGEIM82yhzK36OutY3v_YGMVpIsWkw7nVLlq6uBltKnAqERauXRtmSdQtgAkGL57Gqnfc1_tGiu8DNfgSlDTYqRStiR8/s320/minecraft.png" /></a>
</br>
Now take a look at your monitor, you should see minecraft there and be able to use your laptop's mouse and keyboard to move yourself around
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj4UsrjCSKiue-3Wm0rafZ2WVwAmaLZm3prvzCYBSKJRaGefe6NYE4EAFX5Z8pA09lLDX5C7WJ33vwQjEtSFQ-8eRNE1d0HCQvKrQAIFY5nI1wD6YPiZxUj7xTO7U1WeGvMJUyWwGi0SbE/s1600/2013-03-19+13.17.28.jpg" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj4UsrjCSKiue-3Wm0rafZ2WVwAmaLZm3prvzCYBSKJRaGefe6NYE4EAFX5Z8pA09lLDX5C7WJ33vwQjEtSFQ-8eRNE1d0HCQvKrQAIFY5nI1wD6YPiZxUj7xTO7U1WeGvMJUyWwGi0SbE/s320/2013-03-19+13.17.28.jpg" /></a>
</br>
As a bonus, you should be able to drag the window around on your pc, and have the window move around on your pi as well
</br>
</br>
<h3>Minecraft fails to start: something about a problem loading shared library libGLESv2.so</h3>
</br>
When I run minecraft I get this:
<pre class="prettyprint">
./minecraft-pi
error while loading shared libraries: libGLESv2.so: cannot open shared object file: No such file or directory
</pre>
Shared libraries are essentially bits of code that are shared amongst many programs. You can see what shared objects a program needs by typing
<pre class="prettyprint">
ldd ./minecraft-pi
</pre>
You might see some stuff marked "not found" at the top
<pre class="prettyprint">
/usr/lib/arm-linux-gnueabihf/libcofi_rpi.so (0x4022c000)
libGLESv2.so => not found
libEGL.so => not found
libbcm_host.so => not found
libpng12.so.0 => /lib/arm-linux-gnueabihf/libpng12.so.0 (0x40182000)
libSDL-1.2.so.0 => /usr/lib/arm-linux-gnueabihf/libSDL-1.2.so.0 (0x40235000)
libstdc++.so.6 => /usr/lib/arm-linux-gnueabihf/libstdc++.so.6 (0x402c8000)
libm.so.6 => /lib/arm-linux-gnueabihf/libm.so.6 (0x401aa000)
</pre>
To help linux find some of these shared object you can specify an option on the command line before you run your command:
<pre class="prettyprint">
LD_LIBRARY_PATH=/opt/vc/lib ./minecraft-pi
</pre>Anonymoushttp://www.blogger.com/profile/11476264728618297782noreply@blogger.com1tag:blogger.com,1999:blog-521133349049053228.post-8002642257836056532013-01-17T15:14:00.000+00:002013-01-18T12:02:13.541+00:00Using go to unmarshal json lists with multiple typesEveryday I seem to be writing go code to parse a json string, and this problem
seems to come up often enough for me to write about it. Thanks to adg and asoko on #go-nuts for their suggestions.
<h2>The Problem</h2>
Given a list of json objects of different types (lets say People and Places). You want to Unmarshal them into two lists.
A list of all the people and a list of all the places.
<h2>A bit more definition</h2>
Let's use this json string
<pre class="prettyprint">
{
"things": [
{
"name": "Alice",
"age": 37
},
{
"city": "Ipoh",
"country": "Malaysia"
},
{
"name": "Bob",
"age": 36
},
{
"city": "Northampton",
"country": "England"
}
]
}
</pre>
To help us write some code, let's give ourselves a function, which should be self explanatory:
<pre class="prettyprint lang-go">
func solution(jsonString []byte) ([]Person []Place) {}
</pre>
And some structures
<pre class="prettyprint">
type Person struct {
Name string
Age int
}
type Place struct {
City string
Country string
}
</pre>
I've got two solutions to this problem. I would love to know of better ways.
<h2>SolutionA: map and type assert</h2>
If we tell json to unmarshal into a map we can get it to deal with the parts we know
about, and the rest of it will go into an interface{}. As we loop over the json
structures we use what we do know about the structures to pass the interface{} to
some helper functions what will create one of our structs and add it to our list.
Because the map we take in is a map[string]interface{} we will need to type assert our
values
<pre class="prettyprint">
func solutionA(jsonStr []byte) ([]Person, []Place) {
persons := []Person{}
places := []Place{}
var data map[string][]map[string]interface{}
err := json.Unmarshal(jsonStr, &data)
if err != nil {
fmt.Println(err)
return persons, places
}
for i := range data["things"] {
item := data["things"][i]
if item["name"] != nil {
persons = addPerson(persons, item)
} else {
places = addPlace(places, item)
}
}
return persons, places
}
func addPerson(persons []Person, item map[string]interface{}) []Person {
name, _ := item["name"].(string)
age, _ := item["age"].(int)
person := Person{name, age}
persons = append(persons, person)
return persons
}
func addPlace(places []Place, item map[string]interface{}) []Place {
city, _ := item["city"].(string)
country, _ := item["city"].(string)
place := Place{city, country}
places = append(places, place)
return places
}
</pre>
<h2>SolutionB: Mixed Type struct</h2>
This solution involves creating an interim struct which can be used to represent
either a person or a place
<pre class="prettyprint">
type Mixed struct {
Name string `json:"name"`
Age int `json:"age"`
City string `json:"city"`
Country string `json:"country"`
}
</pre>
With this struct we can then unmarshal our json string into a list of these mixed
types. As we loop over our Mixed structs we just need to examine each one to
work out which type it represents, and then build the right struct from it
<pre class="prettyprint">
func solutionB(jsonStr []byte) ([]Person, []Place) {
persons := []Person{}
places := []Place{}
var data map[string][]Mixed
err := json.Unmarshal(jsonStr, &data)
if err != nil {
fmt.Println(err)
return persons, places
}
for i := range data["things"] {
item := data["things"][i]
if item.Name != "" {
persons = append(persons, Person{item.Name, item.Age})
} else {
places = append(places, Place{item.City, item.Country})
}
}
return persons, places
}
</pre>
These are just two ways I've used to solve these problems, I'd love to know how
others have done it.
<h2>SolutionC: json.RawMessage (Updated 18Jan13)</h2>
Thanks to Jordan's comment and zemo on reddit <a href="http://www.reddit.com/r/golang/comments/16r67l/using_go_to_unmarshal_json_lists_with_multiple/"></a> there is another solution.
Using the json.RawMessage structure in the json package we can delay unmarshalling the json structures in the list. We can then go through our list and unmarshal each of them into the correct type
<pre class="prettyprint">
func solutionC(jsonStr []byte) ([]Person, []Place) {
people := []Person{}
places := []Place{}
var data map[string][]json.RawMessage
err := json.Unmarshal(jsonStr, &data)
if err != nil {
fmt.Println(err)
return people, places
}
for _, thing := range data["things"] {
people = addPersonC(thing, people)
places = addPlaceC(thing, places)
}
return people, places
}
func addPersonC(thing json.RawMessage, people []Person) []Person {
person := Person{}
if err := json.Unmarshal(thing, &person); err != nil {
fmt.Println(err)
} else {
if person != *new(Person) {
people = append(people, person)
}
}
return people
}
func addPlaceC(thing json.RawMessage, places []Place) []Place {
place := Place{}
if err := json.Unmarshal(thing, &place); err != nil {
fmt.Println(err)
} else {
if place != *new(Place) {
places = append(places, place)
}
}
return places
}
</pre>
Here's the full gist:
<script src="https://gist.github.com/4556603.js"></script>Anonymoushttp://www.blogger.com/profile/11476264728618297782noreply@blogger.com12tag:blogger.com,1999:blog-521133349049053228.post-72898367571569410032013-01-07T18:56:00.002+00:002013-01-08T11:21:29.380+00:00Using vim's path to speed up your Go projectIf you're working on a project in Go, and you're using vim, then you really need to check out path:<br/>
<pre>
:help path
</pre>
Here's a very quick tour.<br/><br/>
Add this to you .vimrc:
<pre>
set path +=/your/projects/gopath/src/**
</pre>
** 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. <br/>
<h2>:find</h2>
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:
<pre>
:e /package/v10/another-dir/god.go
</pre>
You just need:
<pre>
:find god.go
</pre>
You also get tab-completion for free - just in case you can't remember the file's name.
<h2>gf</h2>
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:
<pre>
package main
import (
"code.google.com/p/go.net/websocket"
)
</pre>
By "gf"ing over the import vim will open the directory, giving you the list of files in that package.<br/><br/>
Just don't dare navigate the directory using the arrow keys!
<br/><br/>
<h2>Update: 8th Jan 2012</h2>
On the subject of vim and Go it's worth remembering this little gem:
<pre>
au BufWritePost *.go !gofmt -w %
</pre>
Whenever a Go file is saved gofmt will be run against it.
Anonymoushttp://www.blogger.com/profile/11476264728618297782noreply@blogger.com1tag:blogger.com,1999:blog-521133349049053228.post-55488952368333177892012-12-30T23:42:00.000+00:002012-12-30T23:44:00.167+00:00go(ing) underLots 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!<br/><br/>
One of the things that go lets you do is pass functions around.<br/>
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).<br/>
A great example is Pythagoras' theorem:
<ol>
<li>You perform some 'pre-processing' on each value (squaring)</li>
<li>You add all the values up</li>
<li>You square root the result</li>
</ol>
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!<br/>
<script src="https://gist.github.com/4415975.js"></script>Anonymoushttp://www.blogger.com/profile/11476264728618297782noreply@blogger.com0tag:blogger.com,1999:blog-521133349049053228.post-45769641781223326192012-10-22T14:23:00.001+01:002013-05-23T15:34:32.462+01:00Running juju locally on 12.04 (or getting over agent-status pending)I've just tried following the instructions <a href="https://juju.ubuntu.com/docs/getting-started.html">here</a> to get juju running locally on my brand new Ubuntu 12.04 (desktop) system.<br />
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.<br />
<br />
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.<br />
<br />
All it took was to add a rule to ufw:<br />
<pre>
sudo ufw allow from 192.168.122.0/24 to any<br />
</pre>
Hope this helps someone who was stuck in the same way I was.
Anonymoushttp://www.blogger.com/profile/11476264728618297782noreply@blogger.com1tag:blogger.com,1999:blog-521133349049053228.post-28278852396760390382012-09-15T16:29:00.000+01:002012-09-24T07:45:53.770+01:00Clojure, Vim and the delay sending to screenSo, long story short: I'm use vim for writing clojure (and everything else for that matter)<br />
<br />
If you're following the setup <a href="http://writequit.org/blog/?p=386">How I develop Clojure with Vim</a> 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.<br />
<br />
Add the following lines to you ~/.screenrc<br />
<br />
<pre>msgwait 0
msgminwait 0
</pre>
<br />
And there you have it, instant code from vim to your REPL!
Anonymoushttp://www.blogger.com/profile/11476264728618297782noreply@blogger.com0tag:blogger.com,1999:blog-521133349049053228.post-16313829348000121022012-08-14T17:13:00.000+01:002012-08-14T17:13:34.673+01:00Another clojure macro tutorial (that no one should follow)Disclaimer: This post shows you something you can do with macros. <b>Not something you should do.</b><br />
<br />
I like python.<br />
<br />
You define functions<br />
<br />
<pre>def something(x, y):
return x + y
</pre>
<br />
And you can document functions
<br />
<pre>def something(x, y):
"""Adds two things together"""
return x + y
</pre>
<br />
I also like clojure.<br />
<br />
You can define functions
<br />
<pre>(defn something [x y]
(+ x y))
</pre>
<br />
And you can document functions
<br />
<pre>(defn something
"Adds to things together"
[x y]
(+ x y))
</pre>
<br />
Documentation before the arguments? Despicable! If only there was a way of putting them in the right order.<br />
<br />
Well, for the sake of argument let's try<br />
<br />
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:<br />
<br />
(function-name doc-string args function-body)<br />
<br />
and we want to be able to make a function using the argument order<br />
<br />
(function-name args doc-string function-body)<br />
<br />
The first rule of matco club is "Don't write macros". So lets try:<br />
<br />
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.<br />
<br />
<pre>(defndoc something [x y]
"Adds to things together"
(+ x y))
</pre>
<br />
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.<br />
<br />
<pre>(defn defndoc [fname args docs & body]
(defn fname docs args body))
</pre>
<br />
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<br />
<br />
<pre>(defmacro defndoc [fname args docs & body]
`(defn ~fname ~docs [~@args] ~@body))
</pre>
<br />
Let's discuss the differences between this and our non-macro attempt.<br />
<br />
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.<br />
<br />
The next symbol is unquote (~) which tells clojure to evaluate these symbols.<br />
<br />
The last symbol is unquote-split (~@) which tells clojure that there is a list of things here that needs to be expanded in place.<br />
<br />
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<br />
<br />
<pre>(clojure.core/defn something "Adds two things together" [x y] (+ x y))
</pre>
<br />
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.<br />
<br />
Now don't let me ever catch you doing this!Anonymoushttp://www.blogger.com/profile/11476264728618297782noreply@blogger.com4tag:blogger.com,1999:blog-521133349049053228.post-41655452717553719792012-06-30T23:30:00.000+01:002012-06-30T23:30:45.843+01:00Synchronized (promise) queues in Clojure<div>I've mentioned to a few people at work recently that my favourite built in library in python is the <a href="http://docs.python.org/library/queue.html">queue</a>. 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.<br />
<br />
In fact, I like it so much I've decided to write it in clojure<br />
<br />
<a href="https://github.com/mattyw/sync-q">sync-q</a><br />
<br />
There's instructions there about how to use it in your own applications<br />
<br />
The idea started back when I was reading <a href="http://www.clojurebook.com/">Clojure Programming</a> and discovered promises that came in with clojure 1.3.0.<br />
<br />
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<br />
<br />
<pre>
(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
</pre>
<br />
From this point on that promise isn't available for delivering to as it already has a value.<br />
<br />
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.<br />
<br />
creating a new queue becomes creating a queue with one promise in it.<br />
<br />
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.<br />
<br />
getting is just as simple as getting the item at the front of the queue.
<br />
<br />
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
<br />
<br />
The first version took a couple of hours and was fun, it will be great to know if anyone makes use of it.</div>Anonymoushttp://www.blogger.com/profile/11476264728618297782noreply@blogger.com2tag:blogger.com,1999:blog-521133349049053228.post-35482952963388182292012-06-22T19:12:00.000+01:002012-06-22T19:12:58.669+01:00NoClassDefFoundError: scala/ScalaObjectI thought I'd start the weekend by trying to call scala code from a java project in eclipse.<br />
<br />
I wanted to try it without having to compile the scala code to a jar first. The whole exercise was very painless.<br />
<br />
Define some class in scala:<br />
<br />
<br />
<pre><span style="color: #38761d;">class Cell(val row: Int, val col: Int, val slot: Int) {
}</span></pre>
<br />
Then call it in your java project:<br />
<br />
<br />
<pre><span style="color: #274e13;">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());
}
}</span>
</pre>
<br />
<br />
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.Anonymoushttp://www.blogger.com/profile/11476264728618297782noreply@blogger.com0tag:blogger.com,1999:blog-521133349049053228.post-6409851462737200082012-04-01T02:09:00.000+01:002012-04-01T02:09:31.000+01:00Williams CompressionNow April is with us I thought It's about time I shared an idea for a new compression scheme I've been thinking about. In short, it converts all files into ~16 bytes of data. Here's how it works:<br />
<br />
for bit in file:<br />
if bit is ON:<br />
on += 1<br />
else:<br />
off += 1<br />
<br />
output "{on},{off}".<br />
<br />
Here's how it looks:<br />
<br />
Led Zeppelin Stairway to Heaven: 37810200, 39509240<br />
<br />
Stephen Merchant Standup: 283554505, 314875207<br />
<br />
Genesis More Fool Me: 14985253, 15456251<br />
<br />
In fact. Here's the whole album:<br />
<br />
Selling England by the pound: 119882024, 316167032<br />
<br />
It's quite lossy, but it allows you to send most files in a single tweet. You could even send the extended version of the Lord of the Rings.<br />
<br />
<br />
<br />
<br />Anonymoushttp://www.blogger.com/profile/11476264728618297782noreply@blogger.com0tag:blogger.com,1999:blog-521133349049053228.post-71441775461093314952012-02-23T22:19:00.000+00:002012-02-23T22:19:25.721+00:00NeoBlog - My Neo4j Challenge EntryIt's been an interesting start to the year. Towards the end of January I purchased a copy of <a href="http://pragprog.com/book/rwdata/seven-databases-in-seven-weeks">Seven Databases in Seven weeks</a> to give myself a boost in the world of nosql databases. Within a week I had discovered the <a href="http://neo4j-challenge.herokuapp.com/">neo4j challenge</a>. It seemed too good an opportunity to miss, so I embarked on writing an application for the competition. This is my write up of how it went.
<br />
<b>Some Design</b>
<br />
<br />
I decided early on that my focus was.
<br />
<ol>
<li>Learning about neo4j</li>
<li>Learning about writing web apps in python</li>
<li>Submitting an entry</li>
</ol>
<br />
<b>Getting Started</b>
<br />
<br />
With this in mind I spent a week playing around with neo4j via Nigel Small's excellent <a href="http://nigelsmall.name/2011/09/py2neo-and-me/">py2neo</a> library. I started off with modelling the london underground in neo4j, and playing around with finding routes around. <a href="http://yfrog.com/n1azrp">Here's a tweet</a> I made with a photo of part of the network. This was a great learning activity, I found a bug in the pyneo library which I fixed and Nigel was good enough to pull into his repo. You can see the commit <a href="https://github.com/nigelsmall/py2neo/commit/1d2644f24911750bf86fd5b5e9b7d5ff87c69026">here</a> This was my first real contribution to an open source project, which I was pretty pleased with.
<br />
<b><br /></b><br />
<b>The Blog Idea</b>
<br />
<br />
Despite having lots of fun playing I couldn't get it quite working the way I wanted, so I decided to keep it on the back burner, and try out another idea I had for the competition. One I knew wasn't going to take much. The idea was a for each node to be a post. Instead of using tags to connect similar posts you would just connect them with edges. It seemed simple enough - so off I went.
<br />
<b><br /></b><br />
<b>The Doing</b>
<br />
<br />
I'd not done any real web applications in python before - I few toy django applications, but django (and rails for that matter) always feel a bit heavyweight for my liking (that's a topic for another post) so I was looking forward to using <a href="http://flask.pocoo.org/">flask</a>. The application took shape quite quickly. I spent a few hours adding users and admin pages - but I felt this began to detract from the aim. My intention from the start was to keep the application simple, I felt that an application that would be shared for other people to clone from should be as small as possible. I wanted others to be able to understand the application in under ten minutes, by removing admin pages and users I managed to get rid of about half the code until I was down to an application that could do 3 things:<br />
<br />
<ul>
<li>Add a post</li>
<li>Link a post to another one</li>
<li>View all posts</li>
</ul>
<div>
Not especially ground breaking or shippable - but ok I believe as an example.<br />
<br />
<b>What would I do different next time?</b>
<br />
<br />
When I started, I wasn't totally sure how everything was going to end up, so I decided to play safe and use a language I was familiar with. Looking back at it, I wish I had taken the chance and written it in clojure, I think this would have been an ideal opportunity to play more with clojure.<br />
<br />
Something that didn't occur to me until after I deployed and I'm considering adding (I really should do at some point) is that when two people link a post two edges are created. I think instead an edge should have a weighting, and each user that creates a connection adds to the weight. You could then display similar posts in order of similarity. This idea is playing into some other work I'm doing - but I should really add to this one.<br />
<br />
<b>Summary</b>
<br />
<br />
I had lots of fun doing this. I learned a bit more about writing web apps. I learned a bit more about git. and I learned how to use neo4j, all in all, not bad for a few days work.<br />
<br />
References:<br />
<br />
<a href="http://neo4j-challenge.herokuapp.com/">Neo4j Challenge</a><br />
<br />
<a href="http://gensen.herokuapp.com/show/26">Neo Blog Entry</a><br />
<br />
<a href="https://github.com/mattyw/neo_blog">Neo Blog Source Code</a><br />
<br />
<br /></div>Anonymoushttp://www.blogger.com/profile/11476264728618297782noreply@blogger.com0tag:blogger.com,1999:blog-521133349049053228.post-24118472961622854792012-01-04T13:25:00.000+00:002012-01-05T06:11:28.984+00:00Under: A new Idiom from the J languageThanks to <a href="http://prog21.dadgum.com/121.html">this post</a> I've started the year discovering a language I never knew existed - and a cool little feature in it.<br />
<br />
Imagine you have a function called g<br />
<br />
<pre class="brush: python">g(5) #returns 10
</pre>
<br />
Now imagine you have another function which undoes whatever happened in g.
<br />
<pre class="brush: python">undo-g(10) #returns 5
</pre>
<br />
Not too impressive on the face of it. You could guess that g just multiplies by 2 and undo-g divides by 2. The J language comes with some of these built in. Which it calls obverse functions.
<br />
<pre> 4 + 4
8
4 +^:_1 (4)
0
</pre>
In this function +^:_1 effectively means apply the + function -1 times. You could do it twice:
<br />
<pre> 4 +^:_2 (4)
_4 (In J _4 means -4)
</pre>
Seem crazy? Stay with it...
<br />
<br />
How many times do you see this sort of pattern in your code?
<br />
<pre>OpenFile
ReadData
CloseFile
OpenSocket
SendData
CloseSocket
</pre>
Look familiar? Well, because J has the idea of obverse functions you get a lovely little syntax that J calls <b>Under</b> which covers this pattern.
In J it looks like this
<br />
<pre>f&.g x
</pre>
Which means apply g to x. Then apply f. Then apply the inverse of g.
<br />
<pre class="brush: python"> obverse(func(verb(x))) #J calls functions verbs
</pre>
The J documentation lists loads of <a href="http://www.jsoftware.com/jwiki/Essays/Under">cool definitions</a> you can build using under.
<br />
Here's the idea in clojure using the under pattern to construct a new definition of multiplication and addition.
<script src="http://gist.github.com/1560170.js?file=under.clj"></script>
It's a cool idea to start the year with. I wonder how many places I'll start seeing this pattern?
For more information about J check out the excellent <a href="http://www.jsoftware.com/jwiki/HenryRich?action=AttachFile&do=view&target=JforC20071003.pdf">J for C Programmers (Rich 2007)</a>Anonymoushttp://www.blogger.com/profile/11476264728618297782noreply@blogger.com0tag:blogger.com,1999:blog-521133349049053228.post-18966701863359692952012-01-03T06:34:00.000+00:002012-01-03T06:34:31.400+00:002011: A Retrospective<br />
<b>What goals did I set myself last year?</b><br />
<br />
1) Publish a blog entry or video that explains monads, teach someone at<br />
work how to use them.<br />
I've not done this, I think I understand monads, but I'm looking for someone who knows more than me to confirm I've got it right.<br />
<br />
2) Contribute to an open source project related to the arduino.<br />
<br />
I didn't do this, I spent the first 3 months playing with the arduino before I moved on to other things. I designed a simple messaging system in google app engine, and then my interest in the arduino tailed off. I could open the source for this, might be an interesting idea.<br />
<br />
3) Finish one of my articles and submit it to some publishers for<br />
consideration to publish.<br />
<br />
Not really sure what I meant by this. But I spoke at two conferences this year.<br />
<br />
<b>What else did I achieve in 2011?</b><br />
<br />
Touch Typing<br />
<br />
I commited myself to learning to touch type during the year. Thanks to a lot of support from others and from a wide range of freely available tools I'm now typing comfortably above 50WPM - and hoping this will increase as I continue practising - All my blog posts are now proudly touch typed!<br />
<br />
Test Driven Development<br />
<br />
I started work on a new product this year at work, and from the offset everyone on the team was encouraged to do TDD. We're ending the year with the product deployed with 80% test coverage - not perfect but not bad. We've also written our own automated acceptance test suite, which tests all of the things that are above the level of our unit test. We don't yet have a way of measuring test coverage here but it seems reasonable that including the automated suite will push our actual coverage above 80%.<br />
<br />
Functional Programming<br />
<br />
I've played with functional programming at various point in the year. The end of the year has seen me focus more on clojure, and all that lisp languages offer, but Haskell is still there in the background. I need to get some experience building medium sized applications in Clojure or Haskell to increase my confidence<br />
<br />
Emacs & Vim<br />
<br />
This year I added Emacs and Vim to my list of editors I am comfortable with. I think I'm more on the side of sticking with emacs. But time will tell.<br />
<br />
<b>Summary</b><br />
<br />
I think it's clear that I deviated from my goals in some pretty dramatic ways. Looks like I need to examine my priorities more often.<br />
<br />
<b>Plans for 2012</b><br />
<br />
Programming in Schools (Codemanship Teacher-Practitioner Exchange)<br />
<br />
Help teach Ryan enough stuff so he can teach a class in programming.<br />
<br />
Back to Basics: Algorithms and Data Strucutres<br />
<br />
It's become clear this year that this is an area of my knowledge that needs some attention. I've signed up to Tim Roughgarden's <a href="http://www.algo-class.org/">Design and Analysis of Algorithms Course</a> By the end of the year I need to have blogged at least once about and algorithm and once about a data structure.<br />
<br />
Functional Programming in Clojure<br />
<br />
By the end of the year I need to be comfortable enough to do a project in clojure.<br />
<br />
DSLs<br />
<br />
I need to have written a dsl and use it for something.<br />
<br />
Review the Retrospective<br />
<br />
I should review this post half way throught the year and update if needed.<br />
<div>
<br /></div>Anonymoushttp://www.blogger.com/profile/11476264728618297782noreply@blogger.com0tag:blogger.com,1999:blog-521133349049053228.post-37734076179252309562011-12-02T09:38:00.001+00:002011-12-03T00:11:32.946+00:00String equality, identity and interning in PythonIn a list of things I should have already known comes this. The difference between using 'is' and == on strings in Python.
<p>Let's look at two strings. One unicode (u"unicode string") and one not "not unicode string".</p>
<pre class="brush: bash">
Python 2.7.2+ (default, Oct 4 2011, 20:03:08)
>>> type("foo")
type 'str'
>>> type(u"foo")
type "unicode"
>>> u"foo" == "foo"
True
>>> u"foo" is "foo"
False
</pre>
<p>So using == shows the two strings as equal, and 'is' doesn't. What's going on here?</p>
<p>Python <a href="http://en.wikipedia.org/wiki/String_interning">interns</a> its strings. Which means only one copy of each distinct string is stored. You can see this by using the built-in function id() to see the identity of our strings.</p>
<pre class="brush: bash">
>>> a = "foo"
>>> b = "foo"
>>> c = u"foo"
>>> print id(a)
3074129864
>>> print id(b)
3074129864
>>> print id(c)
3074128400
</pre>
You can see our normal strings have the same id because they are the same object. Our unicode string has a different id to our two 'normal' strings. Using the == operator asks python to compare equality of our two strings. Using 'is' compares the identity. As our unicode and normal string are different objects, comparing with 'is' returns false.
<p>I wonder how many of us are guilty of misusing 'is' on strings?</p>Anonymoushttp://www.blogger.com/profile/11476264728618297782noreply@blogger.com0tag:blogger.com,1999:blog-521133349049053228.post-35433597705974159192011-11-29T20:57:00.001+00:002011-12-02T10:11:15.131+00:00Heroku and UbuntuSo, I've just spent an hour or two trying to make a new heroku app on my ubuntu machine, and I was getting nowhere.
<pre>
$git push heroku master
Agent admitted failure to sign using the key.
Permission denied (publickey).
fatal: The remote end hung up unexpectedly
</pre>
I'd followed all the usual help on stackoverflow and heroku's excellent help section but was making no progress. Until I discovered <a href="http://unfuddle.com/community/forums/4/topics/878">this</a>.
The link to the bug report didn't work for me, but it's clear that I needed to set this SSH_AUTH_SOCK=0 environment variable first:
<pre>
$export SSH_AUTH_SOCK=0
$git push heroku master
</pre>
And now we're off!Anonymoushttp://www.blogger.com/profile/11476264728618297782noreply@blogger.com9tag:blogger.com,1999:blog-521133349049053228.post-14760943681065082632011-10-09T11:48:00.000+01:002011-10-09T11:49:30.457+01:00The Sieve of Eratosthenes in PythonWhilst working on the <a href="http://mattyjwilliams.blogspot.com/2011/10/10-io-one-liners-to-impress-your.html">10 Io one liners to impress your friends</a> post I felt I needed to turn to python to complete number 10 - the Sieve of Eratosthenes. My intention was to understand it in python as best I could, then simplify the python code until I had one line I could try to translate into Io. This post is about that attempt.
<br/>
We start off by trying to translate the description in the <a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes">wikipedia</a> article into python line by line. Which gives us the following.
<pre class="brush: python">
def esieve(n):
primes = range(2, n+1)
p = 2
while p < n:
for i in range(p, n+1):
if p*i in primes:
primes.remove(p*i)
p += 1
return primes
</pre>
9 Lines isn't bad for a start, but that if statement can be cleaned up. What if instead of looking for items in our list one at a time. We make a new list of items to be removed, and remove them all at the end? We could use a set to hold our lists. This lets us use the minus (-) operator to give us a new set of items not in our marked set.
<pre class="brush: python">
def shorter_esieve(n):
marked = set()
p = 2
while p < n:
for i in range(p, n+1):
marked.add(p*i)
p += 1
return sorted(set(range(2, n+1)) - marked)
</pre>
We only removed one line in that last attempt. Not great. But it looks like we're using a while loop and incrementing each step. Why don't we just do a for?
<pre class="brush: python">
def shorter_esieve(n):
marked = set()
for p in range(2, n+1):
for i in range(p, n+1):
marked.add(p*i)
return sorted(set(range(2, n+1)) - marked)
</pre>
6 lines, getting better. Now here is the magic. We're using two for loops to generate a set of values. So we can just use a list comprehension to build our list, which we then use to make our marked set.
<pre class="brush: python">
def shorter_esieve(n):
marked = set([p* i for p in range(2, n+1) for i in range(p, n+1)])
return sorted(set(range(2, n+1)) - marked)
</pre>
And moving the assignment inline
<pre class="brush: python">
def much_shorter_esieve(n):
return sorted(set(range(2, n+1)) - set([p*i for p in range(2, n+1) for i in range(p, n+1)]))
</pre>
And there we have it. The Sieve of Eratosthenes in one line of python.
If you'd rather watch the refactoring happening step by step. Here's a video, set to suitable music.
<br/>
<iframe width="560" height="315" src="http://www.youtube.com/embed/JQB5AnCy4Uw" frameborder="0" allowfullscreen></iframe>Anonymoushttp://www.blogger.com/profile/11476264728618297782noreply@blogger.com0tag:blogger.com,1999:blog-521133349049053228.post-37011032471790148302011-10-08T21:56:00.000+01:002011-10-08T21:57:41.976+01:0010 Io one liners to impress your friendsIt's been ages since I've done anything with the Io language. But after seeing this spate of 10 [language] one liners to impress your friends in<a href="http://varvello.blogspot.com/2011/06/test.html"> Davide Varvello's post</a> I thought I would spend the evening trying it with Io.
Here's the gist:
<script src="https://gist.github.com/1272845.js?file=one_liners.io"></script>
According to Davide's post the others in this category so far are:
<br/>
<a href="http://programmingzen.com/2011/06/02/10-ruby-one-liners-to-impress-your-friends/">Ruby</a>
<br/>
<a href="http://solog.co/47/10-scala-one-liners-to-impress-your-friends/">Scala</a>
<br/>
<a href="http://ricardo.cc/2011/06/02/10-CoffeeScript-One-Liners-to-Impress-Your-Friends.html">CoffeeScript</a>
<br/>
<a href="http://blog.fogus.me/2011/06/03/10-haskell-one-liners-to-impress-your-friends/">Haskell</a>
<br/>
<a href="http://freegeek.in/blog/2011/06/10-clojure-one-liners/">Clojure</a>
<br/>
<a href="http://codeblog.dhananjaynene.com/2011/06/10-python-one-liners-to-impress-your-friends/">Python</a>
<br/>
<a href="http://arturoherrero.com/2011/06/04/10-groovy-one-liners-to-impress-your-friends/">Groovy</a>
<br/>
I'd love to see more. It's a great little activity to get started with a language.Anonymoushttp://www.blogger.com/profile/11476264728618297782noreply@blogger.com1tag:blogger.com,1999:blog-521133349049053228.post-54360702781222977282011-09-26T22:59:00.000+01:002011-09-26T22:59:18.412+01:00Chicken Scheme on OS X 10.7 (Lion)I'm making another attempt at going through <a href="http://mitpress.mit.edu/sicp/">SICP</a> doing all of the exercises. I've decided that <a href="http://www.call-cc.org/">chicken scheme</a> is going to be the way I will do it. But to get it going on Lion there are some hoops to jump through thanks largely to a new version of gcc.<br />
<br />
The method I have found that works is.<br />
<br />
1) Download the source from the chicken scheme website.<br />
<br />
2) Following the advice <a href="http://comments.gmane.org/gmane.lisp.scheme.chicken/15559">here</a> you should pass new compiler options to make:<br />
<pre>make C_COMPILER=gcc-4.2 PLATFORM=macosx
</pre><br />
3) To install the additional eggs you will need to pass new options to csc like so:<br />
<pre>CSC_OPTIONS='-cc gcc-4.2' chicken-install
</pre>Anonymoushttp://www.blogger.com/profile/11476264728618297782noreply@blogger.com0tag:blogger.com,1999:blog-521133349049053228.post-49955279419905120802011-09-25T17:05:00.000+01:002011-09-25T17:05:29.170+01:00Software Craftsmanship & The Second ChasmI've just been reading William Pietri's post <a href="http://agilefocus.com/2011/02/21/agiles-second-chasm-and-how-we-fell-in/">Agile's second chasm</a> in which he discusses why ideas, such as agile get watered down as they get taken up by the mainstream. I think there's some stuff hear that applies to the craftsmanship movement. You could probably argue whether or not the craftsmanship movement has passed the first chasm yet, But for the moment lets suppose that it has. What are the chances that we might fall into the second one that William describes in his post?
<p></p>
<b>A Common theme</b>
<p></p>
First off, both ideas have a manifesto - this in itself isn't a bad thing, manifestos are great at getting like minded people talking. The problem is it allows people to 'sign up' to an idea that they have no intention of fulfilling. I've seen CVs that boast about being signatories of the agile manifesto, but have never been part of an agile team. I've been invited to "daily scrum" meetings that were 2-hour-sit-down-and-go-through-spreadsheets-and-gantt-charts meetings. It seems to me that Craftsmanship also has the same "difficult to test" quality to it. Craftsmanship isn't so much a title - as a way of doing things. So you won't really know if someone is a craftsman unless you watch them doing their job over time. I wonder how long it is before I see a CV with a "Software Craftsman" certification, but is willing to implement a mess in order to meet a schedule?
<p></p>
<b>A Difference</b>
<p></p>
The way I see it, there is one important difference with the craftsmanship movement. Its focus is on individuals and their interactions, not teams. Ok, I know words like 'partnership' and 'community' are used in the manifesto, But the point is that it's up to the individual to better themselves and the code they work on, productive customer partnerships are something the individual should seek because it helps them better understand what it is they are being asked to do. The community is all about individuals meeting other individuals to help themselves improve. So how do I think this helps? Because it operates at a completely different level.
<p></p>
Agile has continuous improvement built into it, and of course, if you're doing it properly you will always change and adapt. But it has a core set of practices that are easy to get started with that allow you to call yourself an agile team.
But Craftsmanship doesn't have this (at the moment). Try to do a quick google search for what software craftsmanship is, and you'll struggle for a firm definition. The best I've found so far is <a href="http://thecleancoder.blogspot.com/2011/01/software-craftsmanship-what-it-all.html">Uncle Bob's</a> but even that doesn't have any specifics. Really, the only idea behind software craftsmanship is: <b><i>"We will write the best code we can by constantly reviewing how to write the best code we can"</i></b>. It's a continuous learning process (one that is built into the medical profession by law). Continuous improvement on a personal level is not something that's so easy to fakeAnonymoushttp://www.blogger.com/profile/11476264728618297782noreply@blogger.com0tag:blogger.com,1999:blog-521133349049053228.post-44533777725709221432011-09-14T21:38:00.000+01:002011-09-14T22:01:17.757+01:00Python ShelvingI discovered the cool shelve module earlier. Sometimes it's nice to be able to store python objects using the pickle module. The shelve module builds on this by providing a dictionary that you can put objects into and access later. Check out this gist
<script src="https://gist.github.com/1217714.js?file=shelving.py"></script>Anonymoushttp://www.blogger.com/profile/11476264728618297782noreply@blogger.com0tag:blogger.com,1999:blog-521133349049053228.post-6694361166372723972011-09-13T20:55:00.000+01:002011-09-14T22:01:41.474+01:00Dojos and KatasI recently got back from a very successful <a href="http://socrates2011.pbworks.com/">Socrates 2011</a> (Software craftsmanship and testing camp) where I spent just over 2 days hanging around with a bunch of smart people and spent the whole time either writing code, or talking about code. I was involved in a lot of discussions about software craftsmanship as well as a few coding dojos. Dojos and Katas were always something I've done when I've felt like it, but never as part of a habit. Following the conference I started using Stefan Roock's great site <a href="http://www.codersdojo.com">codersdojo</a>. It allows you to work on a kata, and upload it to the site when you're done. It provides statistics about how long each step took as well as when the tests were passing or failing:
<ul>
<li><a href="http://www.codersdojo.com/statistics/6938f7e212ae5ea89596ac280c0df6016c17e86e">FizzBuzz Python</a></li>
<li><a href="http://www.codersdojo.com/statistics/17c34be04a8442a9b192721507e213de1ddceb13">FizzBuzz Clojure</a></li>
<li><a href="http://www.codersdojo.com/statistics/7fa9dce4cba7a14fc11ffe05dfa34aaae0e7956e">FizzBuzz Haskell</a></li>
</ul>
Thanks to Stefan for a great site and helping me add Haskell Support! Take a look at my haskell fizzbuzz kata below and please checkout Stefan's site.
<script src="https://gist.github.com/1214902.js?file=fizzbuzz.hs"></script>Anonymoushttp://www.blogger.com/profile/11476264728618297782noreply@blogger.com0tag:blogger.com,1999:blog-521133349049053228.post-41673193818773625862011-07-09T22:55:00.000+01:002011-09-14T22:02:12.986+01:00Touch Typing Part 2Well, week 2 of learning to touch type is over. I'm in the region of 50 wpm. Which is almost where I wanted to be (just 10 wpm short). I've spent most of this week trying to improve accuracy, I can recommend <a href="http://www.gnu.org/software/gtypist/">GNU Typist</a> which requires you to be 97% accurate to move on with the exercises. As a summary I would say it's only been two weeks and I'm just about at the speed I was at before I started, except now I don't need to look at the keyboard. I can definitely recommend giving touch typing a try if you haven't already.Anonymoushttp://www.blogger.com/profile/11476264728618297782noreply@blogger.com1tag:blogger.com,1999:blog-521133349049053228.post-54476277508872518242011-07-02T17:46:00.002+01:002011-09-14T22:02:25.059+01:00Touch Typing Part 1So, this the the first blog post I can claim to have touch typed. I realised about a week ago that while I was typing I was spending a large amount of time looking at the keyboard. So I decided I would fix it by learning to touch type. I'm getting a lot of support from the guys on the Software Craftmanship linkedin group. Here is a diary of my progress so far.<br />
<br />
<b>The starting point</b><br />
My current method of typing involves looking at the keyboard while I type and using my index fingers (and occasionally the other fingers for familiar patterns) I average about 55-60wpm doing this.<br />
<br />
<b>The aim</b><br />
Get to 60 wpm without needing to look at the keyboard (Although it would be nice if I could go faster than 60wpm).<br />
<br />
<b>Sunday</b><br />
Spent the day looking at some websites to see what would help. I read <a href="http://skilldrick.co.uk/2011/06/you-should-learn-to-type-properly/">Nick Morgan's blog post</a> and felt inspired. By the end of the day I was used to home row.<br />
<br />
<b>Monday</b><br />
Signed up to <a href=' http://www.typingweb.com'>Typingweb</a> and <a href='http://www.typeracer.com'>Typeracer</a>. I can get to about 16wpm but occasionally I need to force my fingers back onto home row. When I try to go fast they default back to old habits.<br />
<br />
<b>Tuesday</b><br />
By the afternoon my fingers were defaulting to home row, took a test on typing web and got up to 24wpm. Played typeracer in the evening and got up to 26wpm.<br />
<br />
<b>Wednesday</b><br />
Did 5 races before work. Pushed my average up to 28 but the competitive element pushed me into bad habits. By the evening my fingers were feeling more comfortable sitting on home row than it did on monday. Feels like all I need to do now is keep at it and hopefully the speed will come.<br />
<br />
<b>Thursday</b><br />
Felt myself falling back into old habits during the day. I reached 40wpm on typeracer but trying to go above that forces my fingers off home row.<br />
<br />
<b>Friday</b><br />
Spent the whole day at work coding while touch typing. I wasn't typing very quick. But I hardly looked at the keyboard at all.<br />
<br />
So that's it so far. I'm ending the week typing about half the speed I was going at the start of it (34wpm) but I no longer have to look at the keyboard while I'm doing it. So it's a step in the right direction. Now all I need to do is stick at it and hopefully I will start getting faster.<br />
<br />
Thanks to Pascal, Corey and the rest of the guys on the Software Craftsmanship group for their support!Anonymoushttp://www.blogger.com/profile/11476264728618297782noreply@blogger.com0