Learning Clojure

Learning a new language can be difficult even with extensive documentation available. When it comes to clojure it could get easily confusing as not many documentations/explanations are available on the internet. During my journey so far with respect to clojure learning, I have collected few thoughts/links/materials so thought of sharing with you all. These are not hard core rules but following these steps and materials might help speed up your clojure journey.

Day 1 — Day 2:

First things first, get accustomed to the clojure syntax. If you are from OOPs background it might be trickier initially but as you start using you will realise that it’s simple.

It is a declarative language and uses expression form of syntax inspired from mathematics. One good thing with clojure syntax is that it is ubiquitous across various actions unlike non-funtional languages where different actions have different syntax.

Refer this article for detailed explanation about the syntax

http://clojure-doc.org/articles/tutorials/introduction.html

https://yogthos.github.io/ClojureDistilled.html

Take away from clojure syntax — Keep it simple and easy

Day 3–4:

Now that you have a fair understanding of clojure syntax it’s time to dive deeper into core concepts of Clojure

Concept 1 — Clojure is a functional language

Before learning clojure it is very important to understand what is functional programming and its characteristics and how it is differs from Object Oriented Programming

In OOPs world every entity is represented as object and their behavior as actions on objects. Any change to object is done via actions ie mutates object state. There are chances for external systems to mutate object state accidentally. To avoid this, few of the variables on object are made private. The fundamental idea for this kind of representation is to keep related data together and private data secure. Setters and getters are a centralised way for accessing the object state

On the contrary functional programming doesn’t have objects because it doesn’t allow mutation of variables so there is no need to worry about data protection and accidental changes.

Here are some of important characteristics of FP

  1. Pure functions — For a given input, a function always returns the same output without changing any other variable. Functions are applied on input data to transform data from one state to another
  2. Immutability — Any change to the object doesn’t change existing value instead returns a new object with changes
  3. Referential transparency — The value of variables passed to a function are never changed
  4. Uses expressions — In OOPs/Imperative programming code is represented as statements with local assignments(mutates references). Because FP can not mutate data they can not assign value to variables hence it uses expressions to evaluate into a result.
  5. Tail recursion — You must have heard about recursion but not sure about tail recursion. In recursion the evaluation for results starts with the return of last recursion call and all the intermediate functions are stacked for later execution. As we know stack has a limited size. Using recursion with immutability means creating more variables and functional calls which results in stackoverflow errors. To is exactly why tail recursion is used instead of recursion. The only difference between the two is the evaluation for results starts with first function call and results are passed with next function call. Limited object creation and function calls. Refer here http://sercanulucan.com/blog/2017/01/four-ways-of-fibn-with-clojure/
  6. Functions are first class entities — Any problem that you can think of starts with a data which can be passed over a set of functions to achieve data end state.
  7. Higher order functions — It’s possible to return functions and pass function as parameters to other functions.
  8. Concurrency and parallelism — Because of Immutability and Referential transparency variables can be used across multiple threads without any side effects.

Refer here to find detailed explanation of these concepts. The better you understand these concepts the easier it would be to learn clojure.

Take away — Clojure is easy because of its strong basics

Day 5:

Concept 2: Clojure is dynamic and compiled language; runs on jvm

Being dynamic and being able to compile is a powerful combination. The former gives flexibility in writing code and the later makes it efficient for execution.

Jvm takes bytecode and translates it to machine understandable code. It doesn’t care who produces the bytecode or what language is used behind. In Java, compiler converts high level code into bytecode. In Clojure, a reader is used to convert code into data structure which is then fed to clojure compiler to convert to bytecode.

Running on jvm gives an added advantage of using java libraries in clojure code without having to rewrite a lot of existing stuff

Refer here to find the detailed explanation

Take away — Do not reinvent the wheel unless its’ absolutely necessary

Day 6–7:

Concept 3: Clojure uses immutable data structures

Have you ever wondered if using immutable ds is efficient enough? Doesn’t immutability mean creating more objects and frequent garbage collection? How is the immutability implemented?

These are the right questions to ask when trying to learn a new language

Here is the link for detailed explanation on immutability behind the scenes

Day 8–9:

Concept 6: Code is data;Data is code

Yes, you read it right. Are you wondering how code can be data?

Being a declarative language it follows an expressive syntax. This syntax is easy to follow and has an advantage during evaluation. Clojure code can be distinguished into symbols or literals. A literal is something that holds data and symbol is name of a data or an operation. During evaluation clojure reader identifies these symbols and literals and converts them to data structures. Now your code is data

Refer here to find the detailed explanation

Day 10–11:

Concept 7: Destructuring in clojure

Declaring variables do take some lines when writing code. It sometimes can make your code complex with too many declarations. Clojure has a concept of destructuring for declaration of variables in function arguments or in single line. This makes code easy to read and less complex

Refer here to find the detailed explanation

Day 11–12:

Concept: Macros in clojure

The beauty of clojure is, it encourages to think on “what” part of coding than “how”. “How” is already figured out with most of the existing macros ie existing macro functions ie a way to make function reusable.

Refer macros to find the detailed explanation

Day 13–14:

Concept 4: Clojure is built for concurrency and parallelism

Non-functional languages take an extra effort to achieve concurrency and parallelism. Whereas as in clojure, the three goblins problems of thread management are handled at the core of language with immutability and STM(software transactional memory).

Achieve concurrency with,

  1. Future — Creates a new thread for the task and makes current thread non-blocking
  2. Delay — On demand execution of the task
  3. Promises — When a task is done the result can be delivered with promises

Achieve parallelism with,

  1. Pmap — Parallel processing of a map

Refer here for the detailed explanation

Once you have finished reading all these topics please go through Clojure for the brave and true for in depth understanding of every topic in clojure. This is a must read book for clojure enthusiasts. Follow my future posts for a detailed discussion about every topic mentioned here.

Thanks for reading….

References

  1. https://hypirion.com/musings/understanding-persistent-vector-pt-2
  2. https://en.wikipedia.org/wiki/Functional_programming
  3. https://clojure.org/guides/learn/syntax
  4. https://yogthos.github.io/ClojureDistilled.html
  5. https://hypirion.com/musings/understanding-persistent-vector-pt-1
  6. http://sercanulucan.com/blog/2017/01/four-ways-of-fibn-with-clojure/
  7. https://spin.atomicobject.com/2013/07/23/homoiconicity-clojure-macros/
  8. https://clojure.org/guides/destructuring
  9. https://www.braveclojure.com/read-and-eval/
  10. https://www.braveclojure.com/concurrency/
  11. https://www.braveclojure.com/