This talk is meant to throw light on Clojure world, for Java developers.
- Clojure Introduction
- Amusing Parts that you might have not seen in Java
- Hard parts for a Java developer
- Gotchas
A Journey Into the Emotions of Software Developers
Clojure through the eyes of a Java Nut | [Mixed Nuts] at Pramati Technologies
1. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
Clojure through the
eyes of a Java Nut
Kannan Ramamoorthy
Principal Engineer
2. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
● (Used to be) A strong believer that “OOPS is a natural way to design real-
world problems.”
● (Used to) Read & chat a lot about Java and Spring with fellow Java
developers.
● Still a Java developer.
● Not a ‘Know-it-all’ Clojure guy. Hence, one among the Java user tribe.
About Me:
2 2
3. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
This Talk?
● Not a Clojure vs Java comparison.
● Not a Clojure Tutorial.
● Probably won’t be able to use/try Clojure right away.
● My personal experience on how it ‘feels’ to use Clojure.
● Throw light on Clojure world.
3 3
4. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
This Talk?
● Clojure Primer
● Amusing Parts
● Hard parts
● Gotchas
● Questions
4 4
5. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
This Talk?
● Clojure Primer - [About,..]
● Amusing Parts
● Hard parts
● Gotchas
● Questions
5 5
6. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
● A dynamically typed
● Functional programming language
● Lisp family
● Runs on JVM
● 100% Java
Clojure Primer - About:
6 6
7. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
This Talk?
● Clojure Primer - [About, Syntax...]
● Amusing Parts
● Hard parts
● Gotchas
● Questions
7 7
8. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
(operator <operand1> <operand2> ..)
Clojure Primer - (Over)simplified Syntax:
8 8
Special construct/Function/Macro
9. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
Special construct:
Built in language constructs.
Function:
Functions that can be defined by us. Equivalent to Java’s methods.
Macro:
#Later
Clojure Primer - Definitions:
9 9
10. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
This Talk?
● Clojure Primer - [About, Syntax, Evaluation Model]
● Amusing Parts
● Hard parts
● Gotchas
● Questions
10 10
11. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
Clojure Primer - (Over)simplified Evaluation model:
11 11
12. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
This Talk?
● Clojure Primer
● Amusing Parts
● Hard parts
● Gotchas
● Questions
12 12
13. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
This Talk?
● Clojure Primer
● Amusing Parts
● Hard parts
● Gotchas
● Questions
13 13
14. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
● As mentioned in primer, (operator <argument1> <argument2> ..)
Examples,
Simple Syntax:
14 14
Expression Meaning
(+ 1 2) 'Add' 1 and 2.
(= v1 v2) 'Equality check' for values of 'v1'
and 'v2'.
(ns new-name-space) 'Switches namespace' to “new-
name-space”. (Namespace is
equivalent to Java’s package)
15. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
Expression Meaning
(println “Test String”) 'Prints' the given string.
(def v 1) 'Define' a variable ‘v’ with value 1
(in the current namespace)
(defn prn [x] (prinln x)) 'Define' a function named 'prn' that
accepts a parameter and prints it.
Simple Syntax (cont..):
15 15
16. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
Less Special Constructs:
16 16
● Special constructs are the basic building blocks of a language.
● Provided the special constructs, you will be able to build the whole
language yourself.
● If you wish, you could define the whole language by yourself.
17. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
Less Special Constructs (cont..):
17 17
18. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
We can definition of short-circuit or (||) as below,.
(defmacro ||
"Equivalent of or"
([] nil)
([expr]
`~expr)
([expr & other-exprs]
`(let [val# (|| ~expr)]
(if val# val# (|| ~@other-exprs)))))
Less Special Constructs (cont..):
18 18
20. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
● Lazy sequences are common in Clojure.
● (Most of the cases) normal sequence can be replaced with lazy
sequence, helps in deferring the computation until its needed.
Infinite/lazy sequence (cont...)
20 20
21. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
Introducing 2 new construct before going into detail.
List
(list 1 2 “three”)
; This creates a list with elements 1, 2 and “three”
In short-form the above notation can be written as,
‘(1 2 “three”)
Program as Data:
21 21
22. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
Quote
(quote (+ 1 2))
; Returns the expression as is, i.e., (+ 1 2)
In short form this can be written as,
‘(+ 1 2)
Program as Data (cont...):
22 22
23. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
Does that ring a bell?
● After seeing the below Clojure snippets?
(print “string”)
(take 2 (list 1 2 3))
● Isn’t the above code looks like a list without quote?
● All the Clojure programs are just nested list (Homoiconicity).
Program as Data (cont...):
23 23
24. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
● Lets you create DSL or custom syntactic abstraction without having to
be wait for language support.
● Things like ‘Enhanced switch’ in Java 13 doesn’t excite you much. You
can write it yourself if you want.
● For ex, you can define a macro that can be used like this which ends
up adding the numbers that you specify.
(evaluate add "1,2,3 and 4")
Macros (revisited):
24 24
25. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
● Lets you do CSP (Communicating Sequential Process) style
programming.
● Defines threads as go -blocks and channels for inter-
communication.
● Comparable to ‘Thread’ communicating with blocking queues in Java.
● Special highlight feature (alts!!), which lets you wait on multiple
channels.
(alts!! [channel1 channel2 channel3])
core.async (CSP) :
25 25
26. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
● CLP - let’s you define the constraint of your problem and one or
generic algorithms solve the problem for you.
● At a high-level, you say “what is your problem?” the framework will
figure out “how to solve it?”
● Classical applications, “n-queens”, “sudoku”, “Timetable creation”, etc.
core.logic (CLP):
26 26
27. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
core.logic (CLP) - example:
27 27
28. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
● Lets you write a sophisticated switch-case style matching.
Example,
(match [x y z]
[_ 1 2] 1
[1 2 _ ] 2
[_ _ 1] 3
[_ _ 2] 4
:else 5)
core.match:
28 28
29. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
● Has a Isomorphic counterpart ClojureScript (CLJS)
● Write Clojure that can be compiled to Javascript.
● Lets you code make use of all the goodness of Clojure for browser.
Isomorphic:
29 29
30. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
● Less verbose, short and sweet code.
● Powered by syntactic abstractions and abstract, reusable functions
definitions.
● Some examples….
Conciseness:
30 30
32. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
Reading a file:
(slurp "/Users/me/sample.txt")
(slurp "https://google.com")
(slurp "ftp://localhost:2121/sample.txt")
Conciseness example:
32 32
33. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
Fibonacci solution that you can tweet:
(def fib
(lazy-cat [0 1] (map + (rest fib) fib)))
(take 10 fib)
; Prints (0 1 1 2 3 5 8 13 21 34)
Conciseness example:
33 33
34. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
● Wrap your function so that the result of the function is cached for a
given parameter and returned on consecutive call.
● Reusable a caching implementation that can wrap any applicable
functions.
● ‘Referential Transparency’ of most of the functions makes it easy to
memoize it.
Memoization:
34 34
35. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
● With all the goodness of Clojure, you can also have access to the
ocean of Java library.
(println (System/currentTimeMillis))
; prints Current time in milliseconds.
(doto (new java.util.HashMap)
(.put "one" 1)
(.put "two" 2))
; Calls put(“one”, 1) and put(“two”,2)
; a new hashmap.
Java Interop:
35 35
36. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
This Talk?
● Clojure Primer
● Amusing Parts
● Hard parts
● Gotchas
● Questions
36 36
37. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
This Talk?
● Clojure Primer
● Amusing Parts
● Hard parts
● Gotchas
● Questions
37 37
38. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
● Parentheses is not arbitrary as in Java, ex, (+ 1 2 3) cannot be
written as (+ 1 (2 3))
● Naturally, takes some time to adopt for Java developers.
Usage of parentheses:
38 38
39. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
● Understand that each parentheses is a unquoted list and the first
element is expected to be an operand.
● Use an IDE to have the code properly formatted.
Overcoming - Usage of parentheses:
39 39
40. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
Shift to s-expression:
● Mental shift to expressions of style (operator operand1
operand2)especially for the operators where order matters.
Example,
(> 2 1) ; Checks if 2 is greater than 1
(< 1 2) ; Checks if 1 is less than 2
40 40
41. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
● Unlearn the conventional meaning and read the docs for the
operators. Meanings for operators in Clojure may slightly differ.
Ex,
> means if the following numbers are in descending order
< means if the following numbers are in ascending order
● Practice
Overcoming s-expression shift:
41 41
42. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
● All the Clojure’s core data structures are immutable.
● One of the MOST DIFFICULT part for a Java developer.
Immutable Data Structures:
42 42
43. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
● Don’t go with the easy ways of working around with immutable types.
● We naturally think using loops. Rather practice using map, reduce and
recursion.
Overcoming difficulty with Immutable Data Structures:
43 43
44. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
● It takes time to get the significance of Macro.
● Having not used to meta-programming, choice of when to use macro
will be challenging.
● Understanding the scope of variables used in execution for a Macro
will be difficult.
Programming with Macro:
44 44
45. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
● Try implementing the out-of-box macros as functions and see if it is
possible.
● Using Macro for syntactic abstraction is a good choice. For everything
else
● WRT the scope, getting mental model of 2-level of execution helps.
● Practice.
Overcoming Macro usage difficulty:
45 45
46. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
● Lazy sequence itself is hard to imagine initially.
● Implementing one by yourself also bit hard initially.
Overcoming:
Understand, the power of lazy sequence is the implementation of the
consumer. Try understand how it works and try solving it.
Lazy sequence:
46 46
47. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
This Talk?
● Clojure Primer
● Amusing Parts
● Hard parts
● Gotchas
● Questions
47 47
48. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
This Talk?
● Clojure Primer
● Amusing Parts
● Hard parts
● Gotchas
● Questions
48 48
49. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
Question: You say core data structures are not mutable. So how a
very basic use case of addition or deletion of the element from a
data structure is addressed?
Gotchas:
49 49
50. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
Answer:
Clojure does support those operations. But it doesn’t change the
original data structure.
For ex,
(def a [1 2]) ; Define a vector with 1,2
(println (conj a 3)) ; Append 3 to it
;; Prints [1 2 3]
(println a)
;; Prints [1 2]
Gotchas:
50 50
51. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
Question: What?! Do you create a new collection for each change
that we do? Aren’t you filling up the memory unnecessarily?
Gotchas:
51 51
52. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
Answer:
Nope. Clojure uses a mechanism called Persistent Data structure,
which doesn’t actually create a new object but reuses the existing one
and still having the variables immutable.
Gotchas:
52 52
53. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
Question: It is nice that the programs are concise. But anyhow,
under the hood it boils down to so many lines of Java code? What
is so nice about conciseness?
Gotchas:
53 53
54. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
Answer:
● Programming with less code could probably means more
productivity.
● But surely means less testing, if you are building on top of existing
and pre-tested code. In that way, conciseness matters.
Gotchas:
54 54
55. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
This Talk?
● Clojure Primer
● Amusing Parts
● Hard parts
● Gotchas
● Questions
55 55
56. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
This Talk?
● Clojure Primer
● Amusing Parts
● Hard parts
● Gotchas
● Questions
56 56