A home for those vital pieces of information, which make it all work. This site is initially just a container for Chris Dew's blog, but it may grow to be more than that. All from a UK/Linux/Haskell/Python/C perspective.

Immutable Containers in Scala

I'm evaluating Scala and am having a problem with it's immutable collections.

I want to make immutable collections, which are completely immutable, right down through all the contained objects, the objects they reference, ad infinitum.

Is there a simple way to do this?

The code below illustrates what I'm trying to achieve, and a nasty work around (ImmutablePoint).

UDP Echo Server in Scala

The UDP Echo Server seems to be the 'hello world' of network programming.

What is Scala?
Scala is a hybrid OO/functional programming language which runs on the Java Virtual Machine.

Simple encapsulation in Javscript.

In the spirit of 'doing the simplest thing which works', I've been using the following idiom for encapsulating data within objects. I'm still quite new to Javascript, so I'm sure others have been doing similar things for a long time, in a more elegant fashion (I must get round to reading the jQuery source).

A naive object is:

var account = { name    : "fred"
              , balance : 100.00
              , branch  : "Worcester"
              } ;

Goodbye Perl, Ruby and Python. Hello JavaScript.

I read Douglas Crockford's book and was rather inspired.

Now JavaScript is starting to grow up, with excellent projects including env-js, nodeJS, CommonJS, v8 and jQuery, what are the reasons to still use other 'scripting' languages. (I'm not using 'scripting' in a derogatory way - I suppose I mean dynamic/interpreted.)

How to install Debian Lenny on ARM on Qemu on Ubuntu Jaunty.

The hosting environment is assumed to be Ubuntu Jaunty, with qemu already installed.

First, get the files you'll need:

$ wget http://ftp.de.debian.org/debian/dists/lenny/main/installer-armel/current/images/versatile/netboot/initrd.gz
$ wget http://ftp.de.debian.org/debian/dists/lenny/main/installer-armel/current/images/versatile/netboot/vmlinuz-2.6.26-2-versatile

Create a disk image:

$ qemu-img create -f qcow hda.img 10G

Then start qemu:

$ qemu-system-arm -M versatilepb -kernel vmlinuz-2.6.26-2-versatile \

Thoughts on Bresenham's Algorithm in Haskell

I'm still hacking away at little bits of Haskell, gradually learning my way around.

I happened to find code for Bresenham's Algorithm on Rosetta Code. It made me uncomfortable - it looked like the author had pushed a C-shaped programme into Haskell-shaped syntax.

This was just my first impression - I don't know the language well enough to know when to use the State Monad - and I'm certainly not qualified to criticise the orginal author of the following code.

 

Using Python's ctypes library is easier than it looks.

It's this easy to write a programme which uses libc's open and read functions to print it's own source. See Python's documentation for more details.

#!/usr/bin/env python
 
import os
import sys
 
from ctypes import CDLL
 
MAX_CHARS = 1000    # maximum number of characters to read
 
if __name__ == "__main__":
    libc = CDLL("libc.so.6")
    fd = libc.open(sys.argv[0], os.O_RDONLY)
    buffer = " " * MAX_CHARS
    num_chars_read = libc.read(fd, buffer, MAX_CHARS)

Recombinant Hashes Demo

Sorry for the delay, I had more to learn about Apache's proxypass directive.

See http://www.finalcog.com/recombinant-hashes for some details of, and applications for, the algorithm.

The demo is now running, the urls are:

To use the demo:

  1. Open the two urls in two browser windows side-by-side.

Python with Curses with with_curses.

The 'with' keyword, introduced in Python 2.5, allows a nice way of handling the initialisation (and teardown) of curses applications.

The object used with 'with' needs to have two methods, __enter__ and __exit__.

The __enter__ method is called before the code in the 'with' block is executed. Whatever it returns can be assigned to a variable using the 'as' keyword.

The __exit__ method is called after the code in the 'with' block is executed, or if an exception is raised within the block.

Europython 2009 Wifi

Europython 2009 Wifi

Just drew this from a comment, made by a bloke on the row behind me at europython. Thanks to my employers, ApplianSys Ltd., for letting me attend. Enjoy.

Best UK linux wireless broadband, May 2009 - T-Mobile and Ubuntu 9.04.

Ubuntu 9.04 and t-mobile wireless broadband.

I bought a t-mobile 'USB stick 110' (which is actually a Huawai E160 dongle). It cost £40, including £20 credit. It's PAYG, costing £2/day, with a 3GB monthly limit.

I plugged it into my laptop and a new connection appeared in the network manager.

Sceptically, I clicked to enable it - and *it just worked*.

No messing with kernels, mode switching or anything else.

The E160 is detected as an E220, which is fully supported by linux.

chris@vostro:~$ lsusb | grep Huawei

kill -0

Introduction
I must admit, after a decade of working professionally with unix/linux, that I had never encountered kill -0 until last week.

What does the -0 signal do?
Nothing at all...

What is it useful for?
It returns 0 if the process exists, or 1 (and an error message on stderr) if it doesn't.

errno.h - C Error Codes in Linux

All the Linux/C error codes are listed below.

I occasionally google C error codes, but always end up grepping through /usr/include to find the answer (on Ubuntu 8.10). To save myself, and a few others, some time in the future...

/usr/include/asm-generic/errno-base.h

#ifndef _ASM_GENERIC_ERRNO_BASE_H
#define _ASM_GENERIC_ERRNO_BASE_H
 
#define	EPERM		 1	/* Operation not permitted */
#define	ENOENT		 2	/* No such file or directory */
#define	ESRCH		 3	/* No such process */
#define	EINTR		 4	/* Interrupted system call */

The Ultimate Budget Desktop Replacement for £624? Dell Vostro 1710 with Ubuntu 8.10 x86_64 review.

I recently purchased a Dell Vostro 1710 17" laptop, with 2.1GHz Core2 Duo, 4GB RAM and 1920x1200 display for £624 + VAT.

The requirement was for a new machine to be used as a 'desktop replacement' laptop. As I'm a software developer, it needed a big screen, lots of ram, and to have virtualisation support in the CPU. Fast OpenGL support didn't hurt either :-)

SSD Bonnie++ Benchmarks

I've been in the fortunate position to test a couple of SSDs recently. To round things off, I've also included results for a standard 7,200rpm SATA disk and a USB key.

The results are:

SAMSUNG MCBQE32G5MPP-0VA £400
seq. write: 65MB/s
seq. read: 96MB/s
random seeks: 7,511/s

OCZ VERTEX 00.PT1 £100
seq. write: 77MB/s
seq. read: 142MB/s
random seeks: 9,058/s

WDC WD1600AAJS-00B4A0 (normal 3.5" 7,200rpm hdd) £30
seq. write: 68MB/s
seq. read: 91MB/s
random seeks: 226/s

KINGSTON DATATRAVELER USB STICK
seq. write: 8MB/s
seq. read: 32MB/s
random seeks: 2,828/s

Python ConfigParser and lower case names

I was using Python's excellent ConfigParser to manipulate SolidDB's (don't ask) solid.ini configuration file (amongst other files).

I just couldn't figure out why SolidDB wasn't starting, until I realised that ConfigParser changes all the 'names' in an .ini file to lower case, by default. This behaviour can be fixed by simply substituting an identity as ConfigParser's optionxform function.

import ConfigParser
 
config_parser = ConfigParser.ConfigParser()
config_parser.optionxform = lambda x: x

How to undelete any open, deleted file on linux.

Introduction

There are many situations where a file has been deleted (typically by an overnight log-cleaning process), yet the inode is still held open by a process reading from, or writing to, it. Recovery of such a file is simple, regardless of whether it is on ext2, ext3, reiserfs or any other filesystem.

When a file is deleted in linux, it is simply 'unlinked'. The inode, which contains the file's data, is not deleted until all processes have finished with it. This is why processes can carry on writing to deleted files.

Decoupling time from physics.

It's possible to decouple time from physics, and it's especially easy in Haskell.

The (higher order) function below (lazily) accepts a list of events and yields a function which can give the historical or predicted displacement at any given time.

Note: The events do not have to be in chronological order - this provides a way to deal with lag in networked games.

Real World Haskell - Exercise Answers - Chapter 4

Page 84 - Question 1
This question seems quite obvious, just match the patterns for arguments which would return an empty list, and return Nothing instead.

safeHead :: [a] -> Maybe a
safeHead (x:xs) = Just x
safeHead _      = Nothing
 
safeTail :: [a] -> Maybe [a]
safeTail []     = Nothing
safeTail (x:[]) = Nothing
safeTail (x:xs) = Just xs
 
safeLast :: [a] -> Maybe a
safeLast []     = Nothing
safeLast (x:[]) = Just x
safeLast (x:xs) = Just (last xs)
 
safeInit :: [a] -> Maybe [a]
safeInit []     = Nothing
safeInit (x:[]) = Nothing

You can read a local file from javascript.

You can read a local file from javascript.

It turns out that you can access a local file from javascript.

To do so, you need a file upload field. When the user chooses a file to upload, javascript gets read access to that file immediately, even if the form is never submitted.

This access allows client-side javascript validation and processing of file upload fields.

Can anyone think of an interesting use for this? Some mundane possibilities are:

  • Resize a large image in javascript, before uploading it.
  • Virus-check a file in javascript (yes, very inefficient)
Syndicate content