Elm – building a version of Snake in under 100 lines of code

Yan Cui

I help clients go faster for less using serverless technologies.

It’s been a little while since I last spent time with Elm, and since Elm 0.13 was recently announced so what better time to get my Elm hat back on and see what’s new.

There’s a new-look online debugger which looks prettier than before:

image

but more important than that, is the new Elm Reactor command line tool which powers both the online debugger. With the Elm Reactor you can run your own time-travelling debugger locally and as you edit your Elm source files you can watch your application update in real-time while retaining the ability to go back in time by play back previous events.

There’s also a new command line package manager – Elm Get – which gives you the ability to easily add community Elm libraries to your project, or to publish your own libraries to the Elm Public Library. Overall it works very similar to the how Dart-pub works and whilst I haven’t published any libraries myself it seems a straight forward affair.

There are a couple of small breaking changes in the core language, and it’s great to see that F#’s functional composition operators (<< and >>) have been adopted and released in this version!

 

Now that I’ve caught up on the changes, I put together a simple implementation of Snake, and to my pleasant surprise the whole thing came in at less than 100 LOC although admittedly not the easiest 100 LOC I’ve ever written. I had to really think about what I’m doing (which is a good thing), the lack of IDE support occasionally gets in the way, and I find the error message hard to read sometimes (although it’s much better formatted when you work against Elm Reactor running locally.

If you’ve got a few minutes to kill, why not give it a go:

image

and feel free to check out the source code on Github.

 

Links

Elm Reactor – Time Travel made Easy

Elm 0.13 – Architecture Improvements

Elm startup project

Elm-Snake project page

Whenever you’re ready, here are 3 ways I can help you:

  1. Production-Ready Serverless: Join 20+ AWS Heroes & Community Builders and 1000+ other students in levelling up your serverless game. This is your one-stop shop for quickly levelling up your serverless skills.
  2. I help clients launch product ideas, improve their development processes and upskill their teams. If you’d like to work together, then let’s get in touch.
  3. Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.

Leave a Comment

Your email address will not be published. Required fields are marked *