Clojure? Hell Yeah!!!

Clojure is a kick ass Dynamic Programming Language, Clojure is also a Functional Language and "a Lisp". Clojure is not Lisp but is heavy inspired by Lisp, some folks consider Clojure a Lisp dialect. Lisp Rock a lot, unfortunately Lisp didn't take off as a primary option at the enterprise main stream development language.

Clojure target the JVM, JavaScript via ClojureScript and CLR(for .NET platform) so far the best we have in Clojure runs on the JVM and I hope it keep this way. You can generate java classes from a clojure code or you can call java from clojure so interop is good. This is great for a plenty of reasons like: You can use all your frameworks and you basically don't need to re-invent the wheel, people basically create wrappers(Clojure functions) calling java frameworks. There are Clojure libraries and framework as well considering that the language is really really really fresh => 2009 we already have a awesome community and ecosystem.

I spent 4 intensive mouths studying Clojure last year. First it start as curiosity and them it got Heavy, Like Death Metal, the issue with Death Metal is that the very first time you hear it sucks and you wan't throw up, granted that for some people it never gets good, but like all matters of the heart it's about taste and you need some time to get the taste for Functional Programming and Clojure.

I don't know you but I came from a OOP background and I started doing Functional Programming last year(2011). So it took sometime for me to digest-er all the new concepts and start thinking different and appreciate functional programming. Now once I got my taste refined the whole new world opened to me, changing from OOP to FP with Clojure is just great and it's not ONLY because of the fact that I was learning something new but also for the options/possibilities and definitely that made me grow up as a developer in a whole different dimension.

Why I end up enjoying Clojure?

Java has and still have lots of syntax obesity like creating manually getters/setters, such obesity is also re-enforced my java frameworks like JPA, because it does the work with java annotations they made you basically create one entity per annotation and this leads to a danger things that comes true so many times in java projects, you have several data structures with several algorithm(like DAOs for each Entity) this is so bad. In Clojure you have different data structures but you endup using the same functions a.k.a algorithms and this is some much better, you have much less code and you threat everything with the same functions this is so damn good.

If you consider using sequences in Clojure you got something even better, that you can hold different data structures by a same interface in this case a seq or a lazy seq them you got it all, some structures, same functions. Clojure is so minimalist, you do more with less, this is great because gives you a mindset of reduction, so you want do less, you don't want more core, you want less code. Clojure has performance issues event using smart persistent data structures performance is not a main capability of the language right now but this trend to be fixed in near feature.

Incidental Complexity?

Clojure reduce a lot the incidental complexity. You end up thinking much more time on the *real problem* instead of thinking how you gonna express your intention. That make java sucks we have lots of incidental complexity or complexity by design :-) Higher Order Functions, Currying, Partial, Lazy, Seqs, Monads, map/filter/reduce, lazy infinite sequences, zippers, atoms, agents,protocols, STM and other several great things in clojure reduce the complexity on things, clojure is simple very simple indeed.

When you stop to think how many frameworks, libraries and jars you need in work application in java for just don't suck, it's a lot. Not to mention containers, XML, etc... Several issues with the Java language was hidden by java frameworks and libraries, clojure close lots of this caps in the language so this reduce the noise and the things you need to lean and you do lots of things in the same way. It's Clojure Simple principle of the universe applied for the whole ecosystem and this is so cool and awesome.

Functions as Data, Data as Functions

Clojure is not imperative like java and ruby are. You don't give direct instructions to Clojure perform you task instead you programming in a kinda of declarative fashion where you focus on your intention. This is amazing for code communication and simplicity.  If you do DevOps for instance Clojure will be great for DSLs and all kinda of configuration. Recently at the last Clojure Conj(Clojure Conj 2) People said that you should be using Clojure for Serialization so why use JSON if you can use Clojure.

 Looks crazy but makes sense when you stop to think. You don't need write down a Clojure parser because you already have it and will run for JavaScript, .NET and Java so you do have interchangeability. Clojure is much better than JSON 'cause you do code not ONLY data, do you have much more power to describe things and even better than that clojure will be great todo code on demand.

Great For Concurrency

Yes. First, 'cause Clojure is very very very caution with State and Side Effects. So by default nothing has state. Yes I said nothing has state by default, so everything is immutable, function side effects are very well controlled by the language, all this is great for concurrency 'cause the number 1 public enemy of concurrency is state. Alright, so I can write down my code without worrying about locks, that's right.

Hold one a second. This thing must be extremely slow, nope. Clojure does that in a very clever way using Persistent Data Structures. When I need have state? You do that explicit. Clojure uses STM for that. STM is a kinda of database transaction model but without the Durability part of ACID so is ACI.  STM is great not only because is explicit but we have a concurrentmodel that's fare do deal witrh concurrency since deal with concurrency and time is hard and tough and we don't have good abstractions to deal with this.

Higher Order Functions

As a good Functional Programming Language clojure does Higher Order Functions. HOF: It's the ability to pass functions around as parameters and return functions from functions public or even using anonymous functions. This is one of the basic concepts of any functional programming language and leads to beautiful code doing much things with less much less.

Reduce is a very interesting function you pass a function in your case we are passing the function '+'. Yes, '+' it's a function in clojure like the other math operator '/', '*', '-'. The second argument is a collection in this case it's a list, you create a list with '()' since this is also we invoke functions we need pass the tick operator ' to tell clojure to not invoke a function and create a list.  Reduce will iterate each element on the collection and will consume the collection element one by one and pass to the function, in this case '+' and reduce will keep the result and do that incrementally through all elements on the collection. So this code is doing a sum with all elements on the list.

This other sample I'm using the map function, this is not creating a map(the famous data structure) it's going each element of the collection in this case is a vector because we are using the '[]' and calling the function, in this case inc, inc will increment a number by one, the main difference between map and reduce is that map will create a collection will the result of the call of all collections.

Now, I'm using a filter function, this is a filter :-) So we pass a function/predicate that will filter by the result of the function for each element of the collection, in this case the collection is generated by the map function. This is a awesome example of higher order functions and how you do a lot with so little. What's is the #() this is a syntactical sugar for clojure anonymous functions and what's % this is kinda of Perl stuff this will be the first argument, so me filter will pass the current element through that thing you can do crazy Perl stuff like %1 %2 %3, when the number is the following argument.

Dude, Where is my Pojo?

Since clojure is not OO we there is not Objects, but we have structures. Under the hub structures are maps, let's see how we could create a kinda of pojo in clojure, will be something like this.

defstruct define a "type" so you can specify with "attributes" your type gonna have, actually nothing of this is true, you're more telling with keys a map will have :-) Them you create a instance of a struct using the struct function passing whats the struct and the values. You may already realize that Clojure you don't use the ';' in each line and you don't use use the comma(',') because spaces are commas for clojure. Finally I'm using a kinda of symbol, in ruby will be at least. In clojure this is a KEYWORD. a Keyword is a function as well :-) That's why we can do something like this: (:age p) it means give the age from the p struct.

Clojure has a great future coming...

I deeply believe in Clojure. Functional Programming will be the future and clojure features and strong background + community / ecosystem will last. Relatively speaking Clojure is growing faster than Scala, Twitter is using Clojure in Production. Heroku has Clojure support in production Cloud.

I love Scala as well. But Clojure it's also a kick ass language. We gonna see more and more cases of BIG companies doing Clojure in productions. I will blog much more about Scala and Clojure that are the current languages that I have more interest also where I think the fun is happening.

Functional Programming with Clojure #FTW

Diego Pacheco

Popular posts from this blog

Kafka Streams with Java 15

Rust and Java Interoperability

HMAC in Java