SlideShare a Scribd company logo
1 of 28
Download to read offline
A taste of
!
!
David Leung!
!
Twitter: @davleung
Email: david@davidslab.com
GPG Fingerprint: 217E 1ECE 2349 D178 73E5 C194 1E2E C02A A74A A531
What is Clojure?
Hosted
Runs on multiple major
platforms
JVM
CLR
JS Runtime (ClojureScript)
What is Clojure?
Hosted
Excellent Interops
EXAMPLE 1
Retrieve a webpage using Java library
!
!
;;	
  Imports	
  the	
  java.net.URL	
  class	
  into	
  current	
  namespace	
  
(import	
  ‘java.net.URL)	
  
	
  	
  
;;	
  Creating	
  an	
  instance	
  of	
  URL.	
  
;;	
  In	
  Java:	
  
;;	
  	
  	
  URL	
  con	
  =	
  new	
  URL(“http://www.reddit.com”)	
  
(def	
  con	
  (URL.	
  “http://www.reddit.com”))	
  
!
!
;;	
  GET	
  the	
  page	
  and	
  print	
  it!	
  
(println	
  (slurp	
  con))
What is Clojure?
Hosted
Functional
First class functions
Build software by composing over functions
EXAMPLE 2
Applying arbitrary function to a collection of data
!
;;	
  Name	
  a	
  collection	
  of	
  numbers	
  
(def	
  data	
  [1	
  2	
  3	
  4	
  5])	
  
!
;;	
  Creates	
  a	
  function	
  called	
  process-­‐data	
  that	
  accepts	
  a	
  collection	
  and	
  an	
  operation	
  to	
  be
(defn	
  process-­‐data	
  [collection	
  operation]	
  
	
  	
  	
  	
  (apply	
  operation	
  collection))	
  
!
;;	
  Addition	
  
(process-­‐data	
  data	
  +)	
  
!
;;	
  Multiplication	
  
(process-­‐data	
  data	
  *)	
  
!
Relax if you aren’t following the syntax :-)
You’ll learn this in the workshop
What is Clojure?
Hosted
Functional
Immutability
Easy Reasoning and Debugging
In the following Ruby code, can you be sure
that x is not modified?
!
x	
  =	
  {problems:	
  99}	
  
!
dodgyMethod(x);	
  
!
#	
  what	
  is	
  the	
  value	
  of	
  x?	
  
x[":problems"]	
  
What is Clojure?
Hosted
Functional
Immutability
Separation of Identity & State
!
Identity is a reference to something.
State is the aggregated values of an identity
at a particular point in time.
In Clojure, an identity’s state is updated by
creating a new value and assign it back to the
identify.
!
Whaaaaat? Isn’t that inefficient???
What is Clojure?
Hosted
Functional
Immutability
Efficient Structure Sharing
!
Let’s create a hash:
!(def	
  a	
  {:a	
  5	
  :b	
  6	
  :c	
  7	
  :d	
  8})	
  
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
Credit: Image from “Clojure Programming” by Chas Emerick, Brian Carper & Christophe Grand
What is Clojure?
Hosted
Functional
Immutability
Efficient Structure Sharing
!
!(def	
  a	
  {:a	
  5	
  :b	
  6	
  :c	
  7	
  :d	
  8})	
  
!
(def	
  b	
  (assoc	
  a	
  :c	
  0))	
  
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
“Copying” data is cheap!
!
!
!
Credit: Image from “Clojure Programming” by Chas Emerick, Brian Carper & Christophe Grand
What is Clojure?
Hosted
Functional
Immutability
Fosters Concurrent Programming
!
“Immutable objects are always
thread safe.”
—Brian Goetz

(Author of Java Concurrency in Practice)
!
!
!
More about Concurrency later
What is Clojure?
Hosted
Functional
Parallelism
Parallel processing
Trivial Parallel Processing Problem
Problem	 The Boss just asked you to parse
100K serialized JSON objects into a hash.
Each string is ~300KB.
The collection/array of strings are stored in
the variable problemsCollection.
!
Write a snippet that makes use of all cores to process
this collection. Don’t worry about storing the result.
!
Do this within a minute, or you are fired.
!
Time starts now.
What is Clojure?
Hosted
Functional
Parallelism
Parallel processing
Trivial Parallel Processing Problem
Solution
!
!
;;	
  non-­‐parallelized	
  version	
  that	
  will	
  get	
  you	
  fired	
  
(dorun	
  (map	
  json/read-­‐str	
  problemsCollection))	
  
!
;;	
  parallelized	
  version	
  
(dorun	
  (pmap	
  json/read-­‐str	
  problemsCollection))	
  
!
!
EXAMPLE 3 (DEMO)
!
Note	 pmap is a parallelized version of map
What is Clojure?
Hosted
Functional
Parallelism
Parallel processing
Watch out for the overhead involved in
distributing work to the work!
!
EXAMPLE 4 (DEMO)
!
What is Clojure?
Hosted
Functional
Parallelism
Concurrency
Two types of

Concurrent Operations
Coordinated Concurrency
One or more actors (threads) must coordinate
to produce the right computation.
e.g. Bank transactions
!
Synchronous Concurrency
Should the thread that wants to update a
reference be blocked until their return?
What is Clojure?
Hosted
Functional
Parallelism
Concurrency
3 flavors of Reference
Types
Coordinated Uncoordinated
Synchronous Refs Atoms
Asynchronous Agents
What is Clojure?
Hosted
Functional
Parallelism
Concurrency
Atoms
Create an atom
(def	
  counter	
  
	
  	
  	
  	
  (atom	
  0))	
  
Access an atom’s value (dereferencing)
(deref	
  counter)	
  
!
;;	
  syntactical	
  sugar	
  
@counter	
  
Compare and Swap
(swap!	
  counter	
  +	
  2)	
  
!
Note:	 Clojure uses ! in a function’s name to
	 	 denote that the function mutates 	
	 	 state (memory/IO).
What is Clojure?
Hosted
Functional
Parallelism
Concurrency
Software Transactional
Memory (STM)
STM: Database-like Transactions for Concurrency
!
Clojure controls updates to references via a system
called STM. It removes the need for manual locking.
!
!
!
!
!
Fulfills the Atomicity, Consistency, and Isolation
aspects of the Database Transaction Properties ACID.
STM is to concurrency
what
Garbage Collector is to memory management
What is Clojure?
Hosted
Functional
Parallelism
Concurrency
Software Transactional
Memory (STM)
There are more to concurrency
Clojure has “data flow variables” too!
We’ll cover concurrency in the workshops
later. Things we will look at:
!
Future
Delay
Promise
What is Clojure?
Hosted
Functional
Parallelism
Concurrency
Lisp
What is a Lisp?
!
!
!
!
!
!
Lisp is invented by John McCarthy in 1958.
Lisp pioneered ideas like tree data structures,
dynamic typing, garbage collection, higher-
order functions, recursion.
John McCarthy is quite the hero in Computer
Science.
What is Clojure?
Hosted
Functional
Parallelism
Concurrency
Lisp
Lisp: LISt Processing
Lisp is written in symbolic expressions, or S-
expressions.
S-expressions are written as a pair of parenthesis
surrounding a sequence of symbols:
(f a b c)
!
The first position in the list is also known as the
functional position. When evaluated, the function f will
be called with the rest of the list as arguments.
What is Clojure?
Hosted
Functional
Parallelism
Concurrency
Lisp
Collection Types
Collection types in Clojure
!
List		 (a b c d e)	 	 Essentially a linked list
Vector	 [a b c d e]	 	 Like “arrays” in Ruby/Python
!
Wait a second…
Didn’t we already see those square brackets and
parentheses when we define a function?
What is Clojure?
Hosted
Functional
Parallelism
Concurrency
Lisp
Homoiconicity
Homoiconicity
Lisp is a language where the structure of the code is
represented by standard data structures (list, vectors,
hash).
Repeat after me
!
Code as Data. Data as Code.
!
!
(defn	
  wont-­‐make-­‐you-­‐blind	
  [name]	
  
	
  	
  (println	
  name	
  ",	
  parentheses	
  won't	
  make	
  you	
  
blind"))
What is Clojure?
Hosted
Functional
Parallelism
Concurrency
Lisp
Homoiconicity
Construct a function call with collection manipulation
functions
!
;;	
  the	
  quotation	
  mark	
  stops	
  Clojure	
  treating	
  the	
  
;;	
  list	
  as	
  a	
  function	
  call	
  
(def	
  arguments	
  '(1	
  2	
  3))	
  
!
;;	
  cons	
  is	
  a	
  traditional	
  lisp	
  function	
  
;;	
  that	
  prepends	
  an	
  object	
  to	
  the	
  head	
  of	
  a	
  list	
  
(def	
  function-­‐call	
  (cons	
  +	
  arguments))	
  
!
(eval	
  function-­‐call)	
  
;;	
  =>	
  6	
  
!
!
What is Clojure?
Hosted
Functional
Parallelism
Concurrency
Lisp
Macros
Building Malleable Systems
!
As well as being able to write code that “writes
code” (metaprogramming), you can even change the
evaluation semantics by writing macros!
We’ll explore this in the workshops.
What is Clojure?
Hosted
Functional
Parallelism
Concurrency
Lisp
Language Extensions
The flexible of Clojure allows Clojure and your
codebase to stretch and bend to your needs.
Notable Clojure extensions:
!
core.async	 	 	 Implementation of channels and
	 	 	 	 	 blocks from the Go programming
	 	 	 	 	 language.
core.logic	 	 	 Implementation of miniKanren for
	 	 	 	 	 doing relational/constraint logic
	 	 	 	 	 programming.
core.typed	 	 	 Optional typing for Clojure.
Working with Clojure
Editors
!
emacs
Light Table
VimClojure
CounterClockWise (eclipse)
IntelliJ
Working with Clojure
Build Tools
!
	

 	

 	

 	

 	

 Leiningen	

	

 	

 	

 	

 	

 for automating Clojure projects without setting your hair on fire	

	

 	

 	

 	

 	

 	

	

 	

 	

 	

 	

 	

	

 	

 	

 	

 	

 http://leiningen.org	

!
ht
Working with Clojure
Learning Resources
	

 	

 	

 	

 	

 	

Test Driven Learning
	 4Clojure	 	 	 	 4clojure.com
	 Clojure Koans		 	 clojurekoans.com
!
Books
	 Clojure Programming
	 Joy of Clojure Second Edition
	 Clojure Cookbook
Working with Clojure
!
!
David Leung!
!
Twitter: @davleung
Email: david@davidslab.com
GPG Fingerprint: 217E 1ECE 2349 D178 73E5 C194 1E2E C02A A74A A531

More Related Content

What's hot

Node.js: A Guided Tour
Node.js: A Guided TourNode.js: A Guided Tour
Node.js: A Guided Tourcacois
 
Quick Introduction to Kotlin Coroutine for Android Dev
Quick Introduction to Kotlin Coroutine for Android DevQuick Introduction to Kotlin Coroutine for Android Dev
Quick Introduction to Kotlin Coroutine for Android DevShuhei Shogen
 
Connecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRubyConnecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRubyNick Sieger
 
Java Serialization Facts and Fallacies
Java Serialization Facts and FallaciesJava Serialization Facts and Fallacies
Java Serialization Facts and FallaciesRoman Elizarov
 
Xitrum Web Framework Live Coding Demos / Xitrum Web Framework ライブコーディング
Xitrum Web Framework Live Coding Demos / Xitrum Web Framework ライブコーディングXitrum Web Framework Live Coding Demos / Xitrum Web Framework ライブコーディング
Xitrum Web Framework Live Coding Demos / Xitrum Web Framework ライブコーディングscalaconfjp
 
Clojure made-simple - John Stevenson
Clojure made-simple - John StevensonClojure made-simple - John Stevenson
Clojure made-simple - John StevensonJAX London
 
Clojure for Java developers
Clojure for Java developersClojure for Java developers
Clojure for Java developersJohn Stevenson
 
Node Architecture and Getting Started with Express
Node Architecture and Getting Started with ExpressNode Architecture and Getting Started with Express
Node Architecture and Getting Started with Expressjguerrero999
 
Understanding the Single Thread Event Loop
Understanding the Single Thread Event LoopUnderstanding the Single Thread Event Loop
Understanding the Single Thread Event LoopTorontoNodeJS
 
Kotlin @ Coupang Backed - JetBrains Day seoul 2018
Kotlin @ Coupang Backed - JetBrains Day seoul 2018Kotlin @ Coupang Backed - JetBrains Day seoul 2018
Kotlin @ Coupang Backed - JetBrains Day seoul 2018Sunghyouk Bae
 
Building Fast, Modern Web Applications with Node.js and CoffeeScript
Building Fast, Modern Web Applications with Node.js and CoffeeScriptBuilding Fast, Modern Web Applications with Node.js and CoffeeScript
Building Fast, Modern Web Applications with Node.js and CoffeeScriptroyaldark
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojureAbbas Raza
 
Introduction to node.js
Introduction to node.jsIntroduction to node.js
Introduction to node.jsjacekbecela
 
Node.js Patterns for Discerning Developers
Node.js Patterns for Discerning DevelopersNode.js Patterns for Discerning Developers
Node.js Patterns for Discerning Developerscacois
 
Kotlin @ Coupang Backend 2017
Kotlin @ Coupang Backend 2017Kotlin @ Coupang Backend 2017
Kotlin @ Coupang Backend 2017Sunghyouk Bae
 
Original slides from Ryan Dahl's NodeJs intro talk
Original slides from Ryan Dahl's NodeJs intro talkOriginal slides from Ryan Dahl's NodeJs intro talk
Original slides from Ryan Dahl's NodeJs intro talkAarti Parikh
 
Multithreading and Parallelism on iOS [MobOS 2013]
 Multithreading and Parallelism on iOS [MobOS 2013] Multithreading and Parallelism on iOS [MobOS 2013]
Multithreading and Parallelism on iOS [MobOS 2013]Kuba Břečka
 

What's hot (20)

CoffeeScript By Example
CoffeeScript By ExampleCoffeeScript By Example
CoffeeScript By Example
 
Node.js: A Guided Tour
Node.js: A Guided TourNode.js: A Guided Tour
Node.js: A Guided Tour
 
Quick Introduction to Kotlin Coroutine for Android Dev
Quick Introduction to Kotlin Coroutine for Android DevQuick Introduction to Kotlin Coroutine for Android Dev
Quick Introduction to Kotlin Coroutine for Android Dev
 
Connecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRubyConnecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRuby
 
Java Serialization Facts and Fallacies
Java Serialization Facts and FallaciesJava Serialization Facts and Fallacies
Java Serialization Facts and Fallacies
 
Xitrum Web Framework Live Coding Demos / Xitrum Web Framework ライブコーディング
Xitrum Web Framework Live Coding Demos / Xitrum Web Framework ライブコーディングXitrum Web Framework Live Coding Demos / Xitrum Web Framework ライブコーディング
Xitrum Web Framework Live Coding Demos / Xitrum Web Framework ライブコーディング
 
Clojure made-simple - John Stevenson
Clojure made-simple - John StevensonClojure made-simple - John Stevenson
Clojure made-simple - John Stevenson
 
Zen of Akka
Zen of AkkaZen of Akka
Zen of Akka
 
Clojure for Java developers
Clojure for Java developersClojure for Java developers
Clojure for Java developers
 
Node Architecture and Getting Started with Express
Node Architecture and Getting Started with ExpressNode Architecture and Getting Started with Express
Node Architecture and Getting Started with Express
 
Understanding the Single Thread Event Loop
Understanding the Single Thread Event LoopUnderstanding the Single Thread Event Loop
Understanding the Single Thread Event Loop
 
Kotlin @ Coupang Backed - JetBrains Day seoul 2018
Kotlin @ Coupang Backed - JetBrains Day seoul 2018Kotlin @ Coupang Backed - JetBrains Day seoul 2018
Kotlin @ Coupang Backed - JetBrains Day seoul 2018
 
Building Fast, Modern Web Applications with Node.js and CoffeeScript
Building Fast, Modern Web Applications with Node.js and CoffeeScriptBuilding Fast, Modern Web Applications with Node.js and CoffeeScript
Building Fast, Modern Web Applications with Node.js and CoffeeScript
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojure
 
Kotlin - Better Java
Kotlin - Better JavaKotlin - Better Java
Kotlin - Better Java
 
Introduction to node.js
Introduction to node.jsIntroduction to node.js
Introduction to node.js
 
Node.js Patterns for Discerning Developers
Node.js Patterns for Discerning DevelopersNode.js Patterns for Discerning Developers
Node.js Patterns for Discerning Developers
 
Kotlin @ Coupang Backend 2017
Kotlin @ Coupang Backend 2017Kotlin @ Coupang Backend 2017
Kotlin @ Coupang Backend 2017
 
Original slides from Ryan Dahl's NodeJs intro talk
Original slides from Ryan Dahl's NodeJs intro talkOriginal slides from Ryan Dahl's NodeJs intro talk
Original slides from Ryan Dahl's NodeJs intro talk
 
Multithreading and Parallelism on iOS [MobOS 2013]
 Multithreading and Parallelism on iOS [MobOS 2013] Multithreading and Parallelism on iOS [MobOS 2013]
Multithreading and Parallelism on iOS [MobOS 2013]
 

Viewers also liked

Hopper Elasticsearch Hackathon
Hopper Elasticsearch HackathonHopper Elasticsearch Hackathon
Hopper Elasticsearch Hackathonimotov
 
Boston elasticsearch meetup October 2012
Boston elasticsearch meetup October 2012Boston elasticsearch meetup October 2012
Boston elasticsearch meetup October 2012imotov
 
Elasticsearch Quick Introduction
Elasticsearch Quick IntroductionElasticsearch Quick Introduction
Elasticsearch Quick Introductionimotov
 
Real time analytics using Hadoop and Elasticsearch
Real time analytics using Hadoop and ElasticsearchReal time analytics using Hadoop and Elasticsearch
Real time analytics using Hadoop and ElasticsearchAbhishek Andhavarapu
 
Business Transformation: PwC Presents Its Viewpoint on the Integration Fabric
Business Transformation: PwC Presents Its Viewpoint on the Integration FabricBusiness Transformation: PwC Presents Its Viewpoint on the Integration Fabric
Business Transformation: PwC Presents Its Viewpoint on the Integration FabricCA Technologies
 
Combine Apache Hadoop and Elasticsearch to Get the Most of Your Big Data
Combine Apache Hadoop and Elasticsearch to Get the Most of Your Big DataCombine Apache Hadoop and Elasticsearch to Get the Most of Your Big Data
Combine Apache Hadoop and Elasticsearch to Get the Most of Your Big DataHortonworks
 

Viewers also liked (6)

Hopper Elasticsearch Hackathon
Hopper Elasticsearch HackathonHopper Elasticsearch Hackathon
Hopper Elasticsearch Hackathon
 
Boston elasticsearch meetup October 2012
Boston elasticsearch meetup October 2012Boston elasticsearch meetup October 2012
Boston elasticsearch meetup October 2012
 
Elasticsearch Quick Introduction
Elasticsearch Quick IntroductionElasticsearch Quick Introduction
Elasticsearch Quick Introduction
 
Real time analytics using Hadoop and Elasticsearch
Real time analytics using Hadoop and ElasticsearchReal time analytics using Hadoop and Elasticsearch
Real time analytics using Hadoop and Elasticsearch
 
Business Transformation: PwC Presents Its Viewpoint on the Integration Fabric
Business Transformation: PwC Presents Its Viewpoint on the Integration FabricBusiness Transformation: PwC Presents Its Viewpoint on the Integration Fabric
Business Transformation: PwC Presents Its Viewpoint on the Integration Fabric
 
Combine Apache Hadoop and Elasticsearch to Get the Most of Your Big Data
Combine Apache Hadoop and Elasticsearch to Get the Most of Your Big DataCombine Apache Hadoop and Elasticsearch to Get the Most of Your Big Data
Combine Apache Hadoop and Elasticsearch to Get the Most of Your Big Data
 

Similar to An Introduction to Clojure

Functional web with clojure
Functional web with clojureFunctional web with clojure
Functional web with clojureJohn Stevenson
 
Clojure Fundamentals Course For Beginners
Clojure Fundamentals Course For Beginners Clojure Fundamentals Course For Beginners
Clojure Fundamentals Course For Beginners Paddy Lock
 
Clojure and The Robot Apocalypse
Clojure and The Robot ApocalypseClojure and The Robot Apocalypse
Clojure and The Robot Apocalypseelliando dias
 
Getting started with Clojure
Getting started with ClojureGetting started with Clojure
Getting started with ClojureJohn Stevenson
 
The Why and How of Scala at Twitter
The Why and How of Scala at TwitterThe Why and How of Scala at Twitter
The Why and How of Scala at TwitterAlex Payne
 
Get into Functional Programming with Clojure
Get into Functional Programming with ClojureGet into Functional Programming with Clojure
Get into Functional Programming with ClojureJohn Stevenson
 
Fun with Functional Programming in Clojure - John Stevenson - Codemotion Amst...
Fun with Functional Programming in Clojure - John Stevenson - Codemotion Amst...Fun with Functional Programming in Clojure - John Stevenson - Codemotion Amst...
Fun with Functional Programming in Clojure - John Stevenson - Codemotion Amst...Codemotion
 
Modern JavaScript Development @ DotNetToscana
Modern JavaScript Development @ DotNetToscanaModern JavaScript Development @ DotNetToscana
Modern JavaScript Development @ DotNetToscanaMatteo Baglini
 
Building Concurrent WebObjects applications with Scala
Building Concurrent WebObjects applications with ScalaBuilding Concurrent WebObjects applications with Scala
Building Concurrent WebObjects applications with ScalaWO Community
 
ITB2019 Real World Scenarios for Modern CFML - Nolan Erck
ITB2019 Real World Scenarios for Modern CFML - Nolan ErckITB2019 Real World Scenarios for Modern CFML - Nolan Erck
ITB2019 Real World Scenarios for Modern CFML - Nolan ErckOrtus Solutions, Corp
 
Introduction to Clojure
Introduction to ClojureIntroduction to Clojure
Introduction to ClojureRenzo Borgatti
 
Thinking Functionally - John Stevenson - Codemotion Rome 2017
Thinking Functionally - John Stevenson - Codemotion Rome 2017Thinking Functionally - John Stevenson - Codemotion Rome 2017
Thinking Functionally - John Stevenson - Codemotion Rome 2017Codemotion
 
Thinking Functionally with Clojure
Thinking Functionally with ClojureThinking Functionally with Clojure
Thinking Functionally with ClojureJohn Stevenson
 
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-FrameworksZepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-FrameworksThomas Fuchs
 
Raffaele Rialdi
Raffaele RialdiRaffaele Rialdi
Raffaele RialdiCodeFest
 
Scripting as a Second Language
Scripting as a Second LanguageScripting as a Second Language
Scripting as a Second LanguageRob Dunn
 

Similar to An Introduction to Clojure (20)

Functional web with clojure
Functional web with clojureFunctional web with clojure
Functional web with clojure
 
Clojure Fundamentals Course For Beginners
Clojure Fundamentals Course For Beginners Clojure Fundamentals Course For Beginners
Clojure Fundamentals Course For Beginners
 
Clojure and The Robot Apocalypse
Clojure and The Robot ApocalypseClojure and The Robot Apocalypse
Clojure and The Robot Apocalypse
 
Getting started with Clojure
Getting started with ClojureGetting started with Clojure
Getting started with Clojure
 
The Why and How of Scala at Twitter
The Why and How of Scala at TwitterThe Why and How of Scala at Twitter
The Why and How of Scala at Twitter
 
Get into Functional Programming with Clojure
Get into Functional Programming with ClojureGet into Functional Programming with Clojure
Get into Functional Programming with Clojure
 
Fun with Functional Programming in Clojure - John Stevenson - Codemotion Amst...
Fun with Functional Programming in Clojure - John Stevenson - Codemotion Amst...Fun with Functional Programming in Clojure - John Stevenson - Codemotion Amst...
Fun with Functional Programming in Clojure - John Stevenson - Codemotion Amst...
 
All of javascript
All of javascriptAll of javascript
All of javascript
 
Modern JavaScript Development @ DotNetToscana
Modern JavaScript Development @ DotNetToscanaModern JavaScript Development @ DotNetToscana
Modern JavaScript Development @ DotNetToscana
 
Building Concurrent WebObjects applications with Scala
Building Concurrent WebObjects applications with ScalaBuilding Concurrent WebObjects applications with Scala
Building Concurrent WebObjects applications with Scala
 
All of Javascript
All of JavascriptAll of Javascript
All of Javascript
 
ITB2019 Real World Scenarios for Modern CFML - Nolan Erck
ITB2019 Real World Scenarios for Modern CFML - Nolan ErckITB2019 Real World Scenarios for Modern CFML - Nolan Erck
ITB2019 Real World Scenarios for Modern CFML - Nolan Erck
 
Introduction to Clojure
Introduction to ClojureIntroduction to Clojure
Introduction to Clojure
 
Play framework
Play frameworkPlay framework
Play framework
 
Thinking Functionally - John Stevenson - Codemotion Rome 2017
Thinking Functionally - John Stevenson - Codemotion Rome 2017Thinking Functionally - John Stevenson - Codemotion Rome 2017
Thinking Functionally - John Stevenson - Codemotion Rome 2017
 
Thinking Functionally with Clojure
Thinking Functionally with ClojureThinking Functionally with Clojure
Thinking Functionally with Clojure
 
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-FrameworksZepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
 
Clojure
ClojureClojure
Clojure
 
Raffaele Rialdi
Raffaele RialdiRaffaele Rialdi
Raffaele Rialdi
 
Scripting as a Second Language
Scripting as a Second LanguageScripting as a Second Language
Scripting as a Second Language
 

Recently uploaded

Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
"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
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
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
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 

Recently uploaded (20)

Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
"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
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
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
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 

An Introduction to Clojure

  • 1. A taste of ! ! David Leung! ! Twitter: @davleung Email: david@davidslab.com GPG Fingerprint: 217E 1ECE 2349 D178 73E5 C194 1E2E C02A A74A A531
  • 2. What is Clojure? Hosted Runs on multiple major platforms JVM CLR JS Runtime (ClojureScript)
  • 3. What is Clojure? Hosted Excellent Interops EXAMPLE 1 Retrieve a webpage using Java library ! ! ;;  Imports  the  java.net.URL  class  into  current  namespace   (import  ‘java.net.URL)       ;;  Creating  an  instance  of  URL.   ;;  In  Java:   ;;      URL  con  =  new  URL(“http://www.reddit.com”)   (def  con  (URL.  “http://www.reddit.com”))   ! ! ;;  GET  the  page  and  print  it!   (println  (slurp  con))
  • 4. What is Clojure? Hosted Functional First class functions Build software by composing over functions EXAMPLE 2 Applying arbitrary function to a collection of data ! ;;  Name  a  collection  of  numbers   (def  data  [1  2  3  4  5])   ! ;;  Creates  a  function  called  process-­‐data  that  accepts  a  collection  and  an  operation  to  be (defn  process-­‐data  [collection  operation]          (apply  operation  collection))   ! ;;  Addition   (process-­‐data  data  +)   ! ;;  Multiplication   (process-­‐data  data  *)   ! Relax if you aren’t following the syntax :-) You’ll learn this in the workshop
  • 5. What is Clojure? Hosted Functional Immutability Easy Reasoning and Debugging In the following Ruby code, can you be sure that x is not modified? ! x  =  {problems:  99}   ! dodgyMethod(x);   ! #  what  is  the  value  of  x?   x[":problems"]  
  • 6. What is Clojure? Hosted Functional Immutability Separation of Identity & State ! Identity is a reference to something. State is the aggregated values of an identity at a particular point in time. In Clojure, an identity’s state is updated by creating a new value and assign it back to the identify. ! Whaaaaat? Isn’t that inefficient???
  • 7. What is Clojure? Hosted Functional Immutability Efficient Structure Sharing ! Let’s create a hash: !(def  a  {:a  5  :b  6  :c  7  :d  8})   ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! Credit: Image from “Clojure Programming” by Chas Emerick, Brian Carper & Christophe Grand
  • 8. What is Clojure? Hosted Functional Immutability Efficient Structure Sharing ! !(def  a  {:a  5  :b  6  :c  7  :d  8})   ! (def  b  (assoc  a  :c  0))   ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! “Copying” data is cheap! ! ! ! Credit: Image from “Clojure Programming” by Chas Emerick, Brian Carper & Christophe Grand
  • 9. What is Clojure? Hosted Functional Immutability Fosters Concurrent Programming ! “Immutable objects are always thread safe.” —Brian Goetz
 (Author of Java Concurrency in Practice) ! ! ! More about Concurrency later
  • 10. What is Clojure? Hosted Functional Parallelism Parallel processing Trivial Parallel Processing Problem Problem The Boss just asked you to parse 100K serialized JSON objects into a hash. Each string is ~300KB. The collection/array of strings are stored in the variable problemsCollection. ! Write a snippet that makes use of all cores to process this collection. Don’t worry about storing the result. ! Do this within a minute, or you are fired. ! Time starts now.
  • 11. What is Clojure? Hosted Functional Parallelism Parallel processing Trivial Parallel Processing Problem Solution ! ! ;;  non-­‐parallelized  version  that  will  get  you  fired   (dorun  (map  json/read-­‐str  problemsCollection))   ! ;;  parallelized  version   (dorun  (pmap  json/read-­‐str  problemsCollection))   ! ! EXAMPLE 3 (DEMO) ! Note pmap is a parallelized version of map
  • 12. What is Clojure? Hosted Functional Parallelism Parallel processing Watch out for the overhead involved in distributing work to the work! ! EXAMPLE 4 (DEMO) !
  • 13. What is Clojure? Hosted Functional Parallelism Concurrency Two types of
 Concurrent Operations Coordinated Concurrency One or more actors (threads) must coordinate to produce the right computation. e.g. Bank transactions ! Synchronous Concurrency Should the thread that wants to update a reference be blocked until their return?
  • 14. What is Clojure? Hosted Functional Parallelism Concurrency 3 flavors of Reference Types Coordinated Uncoordinated Synchronous Refs Atoms Asynchronous Agents
  • 15. What is Clojure? Hosted Functional Parallelism Concurrency Atoms Create an atom (def  counter          (atom  0))   Access an atom’s value (dereferencing) (deref  counter)   ! ;;  syntactical  sugar   @counter   Compare and Swap (swap!  counter  +  2)   ! Note: Clojure uses ! in a function’s name to denote that the function mutates state (memory/IO).
  • 16. What is Clojure? Hosted Functional Parallelism Concurrency Software Transactional Memory (STM) STM: Database-like Transactions for Concurrency ! Clojure controls updates to references via a system called STM. It removes the need for manual locking. ! ! ! ! ! Fulfills the Atomicity, Consistency, and Isolation aspects of the Database Transaction Properties ACID. STM is to concurrency what Garbage Collector is to memory management
  • 17. What is Clojure? Hosted Functional Parallelism Concurrency Software Transactional Memory (STM) There are more to concurrency Clojure has “data flow variables” too! We’ll cover concurrency in the workshops later. Things we will look at: ! Future Delay Promise
  • 18. What is Clojure? Hosted Functional Parallelism Concurrency Lisp What is a Lisp? ! ! ! ! ! ! Lisp is invented by John McCarthy in 1958. Lisp pioneered ideas like tree data structures, dynamic typing, garbage collection, higher- order functions, recursion. John McCarthy is quite the hero in Computer Science.
  • 19. What is Clojure? Hosted Functional Parallelism Concurrency Lisp Lisp: LISt Processing Lisp is written in symbolic expressions, or S- expressions. S-expressions are written as a pair of parenthesis surrounding a sequence of symbols: (f a b c) ! The first position in the list is also known as the functional position. When evaluated, the function f will be called with the rest of the list as arguments.
  • 20. What is Clojure? Hosted Functional Parallelism Concurrency Lisp Collection Types Collection types in Clojure ! List (a b c d e) Essentially a linked list Vector [a b c d e] Like “arrays” in Ruby/Python ! Wait a second… Didn’t we already see those square brackets and parentheses when we define a function?
  • 21. What is Clojure? Hosted Functional Parallelism Concurrency Lisp Homoiconicity Homoiconicity Lisp is a language where the structure of the code is represented by standard data structures (list, vectors, hash). Repeat after me ! Code as Data. Data as Code. ! ! (defn  wont-­‐make-­‐you-­‐blind  [name]      (println  name  ",  parentheses  won't  make  you   blind"))
  • 22. What is Clojure? Hosted Functional Parallelism Concurrency Lisp Homoiconicity Construct a function call with collection manipulation functions ! ;;  the  quotation  mark  stops  Clojure  treating  the   ;;  list  as  a  function  call   (def  arguments  '(1  2  3))   ! ;;  cons  is  a  traditional  lisp  function   ;;  that  prepends  an  object  to  the  head  of  a  list   (def  function-­‐call  (cons  +  arguments))   ! (eval  function-­‐call)   ;;  =>  6   ! !
  • 23. What is Clojure? Hosted Functional Parallelism Concurrency Lisp Macros Building Malleable Systems ! As well as being able to write code that “writes code” (metaprogramming), you can even change the evaluation semantics by writing macros! We’ll explore this in the workshops.
  • 24. What is Clojure? Hosted Functional Parallelism Concurrency Lisp Language Extensions The flexible of Clojure allows Clojure and your codebase to stretch and bend to your needs. Notable Clojure extensions: ! core.async Implementation of channels and blocks from the Go programming language. core.logic Implementation of miniKanren for doing relational/constraint logic programming. core.typed Optional typing for Clojure.
  • 25. Working with Clojure Editors ! emacs Light Table VimClojure CounterClockWise (eclipse) IntelliJ
  • 26. Working with Clojure Build Tools ! Leiningen for automating Clojure projects without setting your hair on fire http://leiningen.org ! ht
  • 27. Working with Clojure Learning Resources Test Driven Learning 4Clojure 4clojure.com Clojure Koans clojurekoans.com ! Books Clojure Programming Joy of Clojure Second Edition Clojure Cookbook
  • 28. Working with Clojure ! ! David Leung! ! Twitter: @davleung Email: david@davidslab.com GPG Fingerprint: 217E 1ECE 2349 D178 73E5 C194 1E2E C02A A74A A531