32leaves.net

The A2 automaton

In the post “Evolution of the aliveness of cells in a cellular automaton” I promised that I’d post a paper about the automaton I used to generate the pictures and the subsequent video. So here it is. But be aware that this is my very first (semi) mathematical paper ever, so don’t be to harsh with critics :)

You may grab the PDF file here or read the iPaper below

There shall be recursion

Just a quick note that today I added one my most favored features of LISP to uLISP: the

1
recur

keyword. It allows you to call a the lambda you’re in at the point recur is written, thereby enabling the use of recursion without having to give the lambda a name.
I also implemented the

1
range

function which creates sequences of numbers and uploaded a rudimentary reference documentation.

This thing starts to become neat :-)

uLISP

As the rising amount of posts per unit of time shows: I got a loot of spare time to waste these days. This is mostly because we’re writting exams these days and constant learning simply doesn’t work out. So I had to get another project.
A short while ago I bought myself a TI92+ for the math exams. Just after it arrived and I had it connected to my computer I thought: “wouldn’t it be cool to have LISP on that thing”. So I started writing:

uLISP

Roughly two days later there is the first kinda releasable version of this neat little interpreter. It’s pretty much complete when it comes to features, but there are still some more things to do:

  1. Get rid of all the memory leaks
  2. Add the
    1
    recur

    keywork to allow anonymous recursion

  3. Port it to the TI92+

So, in case you want to have a look at it: here you go … the code is published under the terms of LGPL, so feel free to do with it whatever you like. But if it becomes SkyNet don’t blame me :-)

ArduinoFS

Yeah, three posts on one day: cool, huh :-)

Mostly this is just a sum up off the stuff I’ve done lately when I was not learning for my exams or have been to the Speicher. This time it’s propably the most useless peace of software that has ever been written on this planet (but still fun though). It’s filesystem using FUSE which allows you to mount the 512 bytes (yes, that half a kilobyte) of the ATmega168 of an Arduino in read-only.

That said, enjoy the code … play arround with it, or just shake your head and close this tab ;-)


DownloadGrab the code here

X11 automation during a presentation

The other day I was giving a presentation at UnFUG (during the Pimp my x86_64 lighting talk session). What I presented was exclusively console based. And as a programmer I love things I can automate, like typing during a presentation. So I wrote a little Ruby script to do the job for me and bound it to some key-strokes using my window manager on the eeePC. That thing is even able to fake typos, and has some random timing to not let the typing become to regular and fluent. In order to run this script below you’ll need the xdotool.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def play(text, type_speed)
    escape = false
    text.split("\n").each{|l| l.scan(/./).each {|c|
        if c == '!'
            escape = true
        elsif escape
            if c == '<'
                `xdotool keyup BackSpace`
                sleep (rand / 10)
            elsif c[0] - '0'[0] > 0
                sleep (c[0] - '0'[0]) / 10.0
            end
            escape = false
        else
            `xdotool type "#{c}"`
        end
        sleep type_speed + ((rand / 100) * (rand > 0.5 ? -1 : 1))
    }
   `xdotool keyup Return`
    }

end

play STDIN.readlines.join, 0.09

The script has some sort of escape/ex mode to be able to place some commands. The following snipped uses them (watch out for the ! chars):

1
2
3
4
5
ruby session.rb

!5
nr :base, 42,-1,0.2,5,16,EUU!<CLID
!1play

Possible use-cases:

  • stunningly fast typing with one hand
  • telekinesis like typing

All in all it’s just a cool effect :)

The a2 automaton and Clojure

Clojure

During the preparation time for my exams I wanted to do something else than just learning. So I decided to give functional programming a try. The language of choice was Clojure. It’s a LISP written specifically for the JVM. This language got some pretty neat features (taken from wikipedia):

  • Dynamic development with a read-eval-print loop
  • Functions as first-class objects with an emphasis on recursion instead of side-effect-based looping
  • Provides a rich set of immutable, persistent data structures
  • Concurrent programming through software transactional memory, an agent system, and a dynamic var system
  • Compatibility with Java: Clojure can natively call methods and create objects from any Java library, and Java programs can call Clojure functions
  • Clojure is a compiled language producing JVM bytecode

To try this thing out I decided to implement the a2 automaton described a few posts before (those neat picture and the videos, etc.). So below is the code for the automaton. If you’re able to read/understand LISP this should be (hopefully) understandable :-). Especially the

moore-aliveness

function is really neat.

All in all it took me about 8 hours to write this code. Functional programming is a completely new world. Especially that we’re mostly working with immutable data here. That’s also the cause for this code being propably pretty slow. My use of recursion here is most likely not the way that is supposed to be done in Clojure. Anyway it was a fun experience and broadened my (programmers) horizon.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
(def WORLD_WIDTH  800)
(def WORLD_HEIGHT 600)

(def setup
    (map (fn [x] (vec (range WORLD_HEIGHT)))
        (vec (range WORLD_WIDTH))))

(defn init ([field]
    (concat
        (vector (map (fn [x] (rem x 2)) (first field)))
        (map (fn [x] (map (fn [y] 0) x)) (rest field))
    )
))

(defn moore-aliveness ([field x y]
    (let [n (vector [-1 -1] [ 0 -1] [ 1 -1]
                            [-1  0]         [ 1  0]
                            [-1  1] [ 0  1] [ 1  1])]
                ((fn [a i] (do
                    (let [rem (rest  i)
                                cev (first i)
                                row (nth field (+ y (nth cev 0)) [])
                                col (nth row   (+ x (nth cev 1)) 0)
                                b   (+ a col)]
                    (if (nil? rem) b (recur b rem))))) 0 n))
))

(defn nextgen-for-row ([field row x y result]
    ;(println (str x "x" y ": " row))
    (let [nx (inc x) rr (rest row) cr (first row)]
        ;(println (str "[" x " " y "] " cr))
        (let [
            nr (if (== (moore-aliveness field x y) 2) 1 0)
            nresult (conj result nr)]
            (if (nil? rr) nresult (recur field rr nx y nresult))
        )
    )
))

(defn nextgen (
    [field] (
        (fn [f y r] (let [ny (inc y) nf (rest f) cf (first f)]
            (let [nr (concat r (vector (nextgen-for-row field cf 0 y ())))]
                (if (nil? nf) nr (recur nf ny nr)))
        )) field 0 ()
    )
))

(def ng (nextgen (init setup)))

Live coding in Ruby

After I’ve been to the 25C3 and listened to quite a lot of electronic music there, I wanted to do something with visualization again. And while looking for a cool 3D engine for C++ (like Irrlicht), I stumbled uppon fluxus. But Scheme is really not my programming language (at least not yet :) ), so I wrote something similiar in Java using JOGL and JRuby.

Tonight I got the first visualization running on live data being broadcasted by my iTunes using OSC. Some source code and more teasers will follow. Meanwhile, have a look at the screenshot:

Btw. just to have that meantioned here, JRuby is extremely fast (at least the way I’m using it). The guys who wrote that awesome library did a wonderfull job. The next steps will be most likely add some more features (e.g. make the OSC stuff easier) and start playing arround with the SuperColider.

Fork me on GitHub