SlideShare a Scribd company logo
A very little bit of Clojure
Ben Stopford*
!
!
!
!

*(not a Clojure expert)
Clojure
•

Clojure is a Lisp - the oldest programming language other
than Fortran

•

Unusual syntax, stemming from polish notation

•

Functional, but not pure functional (like say Haskell)

•

JVM based and Dynamically Typed
Lisps
•

LISP stands for LISt Processing, it’s a language of lists.

•

Form:

	 	 (function arg1 arg2 arg3)
	 	 (* 1 2 3) => 6
Lets look at a simple function
(defn four-of-kind? [hand]
(= 4
(first
(sort >
(map second
(frequencies
(map second hand)))))))
!

(four-of-kind?
[[:clubs 13] [:diamonds 8] [:hearts 8] [:clubs 8] [:spades 8]])
Everything is a list
(defn four-of-kind? [hand]
(= 4
(first
(sort >
(map second
(frequencies
(map second hand)))))))
!

(four-of-kind?
[[:clubs 13] [:diamonds 8] [:hearts 8] [:clubs 8] [:spades 8]])
Prefix Notation
(defn four-of-kind? [hand]
(= 4
(first
(sort >
(map second
(frequencies
(map second hand)))))))

Start here

!

(four-of-kind?
[[:clubs 13] [:diamonds 8] [:hearts 8] [:clubs 8] [:spades 8]])
Map applies ‘second’ to the array of arrays ‘hand’
(defn four-of-kind? [hand]
(= 4
(first
(sort >
(map second
(frequencies
(map second hand)))))))

user=> (map second hand)!
(13 8 8 8 8)

!

(four-of-kind?
[[:clubs 13] [:diamonds 8] [:hearts 8] [:clubs 8] [:spades 8]])
Frequencies computes number of occurrences
(defn four-of-kind? [hand]
user=> frequencies(…)!
(= 4
{8 4, 13 1}
(first
(sort >
(map second
Hand has three
(frequencies
occurrences of
(map second hand))))))) ;(13 8 8 8 8)
“4” and one “7”
!

(four-of-kind?
[[:clubs 13] [:diamonds 8] [:hearts 8] [:clubs 8] [:spades 8]])
‘map second’ is used again to get the occurrences
(defn four-of-kind? [hand]
user=> (map
(= 4
(4 1)
(first
(sort >
(map second
(frequencies ;{8 4, 13 1}
(map second hand))))))) ;(13 8 8 8 8)

second (freq…!

(four-of-kind?
[[:clubs 13] [:diamonds 8] [:hearts 8] [:clubs 8] [:spades 8]])
Sort for the highest, take the first and see if it is 4
(defn four-of-kind? [hand]
(= 4
(first
(sort >
(map second ; (4 1)
(frequencies ;{8 4, 13 1}
(map second hand))))))) ;(13 8 8 8 8)
!

(four-of-kind?
[[:clubs 13] [:diamonds 8] [:hearts 8] [:clubs 8] [:spades 8]])
Terse but clear

(defn four-of-kind? [hand]
(= 4
(first
(sort >
(map second
(frequencies
(map second hand)))))))
In Java, the signal to noise ratio is higher
Card[] hand = {new Card("Diamond", 2), new Card("Club", 2), new Card("Heart", 2), new Card("Spade", 2), new
Card("Diamond", 3)};

!
private boolean isNOfAKind(Integer n, Card[] hand) {
Map<Integer, Integer> frequencies = new HashMap<Integer, Integer>();
for(Card card: hand){
if(!frequencies.containsKey(card.num)){
frequencies.put(card.num, 1);
}else{
Integer frequency = frequencies.get(card.num);
frequencies.put(card.num, ++frequency);
}
}
List<Integer> counts = new ArrayList<Integer>(frequencies.values());
Collections.sort(counts, Collections.reverseOrder());
return n.equals(frequencies.get(0));
}
static class Card {
String suit;
Integer num;
public Card(String suit, int num) {
this.suit = suit;
this.num = num;
}
Lisps are notoriously slow
1m invocations of four-of-a-kind:
•

Java: 514ms

•

Clojure: 2,536ms

Java is 5x faster (in this overly simple test)
Why learn Clojure?

“Learning a functional approach is good
for your imperative programming”
Why learn Clojure?

Less bloat makes software easier to
comprehend.
Why learn Clojure?

Runs on JVM so you can always drop
back into the world you know
Why learn Clojure?

Immutability: ‘once you have written
software with immutable data
structures you won’t want to go back’
Why learn Clojure?

“The language of Language Makers”
• Came from AI
• Macros allow you to operate on code
whilst it is still data so anything is
possible.
• Language changes are just additional
libraries!
Small but growing community
My thoughts so far
•

Enjoying the complete rather than partial shift

•

Feedback cycle working in the REPL is awesome, TDD++

•

You don’t loose your line of thought through typing / refactoring
=> easy to get flow

•

Surprisingly elegant.

•

Huge amount of depth to it. I’ve only really scratched the
surface

•

Still wondering how manageable a large code base would be?
Hacker and Painters

“All makers have the same problem:
there is not much money to be made
on things fun to work on.
One answer is to have a day job, as
painters do.”
Easy to get started with
•

Lein (think Maven with less xml)

•

Eclipse and Intelij plugins

•

Free books:
•

Pragmatic Programmers “Programming Clojure”

•

Clojure section of 7 languages in 7 weeks

•

Structure and Interpretation of Computer Programs

•

http://www.4clojure.com/ (learning problems site)

•

The Little Schema, learn by example

More Related Content

Viewers also liked (20)

PDF
JAX London Slides
Ben Stopford
 
PDF
Streaming, Database & Distributed Systems Bridging the Divide
Ben Stopford
 
PDF
Microservices for a Streaming World
Ben Stopford
 
PDF
Data Pipelines with Apache Kafka
Ben Stopford
 
PPTX
Big iron 2 (published)
Ben Stopford
 
PDF
Big Data & the Enterprise
Ben Stopford
 
PDF
The return of big iron?
Ben Stopford
 
PDF
The Power of the Log
Ben Stopford
 
PDF
Linux Performance Tools
Brendan Gregg
 
PDF
Where Does Big Data Meet Big Database - QCon 2012
Ben Stopford
 
ODP
Time Manager Workshop at #QGIS2015 Conference in Nodebo
Anita Graser
 
PPT
forward and backward chaining
Rado Sianipar
 
PDF
Spark Summit EU talk by Christos Erotocritou
Spark Summit
 
PPTX
Kafka for data scientists
Jenn Rawlins
 
PDF
Wrangling Big Data in a Small Tech Ecosystem
Shalin Hai-Jew
 
PPTX
Streaming datasets for personalization
Shriya Arora
 
PPTX
Online learning with structured streaming, spark summit brussels 2016
Ram Sriharsha
 
PPTX
Kafka Streams: The Stream Processing Engine of Apache Kafka
Eno Thereska
 
PPT
Best Practices for testing of SOA-based systems - with examples of SOA Suite 11g
Guido Schmutz
 
PDF
Spark Summit EU talk by Ram Sriharsha and Vlad Feinberg
Spark Summit
 
JAX London Slides
Ben Stopford
 
Streaming, Database & Distributed Systems Bridging the Divide
Ben Stopford
 
Microservices for a Streaming World
Ben Stopford
 
Data Pipelines with Apache Kafka
Ben Stopford
 
Big iron 2 (published)
Ben Stopford
 
Big Data & the Enterprise
Ben Stopford
 
The return of big iron?
Ben Stopford
 
The Power of the Log
Ben Stopford
 
Linux Performance Tools
Brendan Gregg
 
Where Does Big Data Meet Big Database - QCon 2012
Ben Stopford
 
Time Manager Workshop at #QGIS2015 Conference in Nodebo
Anita Graser
 
forward and backward chaining
Rado Sianipar
 
Spark Summit EU talk by Christos Erotocritou
Spark Summit
 
Kafka for data scientists
Jenn Rawlins
 
Wrangling Big Data in a Small Tech Ecosystem
Shalin Hai-Jew
 
Streaming datasets for personalization
Shriya Arora
 
Online learning with structured streaming, spark summit brussels 2016
Ram Sriharsha
 
Kafka Streams: The Stream Processing Engine of Apache Kafka
Eno Thereska
 
Best Practices for testing of SOA-based systems - with examples of SOA Suite 11g
Guido Schmutz
 
Spark Summit EU talk by Ram Sriharsha and Vlad Feinberg
Spark Summit
 

Similar to A little bit of clojure (20)

PDF
Brief intro to clojure
Roy Rutto
 
PDF
Clojure - An Introduction for Lisp Programmers
elliando dias
 
ODP
Getting started with Clojure
John Stevenson
 
PDF
Clojure
Rohit Vaidya
 
PDF
Introduction to Clojure
Renzo Borgatti
 
PDF
Clojure values
Christophe Grand
 
PDF
Clojure made-simple - John Stevenson
JAX London
 
PDF
Introduction to clojure
Abhishek Mahawar
 
PDF
The Ideas of Clojure - Things I learn from Clojure
Hsuan Fu Lien
 
PDF
Clojure Small Intro
John Vlachoyiannis
 
PDF
Introduction to clojure
Abbas Raza
 
ODP
Clojure
alandipert
 
KEY
Clojure Intro
thnetos
 
PDF
Clojure class
Aysylu Greenberg
 
KEY
Scala clojure techday_2011
Thadeu Russo
 
PDF
Introductory Clojure Presentation
Jay Victoria
 
PDF
Exploring Clojurescript
Luke Donnet
 
PDF
Pune Clojure Course Outline
Baishampayan Ghose
 
KEY
(map Clojure everyday-tasks)
Jacek Laskowski
 
PDF
Diving into Functional Programming
Lev Walkin
 
Brief intro to clojure
Roy Rutto
 
Clojure - An Introduction for Lisp Programmers
elliando dias
 
Getting started with Clojure
John Stevenson
 
Clojure
Rohit Vaidya
 
Introduction to Clojure
Renzo Borgatti
 
Clojure values
Christophe Grand
 
Clojure made-simple - John Stevenson
JAX London
 
Introduction to clojure
Abhishek Mahawar
 
The Ideas of Clojure - Things I learn from Clojure
Hsuan Fu Lien
 
Clojure Small Intro
John Vlachoyiannis
 
Introduction to clojure
Abbas Raza
 
Clojure
alandipert
 
Clojure Intro
thnetos
 
Clojure class
Aysylu Greenberg
 
Scala clojure techday_2011
Thadeu Russo
 
Introductory Clojure Presentation
Jay Victoria
 
Exploring Clojurescript
Luke Donnet
 
Pune Clojure Course Outline
Baishampayan Ghose
 
(map Clojure everyday-tasks)
Jacek Laskowski
 
Diving into Functional Programming
Lev Walkin
 
Ad

More from Ben Stopford (20)

PPTX
10 Principals for Effective Event-Driven Microservices with Apache Kafka
Ben Stopford
 
PPTX
10 Principals for Effective Event Driven Microservices
Ben Stopford
 
PDF
The Future of Streaming: Global Apps, Event Stores and Serverless
Ben Stopford
 
PDF
A Global Source of Truth for the Microservices Generation
Ben Stopford
 
PDF
Building Event Driven Services with Kafka Streams
Ben Stopford
 
PDF
NDC London 2017 - The Data Dichotomy- Rethinking Data and Services with Streams
Ben Stopford
 
PDF
Building Event Driven Services with Apache Kafka and Kafka Streams - Devoxx B...
Ben Stopford
 
PDF
Building Event Driven Services with Stateful Streams
Ben Stopford
 
PDF
Devoxx London 2017 - Rethinking Services With Stateful Streams
Ben Stopford
 
PDF
Event Driven Services Part 2: Building Event-Driven Services with Apache Kafka
Ben Stopford
 
PDF
Event Driven Services Part 1: The Data Dichotomy
Ben Stopford
 
PDF
Event Driven Services Part 3: Putting the Micro into Microservices with State...
Ben Stopford
 
PDF
Strata Software Architecture NY: The Data Dichotomy
Ben Stopford
 
PPTX
Advanced databases ben stopford
Ben Stopford
 
PDF
Coherence Implementation Patterns - Sig Nov 2011
Ben Stopford
 
PDF
A Paradigm Shift: The Increasing Dominance of Memory-Oriented Solutions for H...
Ben Stopford
 
PDF
Balancing Replication and Partitioning in a Distributed Java Database
Ben Stopford
 
PDF
Test-Oriented Languages: Is it time for a new era?
Ben Stopford
 
PDF
Ideas for Distributing Skills Across a Continental Divide
Ben Stopford
 
PDF
Data Grids with Oracle Coherence
Ben Stopford
 
10 Principals for Effective Event-Driven Microservices with Apache Kafka
Ben Stopford
 
10 Principals for Effective Event Driven Microservices
Ben Stopford
 
The Future of Streaming: Global Apps, Event Stores and Serverless
Ben Stopford
 
A Global Source of Truth for the Microservices Generation
Ben Stopford
 
Building Event Driven Services with Kafka Streams
Ben Stopford
 
NDC London 2017 - The Data Dichotomy- Rethinking Data and Services with Streams
Ben Stopford
 
Building Event Driven Services with Apache Kafka and Kafka Streams - Devoxx B...
Ben Stopford
 
Building Event Driven Services with Stateful Streams
Ben Stopford
 
Devoxx London 2017 - Rethinking Services With Stateful Streams
Ben Stopford
 
Event Driven Services Part 2: Building Event-Driven Services with Apache Kafka
Ben Stopford
 
Event Driven Services Part 1: The Data Dichotomy
Ben Stopford
 
Event Driven Services Part 3: Putting the Micro into Microservices with State...
Ben Stopford
 
Strata Software Architecture NY: The Data Dichotomy
Ben Stopford
 
Advanced databases ben stopford
Ben Stopford
 
Coherence Implementation Patterns - Sig Nov 2011
Ben Stopford
 
A Paradigm Shift: The Increasing Dominance of Memory-Oriented Solutions for H...
Ben Stopford
 
Balancing Replication and Partitioning in a Distributed Java Database
Ben Stopford
 
Test-Oriented Languages: Is it time for a new era?
Ben Stopford
 
Ideas for Distributing Skills Across a Continental Divide
Ben Stopford
 
Data Grids with Oracle Coherence
Ben Stopford
 
Ad

Recently uploaded (20)

PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
July Patch Tuesday
Ivanti
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
July Patch Tuesday
Ivanti
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
Biography of Daniel Podor.pdf
Daniel Podor
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 

A little bit of clojure

  • 1. A very little bit of Clojure Ben Stopford* ! ! ! ! *(not a Clojure expert)
  • 2. Clojure • Clojure is a Lisp - the oldest programming language other than Fortran • Unusual syntax, stemming from polish notation • Functional, but not pure functional (like say Haskell) • JVM based and Dynamically Typed
  • 3. Lisps • LISP stands for LISt Processing, it’s a language of lists. • Form: (function arg1 arg2 arg3) (* 1 2 3) => 6
  • 4. Lets look at a simple function (defn four-of-kind? [hand] (= 4 (first (sort > (map second (frequencies (map second hand))))))) ! (four-of-kind? [[:clubs 13] [:diamonds 8] [:hearts 8] [:clubs 8] [:spades 8]])
  • 5. Everything is a list (defn four-of-kind? [hand] (= 4 (first (sort > (map second (frequencies (map second hand))))))) ! (four-of-kind? [[:clubs 13] [:diamonds 8] [:hearts 8] [:clubs 8] [:spades 8]])
  • 6. Prefix Notation (defn four-of-kind? [hand] (= 4 (first (sort > (map second (frequencies (map second hand))))))) Start here ! (four-of-kind? [[:clubs 13] [:diamonds 8] [:hearts 8] [:clubs 8] [:spades 8]])
  • 7. Map applies ‘second’ to the array of arrays ‘hand’ (defn four-of-kind? [hand] (= 4 (first (sort > (map second (frequencies (map second hand))))))) user=> (map second hand)! (13 8 8 8 8) ! (four-of-kind? [[:clubs 13] [:diamonds 8] [:hearts 8] [:clubs 8] [:spades 8]])
  • 8. Frequencies computes number of occurrences (defn four-of-kind? [hand] user=> frequencies(…)! (= 4 {8 4, 13 1} (first (sort > (map second Hand has three (frequencies occurrences of (map second hand))))))) ;(13 8 8 8 8) “4” and one “7” ! (four-of-kind? [[:clubs 13] [:diamonds 8] [:hearts 8] [:clubs 8] [:spades 8]])
  • 9. ‘map second’ is used again to get the occurrences (defn four-of-kind? [hand] user=> (map (= 4 (4 1) (first (sort > (map second (frequencies ;{8 4, 13 1} (map second hand))))))) ;(13 8 8 8 8) second (freq…! (four-of-kind? [[:clubs 13] [:diamonds 8] [:hearts 8] [:clubs 8] [:spades 8]])
  • 10. Sort for the highest, take the first and see if it is 4 (defn four-of-kind? [hand] (= 4 (first (sort > (map second ; (4 1) (frequencies ;{8 4, 13 1} (map second hand))))))) ;(13 8 8 8 8) ! (four-of-kind? [[:clubs 13] [:diamonds 8] [:hearts 8] [:clubs 8] [:spades 8]])
  • 11. Terse but clear (defn four-of-kind? [hand] (= 4 (first (sort > (map second (frequencies (map second hand)))))))
  • 12. In Java, the signal to noise ratio is higher Card[] hand = {new Card("Diamond", 2), new Card("Club", 2), new Card("Heart", 2), new Card("Spade", 2), new Card("Diamond", 3)}; ! private boolean isNOfAKind(Integer n, Card[] hand) { Map<Integer, Integer> frequencies = new HashMap<Integer, Integer>(); for(Card card: hand){ if(!frequencies.containsKey(card.num)){ frequencies.put(card.num, 1); }else{ Integer frequency = frequencies.get(card.num); frequencies.put(card.num, ++frequency); } } List<Integer> counts = new ArrayList<Integer>(frequencies.values()); Collections.sort(counts, Collections.reverseOrder()); return n.equals(frequencies.get(0)); } static class Card { String suit; Integer num; public Card(String suit, int num) { this.suit = suit; this.num = num; }
  • 13. Lisps are notoriously slow 1m invocations of four-of-a-kind: • Java: 514ms • Clojure: 2,536ms Java is 5x faster (in this overly simple test)
  • 14. Why learn Clojure? “Learning a functional approach is good for your imperative programming”
  • 15. Why learn Clojure? Less bloat makes software easier to comprehend.
  • 16. Why learn Clojure? Runs on JVM so you can always drop back into the world you know
  • 17. Why learn Clojure? Immutability: ‘once you have written software with immutable data structures you won’t want to go back’
  • 18. Why learn Clojure? “The language of Language Makers” • Came from AI • Macros allow you to operate on code whilst it is still data so anything is possible. • Language changes are just additional libraries!
  • 19. Small but growing community
  • 20. My thoughts so far • Enjoying the complete rather than partial shift • Feedback cycle working in the REPL is awesome, TDD++ • You don’t loose your line of thought through typing / refactoring => easy to get flow • Surprisingly elegant. • Huge amount of depth to it. I’ve only really scratched the surface • Still wondering how manageable a large code base would be?
  • 21. Hacker and Painters “All makers have the same problem: there is not much money to be made on things fun to work on. One answer is to have a day job, as painters do.”
  • 22. Easy to get started with • Lein (think Maven with less xml) • Eclipse and Intelij plugins • Free books: • Pragmatic Programmers “Programming Clojure” • Clojure section of 7 languages in 7 weeks • Structure and Interpretation of Computer Programs • http://www.4clojure.com/ (learning problems site) • The Little Schema, learn by example