As if my recent Javascript posts haven’t been nerdy enough, I found some old code of mine for a Javascript Roguelike game and posted it on Github.
Roguelikes, as defined by Wikipedia are “[…] a sub-genre of role-playing video games, characterized by level randomization, permanent death, and turn-based movement.”
I’ve daydreamed about writing my own ever since I first played Nethack. This code is both pleasantly nostalgic and deeply embarrassing!
In a continuation of my general messing about with functional programming and Javascript, I’ve written a Logo interpreter and Turtle Graphics emulator in JS and HTML5 Canvas.
It’s not quite finished - I’m planning on adding a REPL and allowing expressions to be used throughout and perhaps a way to save your drawings or programs.
It works in Chrome 17, Firefox 7 and Safari 5 at the moment, although I’m sure there are problems in other browsers. I’ll try to fix the bugs soon.
This is a new concept for me, but looks like a nice one. Lenses encapsulate the access and modification of values in nested data structures in a functional way. It’s like viewing the data structure through a lens that focuses on the bit that you’re interested in. I still don’t fully understand how this works, but I got enough from the simple cases presented in this talk on Lenses by Edward Kmett to be able to translate this into Javascript.
I’m working on a more real-world example, which I’ll post as soon as I figure it out.
Javascript has had map and filter methods on arrays since version 1.6, so these are just for fun.
I’ve been enjoying messing around with functional programming in Javascript, and I think these two functions show how concise FP can be. Perhaps a little too concise in this example, but if programming is like poetry, then these are haiku. They could be even shorter in Javascript 1.8, by dropping the returns and braces.
Not so happy with this drawing, but I think I was a little overambitious. I was distracted by the refractions in the glass and got the dimensions wrong. It’s upside-down, just because.
A little feature that I quite like in the Lift Web Framework for Scala is the tryo function. This function accepts a function parameter, which it then executes and wraps the return value in a Box object (similar to Scala’s Option). This allows you to map a function to a collection and not worry about handling exceptions thrown in the function.
I’ve written up a quick version in Javascript:
The problem with my version is that it handles all exceptions the same way and loses the exception information. I’ll update the maybe() function soon to include this.
I’m trying to develop some of my hobbies this year, and drawing is one that I have neglected for ages. To encourage myself to do more drawing, I’ve set myself a goal of keeping a sketch diary, drawing at least one thing a week. Here’s my second drawing of the year, which shows how much I’ve been playing Skyrim :(
Having been working with Scala for the last year and a bit, I’ve really grown to enjoy functional programming and miss some of the features when I’m using other languages. Javascript is quite FP-ish, in some ways, but I miss the match … case construction in Scala.
I was working on integrating IP geo location lookup into the site and took advantage of the excellent free resources that MaxMind provide.
As their data codes IP addresses as integers, rather than the dotted quad format, I wrote a simple function to convert dotted quad strings to integers in Scala. It’s so short and sweet that I wanted to share it: