ClojureScript Experiments

ClojureScript was one of the options that piqued my interest when I saw it on the list for several reasons:

  • It’s a modern but fully featured language
  • It’s functional (as opposed to imperative)
  • My sister programs in Clojure so I’ve heard a bit about it
  • I don’t know it at all, so it would be an interesting experiment

So I decided to try it out, see where I would go. This is about adventures after all!

Installation and setup

This I hoped would be an easy job – download a compiler, it produces something I can test in a browser. It wasn’t quite as easy as that, but I suppose on reflection it wasn’t too bad.

After the initial install, I followed the tutorial on the quick start page. This didn’t go too well – I had to kill java several times to get the pages to start, and it feels like there’s a lot of things you’re expected to know – eg what a REPL is for example (it’s a shell, in short) – and a lot of things that are only half documented – eg the Dependencies section does not contain instructions for windows.

That missing dependencies section caused me problems, since it was pretty soon that I realised that all this gives me is JavaScript. I still will need all of the accompaniments such as stylesheets etc, and you’re not abstracted from the output at all – I need to know the structure of the output in HTML terms. That being said, there are probably libraries I can use to make that better…but that means I need better build tools.

Build Tools

ClojureScript does not seem to be simple to build anything in for the uninitiated. The example projects came in two flavours, Leiningen and Boot. These toolsets do quite a lot – compile, set up a web server, create template projects, watch for changes etc. This is a lot more than I’m used to!

I tried a Boot project first, tenzing, but I hit one of those tiny annoyances that makes or breaks software: when I changed my program and saved, it would play an audible feedback – usually someone saying “Warning”. This would be a nightmare if I was in the office! I couldn’t work out how to turn it off – it looks to me that I’d have to write some scripts to disable the audible feedback, which seems amazingly user-unfriendly. I’m not obliged to fight it, so I didn’t and had a look at Leiningen instead.

Leiningen wasn’t all that easy either – it couldn’t do it’s self-install properly due to a certificate problem, but it did provide instructions to ignore the certificate that worked. Not exactly a great introduction though.

Once I’d done that I used the figwheel template and that got me to a stage where I could edit some ClojureScript files, and the changes appeared on a page. Seems pretty reasonable.

Since I did all this I found a set of ClojureScript tutorials that look much better. I’ll have to have a look later!

Some Programming

This had all taken a lot longer than I expected! But I couldn’t evaluate if this was going to go anywhere without actually trying to see if we could make our idle game.

So I looked into the most basic thing: can I make a game loop? Turns out this wasn’t too difficult. Most of my inspiration came from a binary clock example I found, but this fell into three distinct logical parts:

  • A state definition
  • A render function, that when evaluated renders the page
  • A timer which updates state and evaluates render

So here we have some functions:

(defonce appState (atom {:text "Hello world!"
	:counter 0}))

(defn pageLayout []
  	[:div
   		[:h1 (:text @appState)]
   		[:p (:counter @appState)]])

(defn render []
	(reagent/render-component [pageLayout]
    	(. js/document (getElementById "app"))))

(defn timer []
	(swap! appState update-in [:counter] inc)
	(render))

(js/setInterval timer 1000)
(render)

This will count up forever. Hardly a witch in sight, but this proves the concept of a game loop. I’m not sure I understand completely every line of the code, but then I’ve never looked at Clojure before!

Conclusion

I’ve managed to have a look at using ClojureScript to write a basic game loop for an initial proof of concept. I think from this I can say that this is possible for me to write my idle game in ClojureScript.

But possible doesn’t mean sensible. There’s some things that worry me:

  • This appears to be essentially writing an imperative program with a functional syntax. Is that really right? Have I missed something? If I’m doing that, why don’t I use an imperative language in the first place?
  • As I don’t know the language, or even feel particularly strong with functional languages, am I going to hit a lot of barriers, and make a lot of mistakes? Will this be more or less interesting to read about than if I do something I know better?
  • Am I painting myself into a corner where it would be difficult for me to change platform later, but there are missing essential features?

I’m going to have a think about it. I have not gone too far down this path yet so nothing is set in stone!


Leave a Reply

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