SlideShare una empresa de Scribd logo
1 de 85
Clojure
JVM språket som er
“multi-core ready”

    Alf Kristian Støyle
Concurrency
 dreier som om å gjøre flere urelaterte
 oppgaver omtrent samtidig


Parallelism
 dreier seg om å dele en oppgave i mange
 deloppgaver som kan gjøres concurrent
Clojure
Accidental complexity
Alf Kristian Støyle

7 års erfaring som programmerer
Clojure
• General purpose
• Lisp (List Processing)
• Funksjonelt
• Kompilert
• Dynamisk typet
• Tett knyttet til JVM’en
1
; => 1

"Hello World"
; => "Hello World"
1
; => 1

"Hello World"
; => "Hello World"


(class 1)
; => java.lang.Integer

(class "Hello world")
; => java.lang.String
(println "Hello " "world")
; “Hello world”
; => nil
Funksjonsnavn


(println "Hello " "world")
; “Hello world”
; => nil
Funksjonsnavn   Parametere


(println "Hello " "world")
; “Hello world”
; => nil
Funksjonsnavn   Parametere


(println "Hello " "world")
                 ,
; “Hello world”
; => nil
Prefiks notasjon
 Funksjonsnavn   Parametere


(println "Hello " "world")
                 ,
; “Hello world”
; => nil
Methods.callStatic();
methods.callInstance();

callStatic();
callInstance();

this.callInstance();
methods.callStatic();

Methods.this.callInstance();
Methods.this.callStatic();
(println "Hello world")
; Hello world
; => nil
(println "Hello world")
; Hello world
; => nil

(sum 1 2 3)
; => 6
(println "Hello world")
; Hello world
; => nil

(sum 1 2 3)
; => 6

(+ 1 2)
; => 3
(println "Hello world")
; Hello world
; => nil

(sum 1 2 3)
; => 6

(+ 1 2)
; => 3

(+ 1 2 3)
; => 6
(println "Hello world")
; Hello world
; => nil

(sum 1 2 3)
; => 6

(+ 1 2)
; => 3

(+ 1 2 3)
; => 6

(+)
; => 0
1 + 2 * 3
1 + 2 * 3

1 + (2 * 3)
1 + 2 * 3

1 + (2 * 3)

(+ 1 (* 2 3))
Funksjoner

(fn [& nums]
  (apply + nums))
Funksjoner
(def sum
  (fn [& nums]
    (apply + nums)))
Funksjoner
(def sum

              )
Funksjoner
(def sum
  (fn [& nums]
    (apply + nums)))
Funksjoner
(def sum
  (fn [& nums]
    (apply + nums)))


(defn sum
  [& nums] (apply + nums))
Lister!
'(3 2 1)
; => (3 2 1)

(+ 3 2 1)
; => 6
Lister!
'(3 2 1)
; => (3 2 1)

(+ 3 2 1)
; => 6


(defn iterate
  [f x] (cons x (lazy-seq (iterate f (f x)))))
Makroer
Makroer



(postfix (2 1 +))
; => 3
Makroer

(defmacro postfix [form]
  (reverse form))

(postfix (2 1 +))
; => 3
Makroer



(infix (1 + 2))
; => 3
Makroer

(defmacro infix [form]
  `(~(second form) ~(first form) ~@(nnext form)))


(infix (1 + 2))
; => 3
Makroer

(defmacro infix [form]
  `(~(second form) ~(first form) ~@(nnext form)))


(infix (1 + 2))
; => 3

(macroexpand '(infix (1 + 2)))
; => (+ 1 2)
(+ Clojure Alf Kristian)
(+ Clojure Alf Kristian)
           oooooo             oooooo
         oooooooooo         oooooooooo
       oooooooooooooo     oooooooooooooo
     ooooooooooooooooo ooooooooooooooooo
    ooooooooooooooooooooooooooooooooooooo
   ooooooooooooooooooooooooooooooooooooooo
   ooooooooooooooooooooooooooooooooooooooo
   ooooooooooooooooooooooooooooooooooooooo
    ooooooooooooooooooooooooooooooooooooo
     ooooooooooooooooooooooooooooooooooo
      ooooooooooooooooooooooooooooooooo
       ooooooooooooooooooooooooooooooo
         ooooooooooooooooooooooooooo
           ooooooooooooooooooooooo
             ooooooooooooooooooo
               ooooooooooooooo
                  ooooooooooo
                    ooooooo
                      ooo
                       o
              Clojure + Alf Kristian
Immutable collections
; List
'(3 2 1)

; Vector
[1 2 3]

; Set
#{1 2 3}

; Map
{1 "one", 2 "two"‚ 3 "three"}
Persistent data
        structures
(def my-list '(3 2 1))
; => (3 2 1)
Persistent data
        structures
(def my-list '(3 2 1))
; => (3 2 1)
Persistent data
        structures
(def my-list '(3 2 1))
; => (3 2 1)

(def my-other-list (cons 4 my-list))
; => (4 3 2 1)
Persistent data
        structures
(def my-list '(3 2 1))
; => (3 2 1)

(def my-other-list (cons 4 my-list))
; => (4 3 2 1)
Immutable records
(defrecord Person [fname lname])
; => user.Person


(Person. "Alf" "Støyle")
; => #:user.Person{:fname "Alf", :lname "Støyle"}
Immutable objects are always thread-safe.
Ren funksjonell
programmering


  f(x) = x + 2
Clojures filosofi
Verdier
 Immutable/uforanderlig, primitiv eller sammensatt
Clojures filosofi
Verdier
 Immutable/uforanderlig, primitiv eller sammensatt
Identitet
  En observert entitet som vi assosierer med en serie
  relaterte tilstander (verdier) over tid
Clojures filosofi
Verdier
 Immutable/uforanderlig, primitiv eller sammensatt
Identitet
  En observert entitet som vi assosierer med en serie
  relaterte tilstander (verdier) over tid
Tilstand
 Verdien til en identitet i på et gitt tidspunkt
Clojures filosofi
Verdier
 Immutable/uforanderlig, primitiv eller sammensatt
Identitet
  En observert entitet som vi assosierer med en serie
  relaterte tilstander (verdier) over tid
Tilstand
 Verdien til en identitet i på et gitt tidspunkt
Tid
 Relativ før/etter endring av identitet
Perception
Perception
Perception
Perception
Perception
Perception
Fra Rich Hickeys
“Are we there yet”
Reference types
• Atoms - synkron og ukoordinert oppdatering
• Agents - asynkron og ukoordinert oppdatering
• Refs - synkron og koordinert oppdatering
Reference types
• Atoms - synkron og ukoordinert oppdatering
• Agents - asynkron og ukoordinert oppdatering
• Refs - synkron og koordinert oppdatering
• Synkron perception
Atoms
(def an-atom (atom 0))
; => #'user/an-atom
Atoms
(def an-atom (atom 0))
; => #'user/an-atom

(swap! an-atom inc)
; => 1
Atoms
(def an-atom (atom 0))
; => #'user/an-atom

(swap! an-atom inc)
; => 1

(deref an-atom)
; => 1
Atoms
(def an-atom (atom 0))
; => #'user/an-atom

(swap! an-atom inc)
; => 1

(deref an-atom)
; => 1

@an-atom
; => 1
Agents
(def an-agent (agent 0))
; => #'user/an-atom
Agents
(def an-agent (agent 0))
; => #'user/an-atom

(send an-agent inc)
; => #<Agent@9b7c63f: 0>
Agents
(def an-agent (agent 0))
; => #'user/an-atom

(send an-agent inc)
; => #<Agent@9b7c63f: 0>

@an-agent
; => ???
Agents
(def an-agent (agent 0))
; => #'user/an-atom

(send an-agent inc)
; => #<Agent@9b7c63f: 0>

@an-agent              (await an-agent)
; => ???               ; => nil

                       @an-agent
                       ; => 1
Refs
(def ref-1 (ref 0))
; => #'user/ref-1

(def ref-2 (ref 0))
; => #'user/ref-2
Refs
(def ref-1 (ref 0))
; => #'user/ref-1

(def ref-2 (ref 0))
; => #'user/ref-2

(alter ref-1 inc)
Refs
(def ref-1 (ref 0))
; => #'user/ref-1

(def ref-2 (ref 0))
; => #'user/ref-2

(alter ref-1 inc)
; java.lang.IllegalStateException:
; No transaction running
Refs
(def ref-1 (ref 0))
; => #'user/ref-1

(def ref-2 (ref 0))
; => #'user/ref-2

(alter ref-1 inc)
; java.lang.IllegalStateException:
; No transaction running

(dosync (alter ref-1 inc))
; => 1
Refs
(dosync
  (alter ref-1 inc)
  (alter ref-2 dec))
; => -1
Refs
(dosync
  (alter ref-1 inc)
  (alter ref-2 dec))
; => -1


@ref-1
; => 2

@ref-2
; => -1
Refs
(dosync
  (alter ref-1 inc)
  (dosync
    (alter ref-2 dec)))
; => -2

@ref-1
; => 3

@ref-2
; => -2
Software transactional
      memory
Software transactional
      memory
Software transactional
      memory
   Transactional memory is to
  shared-memory concurrency
               as
     garbage collection is to
      memory management
Software transactional
      memory
Software transactional
      memory
    Atomic
    Consistent
    Isolated
    Durable
(dosync
; ...
  (commute ref-3 inc)
  (alter ref-2 + @ref-1))
; => 1
Oppsummering
• Skiller tilstand og identitet
• Ingen mutable tilstand
• Ingenting blokkerer
• Clojure hjelper langt på vei med concurrency
Oppsummering
• Skiller tilstand og identitet
• Ingen mutable tilstand
• Ingenting blokkerer
• Clojure hjelper langt på vei med concurrency
• Mindre “accidental complexity”
clojure.org

       slideshare.net/stoyle
          aks@knowit.no

meetup.com/Oslo-Socially-Functional

              Q &A

Más contenido relacionado

Destacado

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Destacado (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Clojure - JVM språket som er "multi-core ready"

Notas del editor

  1. \n
  2. \n
  3. Korrekthet og ytelse\nPresisere at det vi skal snakke om er Concurrency.\n
  4. \n
  5. Startet mars i 2006.\nF&amp;#xF8;rste versjon 4 May 2009\nFikk veldig mye oppmerksomhet.\n
  6. Alle problemer har en inherent complexity. Dette er det som uansett m&amp;#xE5; til for &amp;#xE5; l&amp;#xF8;se problemet.\n
  7. V&amp;#xE6;rt programmerer i 7 &amp;#xE5;r, har hatt alle mulige roller som konsulent. De siste 4 &amp;#xE5;ra...\n
  8. F&amp;#xF8;rst lisp i 1958 John McCarthy\nIkke objektorientert\nYtelse nesten som Java\n
  9. \n
  10. Cambridge polish notation\n
  11. Cambridge polish notation\n
  12. Cambridge polish notation\n
  13. Cambridge polish notation\n
  14. Cambridge polish notation\n
  15. Cambridge polish notation\n
  16. Cambridge polish notation\n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. Makroer brukes bl.a. for kontrollstrukturer.\nClojure er et lite spr&amp;#xE5;k - For comprehensions\n\nDersom du ikke skj&amp;#xF8;nte dette...\n
  32. Makroer brukes bl.a. for kontrollstrukturer.\nClojure er et lite spr&amp;#xE5;k - For comprehensions\n\nDersom du ikke skj&amp;#xF8;nte dette...\n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. Perception: You don&apos;t rub your brain on a value, there is a disconnect. Perception is massively parallel and requires no coordination. Uncoordinated.\n\n Med l&amp;#xE5;semodell, vil programmet g&amp;#xE5; tregere jo flere som oppfatter/perceives meg?\n
  45. \n
  46. \n
  47. \n
  48. Epokisk tidsmodell\n
  49. \n
  50. Gjenta egenskaper! N&amp;#xE5;r brukes dette!\n
  51. Gjenta egenskaper! N&amp;#xE5;r brukes dette!\n
  52. Gjenta egenskaper! N&amp;#xE5;r brukes dette!\n
  53. Gjenta egenskaper. N&amp;#xE5;r brukes dette!\nActors vs agents:\nAc -&gt; Sende tilstand til oppf&amp;#xF8;rsel\nAg -&gt; Sende oppf&amp;#xF8;rsel til tilstand\nSynkron perception\n
  54. Gjenta egenskaper. N&amp;#xE5;r brukes dette!\nActors vs agents:\nAc -&gt; Sende tilstand til oppf&amp;#xF8;rsel\nAg -&gt; Sende oppf&amp;#xF8;rsel til tilstand\nSynkron perception\n
  55. Gjenta egenskaper. N&amp;#xE5;r brukes dette!\nActors vs agents:\nAc -&gt; Sende tilstand til oppf&amp;#xF8;rsel\nAg -&gt; Sende oppf&amp;#xF8;rsel til tilstand\nSynkron perception\n
  56. Gjenta egenskaper, n&amp;#xE5;r brukes dette\n
  57. Gjenta egenskaper, n&amp;#xE5;r brukes dette\n
  58. Gjenta egenskaper, n&amp;#xE5;r brukes dette\n
  59. \n
  60. \n
  61. STM var en av hoved&amp;#xE5;rsakene til at Clojure ble s&amp;#xE5;pass fort kjent.\nAnalogi\n
  62. Snakke om .Net prosjektet\n
  63. Atomic - rollback ved transactions ellers vil det committe\nConsistent - constraints i database, set-validator\nIsolated - Snapshot MVCC\nMen... kan gi rollback.\nEget eksempel 10 000 retries.\nEksempel tungt oppdaterer en ref som f&amp;#xE5; konkurrerer om. S&amp;#xE5; gj&amp;#xF8;r den en lett jobb som oppdaterer en ref som mange konkurrerer om.\n
  64. Atomic - rollback ved transactions ellers vil det committe\nConsistent - constraints i database, set-validator\nIsolated - Snapshot MVCC\nMen... kan gi rollback.\nEget eksempel 10 000 retries.\nEksempel tungt oppdaterer en ref som f&amp;#xE5; konkurrerer om. S&amp;#xE5; gj&amp;#xF8;r den en lett jobb som oppdaterer en ref som mange konkurrerer om.\n
  65. Atomic - rollback ved transactions ellers vil det committe\nConsistent - constraints i database, set-validator\nIsolated - Snapshot MVCC\nMen... kan gi rollback.\nEget eksempel 10 000 retries.\nEksempel tungt oppdaterer en ref som f&amp;#xE5; konkurrerer om. S&amp;#xE5; gj&amp;#xF8;r den en lett jobb som oppdaterer en ref som mange konkurrerer om.\n
  66. \n
  67. N&amp;#xE5;r man tenker seg om -&gt; fornuftig m&amp;#xE5;te &amp;#xE5; modellere tilstand p&amp;#xE5;. Bonusen er jo da at dette fungerer veldig bra i en concurrent setting.\n
  68. N&amp;#xE5;r man tenker seg om -&gt; fornuftig m&amp;#xE5;te &amp;#xE5; modellere tilstand p&amp;#xE5;. Bonusen er jo da at dette fungerer veldig bra i en concurrent setting.\n
  69. \n