Archive for August, 2009

Update!

Good news first.

It’s not broken…whew! I was really worried. I was worried I’d have to get surgery (something I have an irrational fear of). I was worried I’d have to get a cast, I was worried. Period.

Alas, no. It’s a wicked bad sprain, and an even more wicked hematoma.

I am not sure as of yet what this means for my marathon plans. I suppose only time will tell. I do still plan to line up in November, so we’ll see.

Tonights nasty pic of the day

Well Crap.

There I was 1.1 miles into a 4 miler tonight when my foot came down crooked and rolled under me.

I heard/felt a pop as I went down like a sniper took me out.

My first reaction was to roll out of it and grab my ankle to assess the damage. All I could think about was the marathon and how long I might be out if this were a break. Luckily it felt fine, I grabbed all around and poked and prodded.

Then it started to swell, and by started I mean it went from looking puffy to looking like my body was falling apart inside and pooling up on the side of my ankle.

A woman and her son came running up to me. Luckily it looked like my ankle was destroyed and there was blood from my knees oozing everywhere, or that would have been embarrassing. :)

Since I was a mile from my car on the trail, the woman prepared to carry me out. And I think she intended to do it. But, I told her that I had a good 80 pounds on her and that it was probably best if we flagged down a golf cart. She agreed, but I really think she would have tried. Then another man and woman came on the scene, and the man went to get help.

Then a man in a golf cart brought the man back and I lumbered into the cart. The man took me to my car, where I hobbled around and drove home.

Laying on the floor in our living room, I called Shelly and asked her to come home from her mom’s to help me…heh, that sucked, but it was super hard to get around.

So, after consulting my doctor on IRC…since it wasn’t bruising we have decided to wait upon the morn to see what happens.

Bleh.

Not for the squeamish (I say that because I hate looking at crap like this) :
TPIWWP

As long as it’s not broken, and I don’t think it is, I am pretty sure I can still make Harrisburg.

Reading/Printing a file

A friend of mine at work, also bitten by the functional bug, has been hacking on some Scala.

He showed me some code yesterday and I thought it might be fun to convert it. I am not going to post his code, as that would be presumptuous, but I have mine to show.

The goal is to read up a file, and then print that file prepending each line with the number of characters in that line, as well as padding the beginning so that everything lines up.

The usual caveats about my newbness.

; Get the name of the document from the cli
(def arg (first *command-line-args*))

; Convert the doc to a vector
(def lines (vec (.split (slurp arg) "\n")))

; Takes a string, and finds out how long
; the length, of the length is
(defn length-of-length[s]
(.length (str (.length s))))

; Returns the longest element from a collection
(defn get-longest-line[lines]
(reduce (fn [x y]
(if (>(.length x)(.length y))
x y))lines))

; Repeat s string t times.
(defn pad[s t]
(take t (repeat s)))

; Find the maximum width of a line from the doc
(def max-width (length-of-length (get-longest-line lines)))

; Take the vector of the file, iterate over the lines and
; print each one prepended with the number of characters that
; appear in that line
(doseq [line lines]
(let [num-spaces (- max-width (length-of-length line))]
(println (apply str " " (pad " " num-spaces)) (.length line) " | " line)))

Tip of the hat to ‘Chousuke’ in #clojure for the clue on ‘apply’.

Example


$ java -cp /opt/clojure/clojure.jar clojure.main scala.clj test.txt
50 | This is a text file with lines that vary in length
0 |
13 | ^^ Blank line
34 | Some other text that is mid length
9 | Oh hai!!!

Thanks for playing!

Dwarf Name Generator

The local ruby group sends out the Ruby Quiz (with some regularity). This week it was to create a Dwarf Name Generator.

I thought of a fairly simple solution in Clojure pretty quickly, but then it took me a while to implement it. Anyway, here is what I came up with.


; Create a couple of Maps with keys being Integers
(def firstnames {1 "Danby" 2 "Tamreil" 3 "Vash" 4 "Gobu"})
(def lastnames {1 "Dongledorp" 2 "Floopydoo" 3 "Goran" 4 "Zypher"})

; Return a random Integer with a value > 0 but < = the upper bounds
(defn randomize [upper]
(let [randy (. Math round(* (. Math random) upper))]
(if (= randy 0)
(int (+ 1 randy))
(int randy))))

; Since Maps, can be used as Functions that take a key as an argument
; pass those to println
(println (firstnames (randomize (.size firstnames))) (lastnames (randomize (.size lastnames))))

The reason it took me so long is that initially I wasn't casting 'randy' to a Java Integer, and using it as a argument to a Map (which causes the Map to act as a function) was causing the Map to not find it while looking it up. After a bit of head scratching, I asked in #clojure.

Obviously, if I were a linguist, I would have created a lot of maps with just parts of names in them (I am sure that is called something), and then randomized that further, but I am not, and since the underlying mechanics would be more or less the same, I left it as is.

Happy hacking!