SlideShare a Scribd company logo
1 of 56
Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
Clojure through the
eyes of a Java Nut
Kannan Ramamoorthy
Principal Engineer
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
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
Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
This Talk?
● Clojure Primer
● Amusing Parts
● Hard parts
● Gotchas
● Questions
4 4
Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
This Talk?
● Clojure Primer - [About,..]
● Amusing Parts
● Hard parts
● Gotchas
● Questions
5 5
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
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
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
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
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
Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
Clojure Primer - (Over)simplified Evaluation model:
11 11
Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
This Talk?
● Clojure Primer
● Amusing Parts
● Hard parts
● Gotchas
● Questions
12 12
Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
This Talk?
● Clojure Primer
● Amusing Parts
● Hard parts
● Gotchas
● Questions
13 13
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)
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
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.
Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
Less Special Constructs (cont..):
17 17
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
Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
Infinite/lazy sequence
19 19
(print (take 2 (list 0 1 2 3 4)))
; (0 1)
(print (take 2 (range)))
; (0 1)
(print (take 10 (range)))
; (0 1 2 3 4 5 6 7 8 9)
(print (take 1000000 (range)))
; Prints first 1M numbers
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
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
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
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
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
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
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
Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
core.logic (CLP) - example:
27 27
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
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
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
Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
Cartesian product:
(for [x '(1 2 3) y '(1 2 3)] [x y])
;; Return ([1 1] [1 2] [1 3] [2 1] [2 2]
[2 3] [3 1] [3 2] [3 3])
Conciseness example:
31 31
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
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
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
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
Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
This Talk?
● Clojure Primer
● Amusing Parts
● Hard parts
● Gotchas
● Questions
36 36
Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
This Talk?
● Clojure Primer
● Amusing Parts
● Hard parts
● Gotchas
● Questions
37 37
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
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
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
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
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
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
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
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
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
Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
This Talk?
● Clojure Primer
● Amusing Parts
● Hard parts
● Gotchas
● Questions
47 47
Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
This Talk?
● Clojure Primer
● Amusing Parts
● Hard parts
● Gotchas
● Questions
48 48
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
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
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
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
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
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
Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
This Talk?
● Clojure Primer
● Amusing Parts
● Hard parts
● Gotchas
● Questions
55 55
Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved.
This Talk?
● Clojure Primer
● Amusing Parts
● Hard parts
● Gotchas
● Questions
56 56

More Related Content

What's hot

Multiple Choice Questions on JAVA (object oriented programming) bank 5 -- mem...
Multiple Choice Questions on JAVA (object oriented programming) bank 5 -- mem...Multiple Choice Questions on JAVA (object oriented programming) bank 5 -- mem...
Multiple Choice Questions on JAVA (object oriented programming) bank 5 -- mem...Kuntal Bhowmick
 
The Ring programming language version 1.7 book - Part 89 of 196
The Ring programming language version 1.7 book - Part 89 of 196The Ring programming language version 1.7 book - Part 89 of 196
The Ring programming language version 1.7 book - Part 89 of 196Mahmoud Samir Fayed
 
Multiple Choice Questions on JAVA (object oriented programming) bank 1 -- int...
Multiple Choice Questions on JAVA (object oriented programming) bank 1 -- int...Multiple Choice Questions on JAVA (object oriented programming) bank 1 -- int...
Multiple Choice Questions on JAVA (object oriented programming) bank 1 -- int...Kuntal Bhowmick
 
Multiple Choice Questions on JAVA (object oriented programming) bank 3 -- cla...
Multiple Choice Questions on JAVA (object oriented programming) bank 3 -- cla...Multiple Choice Questions on JAVA (object oriented programming) bank 3 -- cla...
Multiple Choice Questions on JAVA (object oriented programming) bank 3 -- cla...Kuntal Bhowmick
 
L Fu - Dao: a novel programming language for bioinformatics
L Fu - Dao: a novel programming language for bioinformaticsL Fu - Dao: a novel programming language for bioinformatics
L Fu - Dao: a novel programming language for bioinformaticsJan Aerts
 
Learn2Sign : Sign language recognition and translation using human keypoint e...
Learn2Sign : Sign language recognition and translation using human keypoint e...Learn2Sign : Sign language recognition and translation using human keypoint e...
Learn2Sign : Sign language recognition and translation using human keypoint e...Universitat Politècnica de Catalunya
 

What's hot (6)

Multiple Choice Questions on JAVA (object oriented programming) bank 5 -- mem...
Multiple Choice Questions on JAVA (object oriented programming) bank 5 -- mem...Multiple Choice Questions on JAVA (object oriented programming) bank 5 -- mem...
Multiple Choice Questions on JAVA (object oriented programming) bank 5 -- mem...
 
The Ring programming language version 1.7 book - Part 89 of 196
The Ring programming language version 1.7 book - Part 89 of 196The Ring programming language version 1.7 book - Part 89 of 196
The Ring programming language version 1.7 book - Part 89 of 196
 
Multiple Choice Questions on JAVA (object oriented programming) bank 1 -- int...
Multiple Choice Questions on JAVA (object oriented programming) bank 1 -- int...Multiple Choice Questions on JAVA (object oriented programming) bank 1 -- int...
Multiple Choice Questions on JAVA (object oriented programming) bank 1 -- int...
 
Multiple Choice Questions on JAVA (object oriented programming) bank 3 -- cla...
Multiple Choice Questions on JAVA (object oriented programming) bank 3 -- cla...Multiple Choice Questions on JAVA (object oriented programming) bank 3 -- cla...
Multiple Choice Questions on JAVA (object oriented programming) bank 3 -- cla...
 
L Fu - Dao: a novel programming language for bioinformatics
L Fu - Dao: a novel programming language for bioinformaticsL Fu - Dao: a novel programming language for bioinformatics
L Fu - Dao: a novel programming language for bioinformatics
 
Learn2Sign : Sign language recognition and translation using human keypoint e...
Learn2Sign : Sign language recognition and translation using human keypoint e...Learn2Sign : Sign language recognition and translation using human keypoint e...
Learn2Sign : Sign language recognition and translation using human keypoint e...
 

Similar to Clojure through the eyes of a Java Nut | [Mixed Nuts] at Pramati Technologies

Stream Processing with Hazelcast Jet - Voxxed Days Thessaloniki 19.11.2018
Stream Processing with Hazelcast Jet - Voxxed Days Thessaloniki 19.11.2018Stream Processing with Hazelcast Jet - Voxxed Days Thessaloniki 19.11.2018
Stream Processing with Hazelcast Jet - Voxxed Days Thessaloniki 19.11.2018Rafał Leszko
 
From shipping rpms to helm charts - Lessons learned and best practices
From shipping rpms to helm charts - Lessons learned and best practicesFrom shipping rpms to helm charts - Lessons learned and best practices
From shipping rpms to helm charts - Lessons learned and best practicesAnkush Chadha, MBA, MS
 
Airframe: Lightweight Building Blocks for Scala - Scale By The Bay 2018
Airframe: Lightweight Building Blocks for Scala - Scale By The Bay 2018Airframe: Lightweight Building Blocks for Scala - Scale By The Bay 2018
Airframe: Lightweight Building Blocks for Scala - Scale By The Bay 2018Taro L. Saito
 
Top Tips Every Notes Developer Needs To Know
Top Tips Every Notes Developer Needs To KnowTop Tips Every Notes Developer Needs To Know
Top Tips Every Notes Developer Needs To KnowKathy Brown
 
Airframe: Lightweight Building Blocks for Scala @ TD Tech Talk 2018-10-17
Airframe: Lightweight Building Blocks for Scala @ TD Tech Talk 2018-10-17Airframe: Lightweight Building Blocks for Scala @ TD Tech Talk 2018-10-17
Airframe: Lightweight Building Blocks for Scala @ TD Tech Talk 2018-10-17Taro L. Saito
 
Developing Your Own Flux Packages by David McKay | Head of Developer Relation...
Developing Your Own Flux Packages by David McKay | Head of Developer Relation...Developing Your Own Flux Packages by David McKay | Head of Developer Relation...
Developing Your Own Flux Packages by David McKay | Head of Developer Relation...InfluxData
 
Oczyszczacz powietrza i stos sieciowy? Czas na test! Semihalf Barcamp 13/06/2018
Oczyszczacz powietrza i stos sieciowy? Czas na test! Semihalf Barcamp 13/06/2018Oczyszczacz powietrza i stos sieciowy? Czas na test! Semihalf Barcamp 13/06/2018
Oczyszczacz powietrza i stos sieciowy? Czas na test! Semihalf Barcamp 13/06/2018Semihalf
 
Lesson slides for C programming course
Lesson slides for C programming courseLesson slides for C programming course
Lesson slides for C programming courseJean-Louis Gosselin
 
MySQL 8.0: What Is New in Optimizer and Executor?
MySQL 8.0: What Is New in Optimizer and Executor?MySQL 8.0: What Is New in Optimizer and Executor?
MySQL 8.0: What Is New in Optimizer and Executor?Norvald Ryeng
 
Clojure: Programming self-optimizing webapps in Lisp
Clojure: Programming self-optimizing webapps in LispClojure: Programming self-optimizing webapps in Lisp
Clojure: Programming self-optimizing webapps in LispStefan Richter
 
Reading The Source Code of Presto
Reading The Source Code of PrestoReading The Source Code of Presto
Reading The Source Code of PrestoTaro L. Saito
 
Scaling Prometheus Metrics in Kubernetes with Telegraf | Chris Goller | Influ...
Scaling Prometheus Metrics in Kubernetes with Telegraf | Chris Goller | Influ...Scaling Prometheus Metrics in Kubernetes with Telegraf | Chris Goller | Influ...
Scaling Prometheus Metrics in Kubernetes with Telegraf | Chris Goller | Influ...InfluxData
 
What's new with JavaScript in GNOME: The 2020 edition (GUADEC 2020)
What's new with JavaScript in GNOME: The 2020 edition (GUADEC 2020)What's new with JavaScript in GNOME: The 2020 edition (GUADEC 2020)
What's new with JavaScript in GNOME: The 2020 edition (GUADEC 2020)Igalia
 
Meetup 2020 - Back to the Basics part 101 : IaC
Meetup 2020 - Back to the Basics part 101 : IaCMeetup 2020 - Back to the Basics part 101 : IaC
Meetup 2020 - Back to the Basics part 101 : IaCDamienCarpy
 
Custom Script Execution Environment on TD Workflow @ TD Tech Talk 2018-10-17
Custom Script Execution Environment on TD Workflow @ TD Tech Talk 2018-10-17Custom Script Execution Environment on TD Workflow @ TD Tech Talk 2018-10-17
Custom Script Execution Environment on TD Workflow @ TD Tech Talk 2018-10-17Muga Nishizawa
 
Break up the Monolith: Testing Microservices
Break up the Monolith: Testing MicroservicesBreak up the Monolith: Testing Microservices
Break up the Monolith: Testing MicroservicesMarcus Merrell
 
Перехват функций (хуки) под Windows в приложениях с помощью C/C++
Перехват функций (хуки) под Windows в приложениях с помощью C/C++Перехват функций (хуки) под Windows в приложениях с помощью C/C++
Перехват функций (хуки) под Windows в приложениях с помощью C/C++corehard_by
 
Big Decimal: Avoid Rounding Errors on Decimals in JavaScript
Big Decimal: Avoid Rounding Errors on Decimals in JavaScriptBig Decimal: Avoid Rounding Errors on Decimals in JavaScript
Big Decimal: Avoid Rounding Errors on Decimals in JavaScriptIgalia
 
Functional programming, TypeScript and RXJS
Functional programming, TypeScript and RXJSFunctional programming, TypeScript and RXJS
Functional programming, TypeScript and RXJSVivek Tikar
 
On component interface
On component interfaceOn component interface
On component interfaceLaurence Chen
 

Similar to Clojure through the eyes of a Java Nut | [Mixed Nuts] at Pramati Technologies (20)

Stream Processing with Hazelcast Jet - Voxxed Days Thessaloniki 19.11.2018
Stream Processing with Hazelcast Jet - Voxxed Days Thessaloniki 19.11.2018Stream Processing with Hazelcast Jet - Voxxed Days Thessaloniki 19.11.2018
Stream Processing with Hazelcast Jet - Voxxed Days Thessaloniki 19.11.2018
 
From shipping rpms to helm charts - Lessons learned and best practices
From shipping rpms to helm charts - Lessons learned and best practicesFrom shipping rpms to helm charts - Lessons learned and best practices
From shipping rpms to helm charts - Lessons learned and best practices
 
Airframe: Lightweight Building Blocks for Scala - Scale By The Bay 2018
Airframe: Lightweight Building Blocks for Scala - Scale By The Bay 2018Airframe: Lightweight Building Blocks for Scala - Scale By The Bay 2018
Airframe: Lightweight Building Blocks for Scala - Scale By The Bay 2018
 
Top Tips Every Notes Developer Needs To Know
Top Tips Every Notes Developer Needs To KnowTop Tips Every Notes Developer Needs To Know
Top Tips Every Notes Developer Needs To Know
 
Airframe: Lightweight Building Blocks for Scala @ TD Tech Talk 2018-10-17
Airframe: Lightweight Building Blocks for Scala @ TD Tech Talk 2018-10-17Airframe: Lightweight Building Blocks for Scala @ TD Tech Talk 2018-10-17
Airframe: Lightweight Building Blocks for Scala @ TD Tech Talk 2018-10-17
 
Developing Your Own Flux Packages by David McKay | Head of Developer Relation...
Developing Your Own Flux Packages by David McKay | Head of Developer Relation...Developing Your Own Flux Packages by David McKay | Head of Developer Relation...
Developing Your Own Flux Packages by David McKay | Head of Developer Relation...
 
Oczyszczacz powietrza i stos sieciowy? Czas na test! Semihalf Barcamp 13/06/2018
Oczyszczacz powietrza i stos sieciowy? Czas na test! Semihalf Barcamp 13/06/2018Oczyszczacz powietrza i stos sieciowy? Czas na test! Semihalf Barcamp 13/06/2018
Oczyszczacz powietrza i stos sieciowy? Czas na test! Semihalf Barcamp 13/06/2018
 
Lesson slides for C programming course
Lesson slides for C programming courseLesson slides for C programming course
Lesson slides for C programming course
 
MySQL 8.0: What Is New in Optimizer and Executor?
MySQL 8.0: What Is New in Optimizer and Executor?MySQL 8.0: What Is New in Optimizer and Executor?
MySQL 8.0: What Is New in Optimizer and Executor?
 
Clojure: Programming self-optimizing webapps in Lisp
Clojure: Programming self-optimizing webapps in LispClojure: Programming self-optimizing webapps in Lisp
Clojure: Programming self-optimizing webapps in Lisp
 
Reading The Source Code of Presto
Reading The Source Code of PrestoReading The Source Code of Presto
Reading The Source Code of Presto
 
Scaling Prometheus Metrics in Kubernetes with Telegraf | Chris Goller | Influ...
Scaling Prometheus Metrics in Kubernetes with Telegraf | Chris Goller | Influ...Scaling Prometheus Metrics in Kubernetes with Telegraf | Chris Goller | Influ...
Scaling Prometheus Metrics in Kubernetes with Telegraf | Chris Goller | Influ...
 
What's new with JavaScript in GNOME: The 2020 edition (GUADEC 2020)
What's new with JavaScript in GNOME: The 2020 edition (GUADEC 2020)What's new with JavaScript in GNOME: The 2020 edition (GUADEC 2020)
What's new with JavaScript in GNOME: The 2020 edition (GUADEC 2020)
 
Meetup 2020 - Back to the Basics part 101 : IaC
Meetup 2020 - Back to the Basics part 101 : IaCMeetup 2020 - Back to the Basics part 101 : IaC
Meetup 2020 - Back to the Basics part 101 : IaC
 
Custom Script Execution Environment on TD Workflow @ TD Tech Talk 2018-10-17
Custom Script Execution Environment on TD Workflow @ TD Tech Talk 2018-10-17Custom Script Execution Environment on TD Workflow @ TD Tech Talk 2018-10-17
Custom Script Execution Environment on TD Workflow @ TD Tech Talk 2018-10-17
 
Break up the Monolith: Testing Microservices
Break up the Monolith: Testing MicroservicesBreak up the Monolith: Testing Microservices
Break up the Monolith: Testing Microservices
 
Перехват функций (хуки) под Windows в приложениях с помощью C/C++
Перехват функций (хуки) под Windows в приложениях с помощью C/C++Перехват функций (хуки) под Windows в приложениях с помощью C/C++
Перехват функций (хуки) под Windows в приложениях с помощью C/C++
 
Big Decimal: Avoid Rounding Errors on Decimals in JavaScript
Big Decimal: Avoid Rounding Errors on Decimals in JavaScriptBig Decimal: Avoid Rounding Errors on Decimals in JavaScript
Big Decimal: Avoid Rounding Errors on Decimals in JavaScript
 
Functional programming, TypeScript and RXJS
Functional programming, TypeScript and RXJSFunctional programming, TypeScript and RXJS
Functional programming, TypeScript and RXJS
 
On component interface
On component interfaceOn component interface
On component interface
 

More from Pramati Technologies

Graph db - Pramati Technologies [Meetup]
Graph db - Pramati Technologies [Meetup]Graph db - Pramati Technologies [Meetup]
Graph db - Pramati Technologies [Meetup]Pramati Technologies
 
Swift UI - Declarative Programming [Pramati Technologies]
Swift UI - Declarative Programming [Pramati Technologies]Swift UI - Declarative Programming [Pramati Technologies]
Swift UI - Declarative Programming [Pramati Technologies]Pramati Technologies
 
Adaptive Cards - Pramati Technologies
Adaptive Cards - Pramati TechnologiesAdaptive Cards - Pramati Technologies
Adaptive Cards - Pramati TechnologiesPramati Technologies
 
VitaFlow | Mageswaran Dhandapani [Pramati]
VitaFlow | Mageswaran Dhandapani [Pramati]VitaFlow | Mageswaran Dhandapani [Pramati]
VitaFlow | Mageswaran Dhandapani [Pramati]Pramati Technologies
 
Typography Style Transfer using GANs | Pramati
Typography Style Transfer using GANs | Pramati Typography Style Transfer using GANs | Pramati
Typography Style Transfer using GANs | Pramati Pramati Technologies
 
Document Clustering using LDA | Haridas Narayanaswamy [Pramati]
Document Clustering using LDA | Haridas Narayanaswamy [Pramati]Document Clustering using LDA | Haridas Narayanaswamy [Pramati]
Document Clustering using LDA | Haridas Narayanaswamy [Pramati]Pramati Technologies
 
Pramati - Chennai Development Center
Pramati - Chennai Development CenterPramati - Chennai Development Center
Pramati - Chennai Development CenterPramati Technologies
 

More from Pramati Technologies (7)

Graph db - Pramati Technologies [Meetup]
Graph db - Pramati Technologies [Meetup]Graph db - Pramati Technologies [Meetup]
Graph db - Pramati Technologies [Meetup]
 
Swift UI - Declarative Programming [Pramati Technologies]
Swift UI - Declarative Programming [Pramati Technologies]Swift UI - Declarative Programming [Pramati Technologies]
Swift UI - Declarative Programming [Pramati Technologies]
 
Adaptive Cards - Pramati Technologies
Adaptive Cards - Pramati TechnologiesAdaptive Cards - Pramati Technologies
Adaptive Cards - Pramati Technologies
 
VitaFlow | Mageswaran Dhandapani [Pramati]
VitaFlow | Mageswaran Dhandapani [Pramati]VitaFlow | Mageswaran Dhandapani [Pramati]
VitaFlow | Mageswaran Dhandapani [Pramati]
 
Typography Style Transfer using GANs | Pramati
Typography Style Transfer using GANs | Pramati Typography Style Transfer using GANs | Pramati
Typography Style Transfer using GANs | Pramati
 
Document Clustering using LDA | Haridas Narayanaswamy [Pramati]
Document Clustering using LDA | Haridas Narayanaswamy [Pramati]Document Clustering using LDA | Haridas Narayanaswamy [Pramati]
Document Clustering using LDA | Haridas Narayanaswamy [Pramati]
 
Pramati - Chennai Development Center
Pramati - Chennai Development CenterPramati - Chennai Development Center
Pramati - Chennai Development Center
 

Recently uploaded

Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 

Recently uploaded (20)

Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
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
  • 19. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved. Infinite/lazy sequence 19 19 (print (take 2 (list 0 1 2 3 4))) ; (0 1) (print (take 2 (range))) ; (0 1) (print (take 10 (range))) ; (0 1 2 3 4 5 6 7 8 9) (print (take 1000000 (range))) ; Prints first 1M numbers
  • 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
  • 31. Private and confidential. Copyright (C) 2018, Pramati Technologies. All rights reserved. Cartesian product: (for [x '(1 2 3) y '(1 2 3)] [x y]) ;; Return ([1 1] [1 2] [1 3] [2 1] [2 2] [2 3] [3 1] [3 2] [3 3]) Conciseness example: 31 31
  • 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