Writing 2048 made simple with Clojure

Image for post
Image for post

I’m writing this article after writing lot’s of small programs in Clojure. The way Clojure has changed my thoughts about how should I approach a problem and how to solve a particular problem, without thinking about whose responsibility is what.

There is a function for that.

Inbuilt functions of Clojure help in thinking about the problem in functions and more in their domain rather than thinking about how to program them. Once you have enough vocabulary of Clojure’s inbuilt functions it becomes very easy to write a solution in Clojure.

Below I will talk about how I have written 2048 game by only using Clojure’s inbuilt library functions using compose and without writing much of own logic.

While writing in many other languages you’ll probably think the way of looping over-board and than making up the game decisions, but here it’s not the same.

In this most of the time I spent is in thinking what function to use rather than how should I loop over the board.

You can see I have very few function arguments and only a few function declaration of my own! all others are just function compositions.

By writing code in this way you get the ability to express things in a lot easy way and the testing will become very easy. Just think what is the portion or functions of the code that is written by me the answer is very few.

It genuinely makes testing easy. In this what will you test? Just the connections!! Other things are written using Clojure core library functions that means you can trust on those functions that they will work.

Other part that I missed is, all the code I have tested on REPL so it’s kind of tested code but yes we can write a few more :)

Application Developer at ThoughtWorks

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store