paint-brush
Why My Brain Is Wired for Clojureby@offcode
2,213 reads
2,213 reads

Why My Brain Is Wired for Clojure

by Adam SchmidegJuly 6th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

The world according to Clojure is nothing but data. At any point in time, it’s one piece of data. You can transform some data of one type to another type, but you can never get a hair dryer as a result. The universe of the old puzzle game, The Incredible Machine, is limited in a similar way.
featured image - Why My Brain Is Wired for Clojure
Adam Schmideg HackerNoon profile picture


Microcosm

The world according to Clojure is nothing but data.
At any point in time, it’s one piece of data.
You can take thisdata and transform itto something else.
It will be data again.
What you had earlier is not lost.
You can almost go back in time:
you can get your previous data easily and transform it again.
You will get the same result.

In technical terms, it’s an immutable data structure,
an arbitrarily nested collection.
Lists, associative arrays, and primitive types, like ints, floats, and strings.
There are a few more if you want to know,
but this is the whole universe of Clojure.
You can transform some data of one type to another type,
for example, when you count a list, you will transform a list to a number.
But you can never get a hair dryer as a result.
Nor can you transform a hair dryer, that would be an error.

The universe of the old puzzle game, The Incredible Machine, is limited in a similar way.
You have balls, electric switches, light bulbs, and hair dryers.
There are a few more gadgets if you want to know, but that’s basically all.
You can position them freely on the board, then kickstart the laws of physics.
The ball starts to fall unless another object keeps it from falling.
The hair dryer, if turned on, will blow the ball to the side.
Whatever happens in this little, closed universe, you can only get to a state of the objects you could have set up as a starting position.

Not all programming languages and puzzles limit themselves in this sense.
Most of them proudly contain hundreds of different types of entities.
They even encourage you to grow your own.
You go to bed one night with a confidence that you know your world,
and the next morning a Werewolf class pops into your face.
Proponents of this approach will try to comfort you it’s nothing to be afraid of.
It just extends the Human class with a Wolf trait.
Werewolf.smile inherits all its characteristics from the Human.
Well, except when the Moon rises in the night.

To be fair, I fell in love with object-oriented design in a previous life.
It helps you model the world in a beautiful way.
But each class you add to the hierarchy of classes adds a bit of a mental weight your brain has to hold.
I had a bigger brain when I was young.
Age shows and I prefer less mental weight now.
A simple hashtable is enough to represent a werewolf,
with a single attribute:bites-in-the-night set to true.

Juggling data

Why is it so difficult to start when you have nothing to start with?
Why does the blank page horrify us?
Being a little wrong is ok, you can fix it, it takes but a small adjustment.
You bought a pair of shoes one size smaller than the feet (of yours, of your partner, of your kid)?
Return it and get a bigger one.
You bought a pair of shoes when in need of a healthy lunch?
An embarrassing situation that’s less easy to correct.
How much easier it is to continue a page or any other work.
Even if you hate what you have got.
Even if you want to throw it away and start with a blank page.
At least you know what you don’t want, and, hopefully, you know what you want instead.

Clojure always gives you something to start with.
Here, a piece of data, run with it.
Just don’t run too far.
Get from point A to point B.
Only if it’s a couple of steps away.
If not, find out what lies in between and get only that far.

It’s not Clojure that gives you the data, you give it to yourself.
Clojure teaches you to think in terms of data in and data out.
Data on its own is as dead as flowers in a vase, as chess pieces on the board you never touch.
Transformation makes it come to life.

I picture myself as a juggler who transforms every throw.
They throw me a ball, I catch it, and throw it back.
Be it a low ball or a high ball, a fast and strong shot or a sneaky drop, I catch it and throw it back just the right way.
A Clojure function should be so small that you can write it in one sitting.

Juggling has a vocabulary you have to learn in order to master it.
I’m not talking about the words but about the movements you practice until your body speaks overarm throws and release.
The whole vocabulary of Clojure contains a few hundred phrases.
They make up the core library.
They embody a principle that Alan Perlis wrote half a century ago.
“It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures.”

You won’t need intricate knowledge of hundreds of core functions to write decent code.
Learn the twenty most frequent ones, they will take you pretty far.
Spend some to reflect on repetitive patterns.
Develop a sense of noticing “hey, I’m typing almost the same thing for the third time.”
There should be a core function for that.
Then scan the documentation and find it.
Now, your toolset has grown to twenty-one.
If you don’t find it (I’d be surprised, to be frank), then write it the way it should be included in the core.


You have every right to brag about it.