SlideShare a Scribd company logo
1 of 30
Clojure

The LISP that makes the JVM dynamic




           http://clojure.org
What is Clojure?
Clojure is a dynamic, LISP-like programming
       language that runs on the JVM.
Clojure History

Started in 2007

Original author (and BDFL) Rich Hickey

Currently version 1.1 (1.2 expected within the next
month)

Available under Eclipse Public License

    “The Eclipse Public License is designed to be a
    business-friendly free software license and
    features weaker copyleft provisions than
    contemporary licenses such as the GNU General
    Public License (GPL).”
Defining features
LISP syntax

  Macros

Immutability

Functional programming

Concurrency
Java interoperability

REPL-based
Syntax
numbers - 1234
ratios - 22/7
strings - “foo”, “bar”

characters - a b c
symbols - foo, bar
keywords - :foo, :bar

boolean - true/false
null - nil
Syntax

Lists -   (1 2 3 4)


Vectors -     [1 2 3 4]


Maps -     {:a 1 :b 2 :c 3}   or   {:a 1, :b 2, :c 3}


Sets -    #{foo bar baz}
Syntax
               That’s it. There is no other syntax[1].

               Data structures are code.

               Homoiconic

               All data literals stand for themselves
               except:

                     Lists

                     Symbols
[1]: Technically, there are other special symbols for shortcutting syntax provided for the developer, but no other necessary syntax
Lists
(+ 1 1) ; => 2

(+ 1 2 3) ; => 6

(+ 1 2 (+ 3 4)) ; => 10

(class “foo”) ; => java.lang.String

(first [:a :b :c]) ; => :a

(rest ‘(“foo” “bar” “baz”)) ; => (“bar” “baz”)

   The ‘ tells the reader not to evaluate the first element as a function

   ‘(1 2 3) is shorthand for (quote (1 2 3))
Hello World
(ns helloworld)

(defn hello
  “An example function”
  [string]
  (println “Hello” string))

(hello “World”)   ; => “Hello World”
REPL demo
Operating on
       Collections
Collections are the main datastructure in
Clojure

A collection can be a list, a vector, a map or a
set

Some example collection functions:

  first, second, last, count, reverse, concat,
  conj, contains?, map, reduce, apply, filter,
  some, remove, every?, nth, into, doall,
  repeat, repeatedly, range,
Laziness
Sequences can be lazy, which allows them to be
infinite:
  (cycle [1 2 3]) ; => (1 2 3 1 2 3 1 2 3 ...)

  (repeat :a) ; => (:a :a :a :a :a :a :a ...)


When using lazy sequences, processing does not
occur until the sequence is realized.
  (class (repeat :a)) ; => clojure.lang.LazySeq


Realize (part of) a lazy sequence:
  (take 5 (repeat 42)) ; => (42 42 42 42 42)
More laziness

Can do neat things with infinite
sequences:
  (filter even? (iterate inc 1)) ; => (2 4 6 8...)


Lazily read lines from a file:
  (read-lines “/tmp/foo.txt”)


  Lines aren’t read until they are used in
  the returned list
Java interop
(.toLowerCase “EMC”) ; => “emc”


   (<method> <object> [<arg1> <arg2> ...])


(StringBuffer. “foo” ) ; => #<StringBuffer foo>


(Integer/parseInt “5”) ; => 5


(.contains “My spoon is too big.” “spoon“) ; => true


(javax.swing.JOptionPane/showMessageDialog nil "Sup!")




      “Clojure is a better Java than Java”
Java datastructures

Anything that’s a Collection (or array) in Java can easily be
treated (or converted) as a Clojure collection

   (first (.split “This is a sentence” “ “)) ; => “This”

   (import ‘java.util.ArrayList)
   (def foo (ArrayList. [1 2 3])) ; => #<ArrayList [1, 2, 3]>
   (seq foo) ; => (1 2 3)
   (vec foo) ; => [1 2 3]
More Java-interop
Type hints

   (defn #^String uppercase [#^String s] (.toUpperCase s))


Proxying Java classes

   (doto (javax.swing.JFrame.)
     (addKeyListener (proxy [java.awt.event.KeyListener] []
       (keyPressed [e] (println (.getKeyChar e) " key pressed"))
       (keyReleased [e] (println (.getKeyChar e) " key released"))
       (keyTyped [e] (println (.getKeyChar e) " key typed"))))
     (setVisible true))


Lots more

   gen-class, annotations, primitives, definterface, defprotocol
Immutability
    (defn add-age
      [person]
      (assoc person :age 10))

    (def timmy {:name “Timmy”})

    (print timmy)   ; => {:name “Timmy”}

    (add-age timmy) ; => {:name “Timmy”, :age 10}

    ; timmy never changes
    (print timmy)   ; => {:name “Timmy”}



Under the hood: 32-way trie trees. O(log32n)
Functional
       Programming
Functions are intended to be side-effect free
   They take values and return values

   Same argument, same result
   No notion of time

   Easy to add concurrency to the mix sanely
   Testing is easier because there is no state

Having no side effects isn’t always possible (io,
mutable java objects)
Concurrency
Locks are bad
Clojure’s defining concurrency feature is STM
   Gives you ACI out of ACID
       Atomicity
       Consistency
       Isolation
Immutability is what makes this possible
4 methods for changing data concurrently:
Dead-simple
        Concurrency
(def accounts [{:name   “Bob”   :balance   1000}
               {:name   “Sue”   :balance   2000}
               {:name   “Joe”   :balance   3000}
               {:name   “Ann”   :balance   4000}])

(defn add-interest
  [acct]
  (let [bal (:balance acct)]
    (Thread/sleep 1500)
    (assoc acct :balance (* bal 1.25))))

(time (doall (map add-interest accounts)))
; => "Elapsed time: 6001.023 msecs"

(time (doall (pmap add-interest accounts)))
; => "Elapsed time: 1519.73 msecs"
More simple
       concurrency
All functions defined in Clojure implement
Runnable:
  (defn dowork [] (println “doing work”))
  (.start (Thread. dowork)) ; => “doing work”


Access to all of Java’s concurrency stuff
(ThreadPoolExecutor, etc) with Java
interop
Concurrency (Refs)

   (def x (ref 100))
   @x ; => 100
   (def amounts [5 10 1 9 2])

   (defn dec-x
     [n]
     (dosync
       (alter x #(- % n))))

   (pmap dec-x amounts)
   @x ; => 73
Concurrency (Atoms)

    (def x (atom 100))
    @x ; => 100
    (def amounts [5 10 1 9 2])

    (defn dec-x
      [n]
      (swap! x #(- % n)))

    (pmap dec-x amounts)
    @x ; => 73
Concurrency (Agents)

   (def x (agent 0))
   @x ; => 0

   (defn increment [c n] (+ c n))

   (send x increment 5)   ; @x -> 5

   (send x increment 10) ; @x -> 15
Macros
(defmacro def-name-filter
  [n r]
  (let [docstring (str "Given a list of people maps, filter " n " in a list.")]
    `(defn ~n
       ~docstring
       [elements#]
       (filter (fn [t#] (re-find ~r (:name t#))) elements#))))

(def-name-filter foo-filter #"^foo")

(def people [{:name "pat"} {:name "foobar"} {:name "foo"} {:name "bar"}])

(doc foo-filter)
; => Given a list of people maps, filter foo-filter in a list.

(foo-filter people)
; => ({:name "foobar"} {:name "foo"})
Macroexpand example
List de-structuring
                                         ; Destructure in defn

                                         (defn print-items
                                           [item & rest]
; Destructure in let                       (println "item:" item)
                                           (println "rest:" rest)
(def my-list ‘([:a 1] [:b 2] [:c 3]))      (if-not (nil? rest)
                                             (apply print-items rest)))
(defn print-pair
  [pair]                                 (print-items 1 2 3 4 5)
  (let [[key val] pair]
     (println "key:" key "val:" val)))   item:   1
                                         rest:   (2 3 4 5)
(map print-pair my-list)                 item:   2
                                         rest:   (3 4 5)
key: :a val: 1                           item:   3
key: :b val: 2                           rest:   (4 5)
key: :c val: 3                           item:   4
                                         rest:   (5)
                                         item:   5
                                         rest:   nil
Much More
Polymorphism/multimethods
Pre and Post conditions for functions

Futures, Promises
Watchers

Bindings/Transients

Metadata features
Compilation

Clojure-in-Clojure
So much more!
More info

http://clojure.org

http://clojure.blip.tv

http://www.assembla.com/wiki/show/clojure/Getting_Started

#clojure on irc.freenode.net

Come talk to me about it!
Thanks!
 Questions?

More Related Content

Viewers also liked

Using Clojure, NoSQL Databases and Functional-Style JavaScript to Write Gext-...
Using Clojure, NoSQL Databases and Functional-Style JavaScript to Write Gext-...Using Clojure, NoSQL Databases and Functional-Style JavaScript to Write Gext-...
Using Clojure, NoSQL Databases and Functional-Style JavaScript to Write Gext-...Stefan Richter
 
Clojure: The Art of Abstraction
Clojure: The Art of AbstractionClojure: The Art of Abstraction
Clojure: The Art of AbstractionAlex Miller
 
Scalaで萌える関数型プログラミング[1.1.RC1]
Scalaで萌える関数型プログラミング[1.1.RC1]Scalaで萌える関数型プログラミング[1.1.RC1]
Scalaで萌える関数型プログラミング[1.1.RC1]Ra Zon
 
Winning the Erlang Edit•Build•Test Cycle
Winning the Erlang Edit•Build•Test CycleWinning the Erlang Edit•Build•Test Cycle
Winning the Erlang Edit•Build•Test CycleRusty Klophaus
 
Erlang - Because s**t Happens by Mahesh Paolini-Subramanya
Erlang - Because s**t Happens by Mahesh Paolini-SubramanyaErlang - Because s**t Happens by Mahesh Paolini-Subramanya
Erlang - Because s**t Happens by Mahesh Paolini-SubramanyaHakka Labs
 
What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)Pavlo Baron
 
Messaging With Erlang And Jabber
Messaging With  Erlang And  JabberMessaging With  Erlang And  Jabber
Messaging With Erlang And Jabberl xf
 
20 reasons why we don't need architects (@pavlobaron)
20 reasons why we don't need architects (@pavlobaron)20 reasons why we don't need architects (@pavlobaron)
20 reasons why we don't need architects (@pavlobaron)Pavlo Baron
 
Clojure made-simple - John Stevenson
Clojure made-simple - John StevensonClojure made-simple - John Stevenson
Clojure made-simple - John StevensonJAX London
 
VoltDB and Erlang - Tech planet 2012
VoltDB and Erlang - Tech planet 2012VoltDB and Erlang - Tech planet 2012
VoltDB and Erlang - Tech planet 2012Eonblast
 
Clojure: Functional Concurrency for the JVM (presented at Open Source Bridge)
Clojure: Functional Concurrency for the JVM (presented at Open Source Bridge)Clojure: Functional Concurrency for the JVM (presented at Open Source Bridge)
Clojure: Functional Concurrency for the JVM (presented at Open Source Bridge)Howard Lewis Ship
 
NDC London 2014: Erlang Patterns Matching Business Needs
NDC London 2014: Erlang Patterns Matching Business NeedsNDC London 2014: Erlang Patterns Matching Business Needs
NDC London 2014: Erlang Patterns Matching Business NeedsTorben Hoffmann
 
Elixir for aspiring Erlang developers
Elixir for aspiring Erlang developersElixir for aspiring Erlang developers
Elixir for aspiring Erlang developersTorben Dohrn
 
Introduction to Erlang for Python Programmers
Introduction to Erlang for Python ProgrammersIntroduction to Erlang for Python Programmers
Introduction to Erlang for Python ProgrammersPython Ireland
 

Viewers also liked (20)

3 years with Clojure
3 years with Clojure3 years with Clojure
3 years with Clojure
 
Using Clojure, NoSQL Databases and Functional-Style JavaScript to Write Gext-...
Using Clojure, NoSQL Databases and Functional-Style JavaScript to Write Gext-...Using Clojure, NoSQL Databases and Functional-Style JavaScript to Write Gext-...
Using Clojure, NoSQL Databases and Functional-Style JavaScript to Write Gext-...
 
Clojure: The Art of Abstraction
Clojure: The Art of AbstractionClojure: The Art of Abstraction
Clojure: The Art of Abstraction
 
Scalaで萌える関数型プログラミング[1.1.RC1]
Scalaで萌える関数型プログラミング[1.1.RC1]Scalaで萌える関数型プログラミング[1.1.RC1]
Scalaで萌える関数型プログラミング[1.1.RC1]
 
Winning the Erlang Edit•Build•Test Cycle
Winning the Erlang Edit•Build•Test CycleWinning the Erlang Edit•Build•Test Cycle
Winning the Erlang Edit•Build•Test Cycle
 
Erlang - Because s**t Happens by Mahesh Paolini-Subramanya
Erlang - Because s**t Happens by Mahesh Paolini-SubramanyaErlang - Because s**t Happens by Mahesh Paolini-Subramanya
Erlang - Because s**t Happens by Mahesh Paolini-Subramanya
 
What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)
 
Messaging With Erlang And Jabber
Messaging With  Erlang And  JabberMessaging With  Erlang And  Jabber
Messaging With Erlang And Jabber
 
Elixir talk
Elixir talkElixir talk
Elixir talk
 
High Performance Erlang
High  Performance  ErlangHigh  Performance  Erlang
High Performance Erlang
 
20 reasons why we don't need architects (@pavlobaron)
20 reasons why we don't need architects (@pavlobaron)20 reasons why we don't need architects (@pavlobaron)
20 reasons why we don't need architects (@pavlobaron)
 
Clojure class
Clojure classClojure class
Clojure class
 
Clojure made-simple - John Stevenson
Clojure made-simple - John StevensonClojure made-simple - John Stevenson
Clojure made-simple - John Stevenson
 
Clojure values
Clojure valuesClojure values
Clojure values
 
VoltDB and Erlang - Tech planet 2012
VoltDB and Erlang - Tech planet 2012VoltDB and Erlang - Tech planet 2012
VoltDB and Erlang - Tech planet 2012
 
From Perl To Elixir
From Perl To ElixirFrom Perl To Elixir
From Perl To Elixir
 
Clojure: Functional Concurrency for the JVM (presented at Open Source Bridge)
Clojure: Functional Concurrency for the JVM (presented at Open Source Bridge)Clojure: Functional Concurrency for the JVM (presented at Open Source Bridge)
Clojure: Functional Concurrency for the JVM (presented at Open Source Bridge)
 
NDC London 2014: Erlang Patterns Matching Business Needs
NDC London 2014: Erlang Patterns Matching Business NeedsNDC London 2014: Erlang Patterns Matching Business Needs
NDC London 2014: Erlang Patterns Matching Business Needs
 
Elixir for aspiring Erlang developers
Elixir for aspiring Erlang developersElixir for aspiring Erlang developers
Elixir for aspiring Erlang developers
 
Introduction to Erlang for Python Programmers
Introduction to Erlang for Python ProgrammersIntroduction to Erlang for Python Programmers
Introduction to Erlang for Python Programmers
 

Similar to Clojure Intro

(first '(Clojure.))
(first '(Clojure.))(first '(Clojure.))
(first '(Clojure.))niklal
 
Getting started with Clojure
Getting started with ClojureGetting started with Clojure
Getting started with ClojureJohn Stevenson
 
Exploring Clojurescript
Exploring ClojurescriptExploring Clojurescript
Exploring ClojurescriptLuke Donnet
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojureAbbas Raza
 
Clojure for Java developers - Stockholm
Clojure for Java developers - StockholmClojure for Java developers - Stockholm
Clojure for Java developers - StockholmJan Kronquist
 
(map Clojure everyday-tasks)
(map Clojure everyday-tasks)(map Clojure everyday-tasks)
(map Clojure everyday-tasks)Jacek Laskowski
 
Clojure 1.1 And Beyond
Clojure 1.1 And BeyondClojure 1.1 And Beyond
Clojure 1.1 And BeyondMike Fogus
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with ClojureDmitry Buzdin
 
From Java to Parellel Clojure - Clojure South 2019
From Java to Parellel Clojure - Clojure South 2019From Java to Parellel Clojure - Clojure South 2019
From Java to Parellel Clojure - Clojure South 2019Leonardo Borges
 
ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015Michiel Borkent
 
Clojure made simple - Lightning talk
Clojure made simple - Lightning talkClojure made simple - Lightning talk
Clojure made simple - Lightning talkJohn Stevenson
 
ClojureScript for the web
ClojureScript for the webClojureScript for the web
ClojureScript for the webMichiel Borkent
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingMuthu Vinayagam
 
The Curious Clojurist - Neal Ford (Thoughtworks)
The Curious Clojurist - Neal Ford (Thoughtworks)The Curious Clojurist - Neal Ford (Thoughtworks)
The Curious Clojurist - Neal Ford (Thoughtworks)jaxLondonConference
 

Similar to Clojure Intro (20)

Pune Clojure Course Outline
Pune Clojure Course OutlinePune Clojure Course Outline
Pune Clojure Course Outline
 
(first '(Clojure.))
(first '(Clojure.))(first '(Clojure.))
(first '(Clojure.))
 
Getting started with Clojure
Getting started with ClojureGetting started with Clojure
Getting started with Clojure
 
Exploring Clojurescript
Exploring ClojurescriptExploring Clojurescript
Exploring Clojurescript
 
Full Stack Clojure
Full Stack ClojureFull Stack Clojure
Full Stack Clojure
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojure
 
Hw09 Hadoop + Clojure
Hw09   Hadoop + ClojureHw09   Hadoop + Clojure
Hw09 Hadoop + Clojure
 
Hadoop + Clojure
Hadoop + ClojureHadoop + Clojure
Hadoop + Clojure
 
Clojure for Java developers - Stockholm
Clojure for Java developers - StockholmClojure for Java developers - Stockholm
Clojure for Java developers - Stockholm
 
(map Clojure everyday-tasks)
(map Clojure everyday-tasks)(map Clojure everyday-tasks)
(map Clojure everyday-tasks)
 
Clojure 1.1 And Beyond
Clojure 1.1 And BeyondClojure 1.1 And Beyond
Clojure 1.1 And Beyond
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
 
From Java to Parellel Clojure - Clojure South 2019
From Java to Parellel Clojure - Clojure South 2019From Java to Parellel Clojure - Clojure South 2019
From Java to Parellel Clojure - Clojure South 2019
 
ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015
 
Clojure made simple - Lightning talk
Clojure made simple - Lightning talkClojure made simple - Lightning talk
Clojure made simple - Lightning talk
 
ClojureScript for the web
ClojureScript for the webClojureScript for the web
ClojureScript for the web
 
Clojure入門
Clojure入門Clojure入門
Clojure入門
 
Clojure And Swing
Clojure And SwingClojure And Swing
Clojure And Swing
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python Programming
 
The Curious Clojurist - Neal Ford (Thoughtworks)
The Curious Clojurist - Neal Ford (Thoughtworks)The Curious Clojurist - Neal Ford (Thoughtworks)
The Curious Clojurist - Neal Ford (Thoughtworks)
 

Recently uploaded

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
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
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
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
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 

Recently uploaded (20)

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
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
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
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
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 

Clojure Intro

  • 1. Clojure The LISP that makes the JVM dynamic http://clojure.org
  • 2. What is Clojure? Clojure is a dynamic, LISP-like programming language that runs on the JVM.
  • 3. Clojure History Started in 2007 Original author (and BDFL) Rich Hickey Currently version 1.1 (1.2 expected within the next month) Available under Eclipse Public License “The Eclipse Public License is designed to be a business-friendly free software license and features weaker copyleft provisions than contemporary licenses such as the GNU General Public License (GPL).”
  • 4. Defining features LISP syntax Macros Immutability Functional programming Concurrency Java interoperability REPL-based
  • 5. Syntax numbers - 1234 ratios - 22/7 strings - “foo”, “bar” characters - a b c symbols - foo, bar keywords - :foo, :bar boolean - true/false null - nil
  • 6. Syntax Lists - (1 2 3 4) Vectors - [1 2 3 4] Maps - {:a 1 :b 2 :c 3} or {:a 1, :b 2, :c 3} Sets - #{foo bar baz}
  • 7. Syntax That’s it. There is no other syntax[1]. Data structures are code. Homoiconic All data literals stand for themselves except: Lists Symbols [1]: Technically, there are other special symbols for shortcutting syntax provided for the developer, but no other necessary syntax
  • 8. Lists (+ 1 1) ; => 2 (+ 1 2 3) ; => 6 (+ 1 2 (+ 3 4)) ; => 10 (class “foo”) ; => java.lang.String (first [:a :b :c]) ; => :a (rest ‘(“foo” “bar” “baz”)) ; => (“bar” “baz”) The ‘ tells the reader not to evaluate the first element as a function ‘(1 2 3) is shorthand for (quote (1 2 3))
  • 9. Hello World (ns helloworld) (defn hello “An example function” [string] (println “Hello” string)) (hello “World”) ; => “Hello World”
  • 11. Operating on Collections Collections are the main datastructure in Clojure A collection can be a list, a vector, a map or a set Some example collection functions: first, second, last, count, reverse, concat, conj, contains?, map, reduce, apply, filter, some, remove, every?, nth, into, doall, repeat, repeatedly, range,
  • 12. Laziness Sequences can be lazy, which allows them to be infinite: (cycle [1 2 3]) ; => (1 2 3 1 2 3 1 2 3 ...) (repeat :a) ; => (:a :a :a :a :a :a :a ...) When using lazy sequences, processing does not occur until the sequence is realized. (class (repeat :a)) ; => clojure.lang.LazySeq Realize (part of) a lazy sequence: (take 5 (repeat 42)) ; => (42 42 42 42 42)
  • 13. More laziness Can do neat things with infinite sequences: (filter even? (iterate inc 1)) ; => (2 4 6 8...) Lazily read lines from a file: (read-lines “/tmp/foo.txt”) Lines aren’t read until they are used in the returned list
  • 14. Java interop (.toLowerCase “EMC”) ; => “emc” (<method> <object> [<arg1> <arg2> ...]) (StringBuffer. “foo” ) ; => #<StringBuffer foo> (Integer/parseInt “5”) ; => 5 (.contains “My spoon is too big.” “spoon“) ; => true (javax.swing.JOptionPane/showMessageDialog nil "Sup!") “Clojure is a better Java than Java”
  • 15. Java datastructures Anything that’s a Collection (or array) in Java can easily be treated (or converted) as a Clojure collection (first (.split “This is a sentence” “ “)) ; => “This” (import ‘java.util.ArrayList) (def foo (ArrayList. [1 2 3])) ; => #<ArrayList [1, 2, 3]> (seq foo) ; => (1 2 3) (vec foo) ; => [1 2 3]
  • 16. More Java-interop Type hints (defn #^String uppercase [#^String s] (.toUpperCase s)) Proxying Java classes (doto (javax.swing.JFrame.) (addKeyListener (proxy [java.awt.event.KeyListener] [] (keyPressed [e] (println (.getKeyChar e) " key pressed")) (keyReleased [e] (println (.getKeyChar e) " key released")) (keyTyped [e] (println (.getKeyChar e) " key typed")))) (setVisible true)) Lots more gen-class, annotations, primitives, definterface, defprotocol
  • 17. Immutability (defn add-age [person] (assoc person :age 10)) (def timmy {:name “Timmy”}) (print timmy) ; => {:name “Timmy”} (add-age timmy) ; => {:name “Timmy”, :age 10} ; timmy never changes (print timmy) ; => {:name “Timmy”} Under the hood: 32-way trie trees. O(log32n)
  • 18. Functional Programming Functions are intended to be side-effect free They take values and return values Same argument, same result No notion of time Easy to add concurrency to the mix sanely Testing is easier because there is no state Having no side effects isn’t always possible (io, mutable java objects)
  • 19. Concurrency Locks are bad Clojure’s defining concurrency feature is STM Gives you ACI out of ACID Atomicity Consistency Isolation Immutability is what makes this possible 4 methods for changing data concurrently:
  • 20. Dead-simple Concurrency (def accounts [{:name “Bob” :balance 1000} {:name “Sue” :balance 2000} {:name “Joe” :balance 3000} {:name “Ann” :balance 4000}]) (defn add-interest [acct] (let [bal (:balance acct)] (Thread/sleep 1500) (assoc acct :balance (* bal 1.25)))) (time (doall (map add-interest accounts))) ; => "Elapsed time: 6001.023 msecs" (time (doall (pmap add-interest accounts))) ; => "Elapsed time: 1519.73 msecs"
  • 21. More simple concurrency All functions defined in Clojure implement Runnable: (defn dowork [] (println “doing work”)) (.start (Thread. dowork)) ; => “doing work” Access to all of Java’s concurrency stuff (ThreadPoolExecutor, etc) with Java interop
  • 22. Concurrency (Refs) (def x (ref 100)) @x ; => 100 (def amounts [5 10 1 9 2]) (defn dec-x [n] (dosync (alter x #(- % n)))) (pmap dec-x amounts) @x ; => 73
  • 23. Concurrency (Atoms) (def x (atom 100)) @x ; => 100 (def amounts [5 10 1 9 2]) (defn dec-x [n] (swap! x #(- % n))) (pmap dec-x amounts) @x ; => 73
  • 24. Concurrency (Agents) (def x (agent 0)) @x ; => 0 (defn increment [c n] (+ c n)) (send x increment 5) ; @x -> 5 (send x increment 10) ; @x -> 15
  • 25. Macros (defmacro def-name-filter [n r] (let [docstring (str "Given a list of people maps, filter " n " in a list.")] `(defn ~n ~docstring [elements#] (filter (fn [t#] (re-find ~r (:name t#))) elements#)))) (def-name-filter foo-filter #"^foo") (def people [{:name "pat"} {:name "foobar"} {:name "foo"} {:name "bar"}]) (doc foo-filter) ; => Given a list of people maps, filter foo-filter in a list. (foo-filter people) ; => ({:name "foobar"} {:name "foo"})
  • 27. List de-structuring ; Destructure in defn (defn print-items [item & rest] ; Destructure in let (println "item:" item) (println "rest:" rest) (def my-list ‘([:a 1] [:b 2] [:c 3])) (if-not (nil? rest) (apply print-items rest))) (defn print-pair [pair] (print-items 1 2 3 4 5) (let [[key val] pair] (println "key:" key "val:" val))) item: 1 rest: (2 3 4 5) (map print-pair my-list) item: 2 rest: (3 4 5) key: :a val: 1 item: 3 key: :b val: 2 rest: (4 5) key: :c val: 3 item: 4 rest: (5) item: 5 rest: nil
  • 28. Much More Polymorphism/multimethods Pre and Post conditions for functions Futures, Promises Watchers Bindings/Transients Metadata features Compilation Clojure-in-Clojure So much more!

Editor's Notes