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

It’s been a lit­tle while since I last spent time with Elm, and since Elm 0.13 was recent­ly announced so what bet­ter time to get my Elm hat back on and see what’s new.

There’s a new-look online debug­ger which looks pret­ti­er than before:

image

but more impor­tant than that, is the new Elm Reac­tor com­mand line tool which pow­ers both the online debug­ger. With the Elm Reac­tor you can run your own time-trav­el­ling debug­ger local­ly and as you edit your Elm source files you can watch your appli­ca­tion update in real-time while retain­ing the abil­i­ty to go back in time by play back pre­vi­ous events.

There’s also a new com­mand line pack­age man­ag­er – Elm Get – which gives you the abil­i­ty to eas­i­ly add com­mu­ni­ty Elm libraries to your project, or to pub­lish your own libraries to the Elm Pub­lic Library. Over­all it works very sim­i­lar to the how Dart-pub works and whilst I haven’t pub­lished any libraries myself it seems a straight for­ward affair.

There are a cou­ple of small break­ing changes in the core lan­guage, and it’s great to see that F#’s func­tion­al com­po­si­tion oper­a­tors (« and ») have been adopt­ed and released in this ver­sion!

 

Now that I’ve caught up on the changes, I put togeth­er a sim­ple imple­men­ta­tion of Snake, and to my pleas­ant sur­prise the whole thing came in at less than 100 LOC although admit­ted­ly not the eas­i­est 100 LOC I’ve ever writ­ten. I had to real­ly think about what I’m doing (which is a good thing), the lack of IDE sup­port occa­sion­al­ly gets in the way, and I find the error mes­sage hard to read some­times (although it’s much bet­ter for­mat­ted when you work against Elm Reac­tor run­ning local­ly.

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

image

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

 

Links

Elm Reac­tor – Time Trav­el made Easy

Elm 0.13 – Archi­tec­ture Improve­ments

Elm start­up project

Elm-Snake project page