1. Javascript Roguelike →

    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.

    Screenshot of my Javascript Roguelike game

    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!

  2. Logo Tortoise (Turtle) in Javascript and HTML5 →

    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.

    You can see the source code at https://github.com/andyhd/Logo-Tortoise

    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.

    Give it a try and let me know what you think!

  3. Lenses in Javascript

    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.

  4. Javascript map and filter functions

    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.

  5. 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.

    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.

  6. Handling Exceptions in Mapped Functions

    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.

  7. Maybe monad in Javascript

    In the same vein as my previous post on Functional Pattern Matching, I’ve knocked up a quick and dirty Maybe monad, based on Scala’s Option class.

    This encapsulates testing for null (or undefined), so that you can do something like this incredibly contrived example:

    This will only print out the title of the book if one was found (assuming db.query returns null if none was found).

  8. 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 :(

    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 :(

  9. Functional Pattern Matching (sort of) in Javascript

    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 read Bram Stein’s post on Functional Pattern Matching in Javascript a while ago - although I’m obviously somewhat late to the party - but it feels quite heavy.

    So I whipped up some code which allows a similar construction, but in a lighter way:

  10. Scala is great

    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: