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

PDF
Lisp for Python Programmers
PDF
Brief tour of psp-std
KEY
Incremental Development with Lisp: Building a Game and a Website
KEY
LISP: How I Learned To Stop Worrying And Love Parantheses
PDF
Ruby is an Acceptable Lisp
PDF
Snakes and ladders
PDF
Haskell Introduction
PDF
Elixir introd
Lisp for Python Programmers
Brief tour of psp-std
Incremental Development with Lisp: Building a Game and a Website
LISP: How I Learned To Stop Worrying And Love Parantheses
Ruby is an Acceptable Lisp
Snakes and ladders
Haskell Introduction
Elixir introd

Viewers also liked (20)

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

Similar to A little bit of clojure (20)

PDF
Brief intro to clojure
PDF
Clojure - An Introduction for Lisp Programmers
ODP
Getting started with Clojure
PDF
Clojure
PDF
Introduction to Clojure
PDF
Clojure values
PDF
Clojure made-simple - John Stevenson
PDF
Introduction to clojure
PDF
The Ideas of Clojure - Things I learn from Clojure
PDF
Clojure Small Intro
PDF
Introduction to clojure
ODP
Clojure
KEY
Clojure Intro
PDF
Clojure class
KEY
Scala clojure techday_2011
PDF
Introductory Clojure Presentation
PDF
Exploring Clojurescript
PDF
Pune Clojure Course Outline
KEY
(map Clojure everyday-tasks)
PDF
Diving into Functional Programming
Brief intro to clojure
Clojure - An Introduction for Lisp Programmers
Getting started with Clojure
Clojure
Introduction to Clojure
Clojure values
Clojure made-simple - John Stevenson
Introduction to clojure
The Ideas of Clojure - Things I learn from Clojure
Clojure Small Intro
Introduction to clojure
Clojure
Clojure Intro
Clojure class
Scala clojure techday_2011
Introductory Clojure Presentation
Exploring Clojurescript
Pune Clojure Course Outline
(map Clojure everyday-tasks)
Diving into Functional Programming
Ad

More from Ben Stopford (20)

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

Recently uploaded (20)

PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
KodekX | Application Modernization Development
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Encapsulation theory and applications.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Machine learning based COVID-19 study performance prediction
PDF
Approach and Philosophy of On baking technology
PPTX
Cloud computing and distributed systems.
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
The AUB Centre for AI in Media Proposal.docx
KodekX | Application Modernization Development
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
The Rise and Fall of 3GPP – Time for a Sabbatical?
Encapsulation theory and applications.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Big Data Technologies - Introduction.pptx
Programs and apps: productivity, graphics, security and other tools
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Machine learning based COVID-19 study performance prediction
Approach and Philosophy of On baking technology
Cloud computing and distributed systems.
Encapsulation_ Review paper, used for researhc scholars
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Understanding_Digital_Forensics_Presentation.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
20250228 LYD VKU AI Blended-Learning.pptx

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