SlideShare a Scribd company logo
Agenda

-Overview
-Clojure features
-Concurrent programming
-Vars
-Refs and STM approach
-Atoms
-Agents
Overview

Rich Hickey
https://twitter.com/#!/richhickey




-about 2.5 years working on Clojure
-first public release at 2007
-interview: http://www.simple-talk.com/opinion/geek-of-the-
week/rich-hickey-geek-of-the-week/
What is Clojure?

Clojure is a dynamic, LISP-like programming
       language that runs on the JVM
How does it look? Exactly! Like LISP
A form is a list where the first symbol in the
list has to be a special word that the
compiler can understand - usually the name
of a function
Clojure Features

● Functional
   ○ Immutable, persistent data structures
   ○ Functions are objects
● Lisp
● Hosted on JVM
   ○ the same memory model
   ○ garbage collector
   ○ etc
● Supporting Concurrency
● Open source
Clojure Features

● Dynamic development
   ○ REPL (read-eval-print-loop), on-the-fly
     compilation to JVM bytecode
● Full set of immutable, extensible, read-only
  data structures:
   ○ lists
   ○ maps
   ○ sets
   ○ vectors
● Macros
Clojure Features
                    Java interop



● Call java-methods, access fields
● Functions implement java.util.concurrent.
  Callable           (defn func (smthng))
● Proxy interfaces/classes
● Primitive types - int, long, byte, Char, String etc
● Clojure data structures implement Collection, Iterable,
  Comparable etc
Concurrent programming

● Things are happening at the same time
● Number of CPU is growing fast
Concurrent programming problems

Visibility problem
 ● Multiple threads use shared data
 ● 2 or more threads are trying to
   change the same data at the
   same time
Concurrent programming problems

Basic solution is
 ● locks
 ● synchronized access
 ● only one thread can have
   lock, others blocks
 ● But it requires additional efforts
     ○ thread-coordination
     ○ avoiding deadlocks
Concurrent programming problems

Access problem
 ● several threads are trying to
   access and change the same
   shared data at the same time
 ● write/read at the same time
 ● readers block readers
Clojure way

● There is no any possibility to
  change data by direct access. All
  data structures are immutable,
  remember?
● Only read, if we add element -
  new data structure will be
  created.
● No locks
● No access problems
But what if
we really want change
   data in Clojure?
     This opportunity is also
  provided by the language! All
     hard work done for us.
Reference types

● Vars
● Refs
● Atoms
● Agents
Vars

 ● Reference to mutable storage location
 ● Dynamically rebound on a per-thread basis
 ● Functions are vars, so they can be dynamically rebound too
 ● Ensure safe use via thread-isolation

(def x 5)              // root-binding
(x)                     // 5
....
(binding [x 20]
   (+ x 1))            // 21
....
(x)                     // 5 again, not changed
Changing Vars

(set! var-name new-value)
   ● cannot change root-binding
   ● works only in thread-local context (in "binding")
(def x 5)                  // root-binding
(set! x 7)                // exception will be thrown
....
(binding [x 20]
   (println (+ x 1))                // 21
   (set! x 10)
   (+ x 5))                         // 15
....
(x)                         // 5 again, not changed
Refs

● Based on Software Transactional Memory (STM)
● Change the value by applying a function to old value
● Refs can be changed within a transaction only
● Transactions will be retried automatically if conflict happens
● To make changes code must be surrounded with (dosync
  ...)
● All changes are atomic
Refs


       (def r (ref '(1 2 3)))
       (deref r)                   // list (1 2 3)

       (dosync                     // transaction begins
         ....
         (alter r                  // change ref!
              (fn [_] '(10 9 8))
         ...
       )                            // transaction ends

       (deref r)                   // list (10 9 8)
Refs lifecycle


    (def r (ref '(1 2 3)))
    (deref r)

    (dosync
      ....
                                if any other transaction
      (alter r
                                commutes - this
           (fn [_] '(10 9 8))
                                transaction is repeated
      ...
    )
                                Otherwise, that's ok and
    (deref r)                   programs continues
                                execution
Atoms

● Way to manage shared state synchronously
● Change the value by applying a function to old value
● This is done in an atomic manner by function swap!
● Internally, swap! reads the current value, appliesthe
  function to it, and attemprs to call compare-and-set! for this
  atom
Atoms best practices by Rich Hickey

(defn memoize [f]
 (let [mem (atom {})]
   (fn [& args]
     (if-let [e (find @mem args)]
       (val e)
       (let [ret (apply f args)]
         (swap! mem assoc args ret)
         ret)))))
Atoms best practices by Rich Hickey

(defn fib [n]
 (if (<= n 1)
   n
   (+ (fib (dec n)) (fib (- n 2)))))

(time (fib 35))
user=> "Elapsed time: 941.445 msecs"

(def fib (memoize fib))

(time (fib 35))

user=> "Elapsed time: 0.044 msecs"
Agents

● Asynchronous changing. They are not blocking main
  execution, return immediately
● Change the value by applying a function to old value
● Agent action dispatches take the
  form                      (send agent fn args*)
● If other actions try to change data - they will be added to
  the queue
Agents


(def a (agent 5))      // create agent
(deref a)               // 5

(send a
  (fn [old-val]
     (+ old-val 5)))
(deref a)              // may be 5, or may be 10
(Thread/sleep 1000)
(deref a)              // 10
Useful links

 ● https://groups.google.com/forum/#!forum/clojure
 ● http://clojure.org
 ● http://alexott.net/ru/clojure/index.html
 ● http://www.lisperati.com/clojure-spels/casting.html
 ● http://www.pluralsight-training.
   net/microsoft/courses/TableOfContents?
   courseName=clojure-concurrency-tutorial
Q&A

More Related Content

What's hot

Clojure+ClojureScript Webapps
Clojure+ClojureScript WebappsClojure+ClojureScript Webapps
Clojure+ClojureScript Webapps
Falko Riemenschneider
 
Modxpo 2015 - Custom Manager Page in MODX Revolution
Modxpo 2015 - Custom Manager Page in MODX RevolutionModxpo 2015 - Custom Manager Page in MODX Revolution
Modxpo 2015 - Custom Manager Page in MODX Revolution
rtripault
 
Glusterd_thread_synchronization_using_urcu_lca2016
Glusterd_thread_synchronization_using_urcu_lca2016Glusterd_thread_synchronization_using_urcu_lca2016
Glusterd_thread_synchronization_using_urcu_lca2016
Atin Mukherjee
 
Oslo.versioned objects - Deep Dive
Oslo.versioned objects - Deep DiveOslo.versioned objects - Deep Dive
Oslo.versioned objects - Deep Dive
davanum
 
Concurrency in Programming Languages
Concurrency in Programming LanguagesConcurrency in Programming Languages
Concurrency in Programming Languages
Yudong Li
 
Actor Concurrency
Actor ConcurrencyActor Concurrency
Actor Concurrency
Alex Miller
 
Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to Redis
Saeid Zebardast
 
ConFess Vienna 2015 - Metaprogramming with Groovy
ConFess Vienna 2015 - Metaprogramming with GroovyConFess Vienna 2015 - Metaprogramming with Groovy
ConFess Vienna 2015 - Metaprogramming with Groovy
Iván López Martín
 
Ast transformation
Ast transformationAst transformation
Ast transformation
Gagan Agrawal
 
'Getting' Clojure - '(parentheses are just hugs for your code)
'Getting' Clojure - '(parentheses are just hugs for your code)'Getting' Clojure - '(parentheses are just hugs for your code)
'Getting' Clojure - '(parentheses are just hugs for your code)
Gary Trakhman
 
CoffeeScript - JavaScript in a simple way
CoffeeScript - JavaScript in a simple wayCoffeeScript - JavaScript in a simple way
CoffeeScript - JavaScript in a simple way
Lim Chanmann
 
Polymorphism in c++
Polymorphism in c++Polymorphism in c++
Polymorphism in c++
Dheenadayalan18
 
Tomáš Čorej: Configuration management & CFEngine3
Tomáš Čorej: Configuration management & CFEngine3Tomáš Čorej: Configuration management & CFEngine3
Tomáš Čorej: Configuration management & CFEngine3
Jano Suchal
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojure
Abbas Raza
 
Swift Tutorial 2
Swift Tutorial  2Swift Tutorial  2
Swift Tutorial 2
Jintin Lin
 
Using VI Java from Scala
Using VI Java from ScalaUsing VI Java from Scala
Using VI Java from Scala
dcbriccetti
 
DConf 2016: Keynote by Walter Bright
DConf 2016: Keynote by Walter Bright DConf 2016: Keynote by Walter Bright
DConf 2016: Keynote by Walter Bright
Andrei Alexandrescu
 
Java8 and Functional Programming
Java8 and Functional ProgrammingJava8 and Functional Programming
Java8 and Functional Programming
Yiguang Hu
 
Transactional Memory for Smalltalk
Transactional Memory for SmalltalkTransactional Memory for Smalltalk
Transactional Memory for Smalltalk
Lukas Renggli
 
CODEsign 2015
CODEsign 2015CODEsign 2015
CODEsign 2015
Max Kleiner
 

What's hot (20)

Clojure+ClojureScript Webapps
Clojure+ClojureScript WebappsClojure+ClojureScript Webapps
Clojure+ClojureScript Webapps
 
Modxpo 2015 - Custom Manager Page in MODX Revolution
Modxpo 2015 - Custom Manager Page in MODX RevolutionModxpo 2015 - Custom Manager Page in MODX Revolution
Modxpo 2015 - Custom Manager Page in MODX Revolution
 
Glusterd_thread_synchronization_using_urcu_lca2016
Glusterd_thread_synchronization_using_urcu_lca2016Glusterd_thread_synchronization_using_urcu_lca2016
Glusterd_thread_synchronization_using_urcu_lca2016
 
Oslo.versioned objects - Deep Dive
Oslo.versioned objects - Deep DiveOslo.versioned objects - Deep Dive
Oslo.versioned objects - Deep Dive
 
Concurrency in Programming Languages
Concurrency in Programming LanguagesConcurrency in Programming Languages
Concurrency in Programming Languages
 
Actor Concurrency
Actor ConcurrencyActor Concurrency
Actor Concurrency
 
Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to Redis
 
ConFess Vienna 2015 - Metaprogramming with Groovy
ConFess Vienna 2015 - Metaprogramming with GroovyConFess Vienna 2015 - Metaprogramming with Groovy
ConFess Vienna 2015 - Metaprogramming with Groovy
 
Ast transformation
Ast transformationAst transformation
Ast transformation
 
'Getting' Clojure - '(parentheses are just hugs for your code)
'Getting' Clojure - '(parentheses are just hugs for your code)'Getting' Clojure - '(parentheses are just hugs for your code)
'Getting' Clojure - '(parentheses are just hugs for your code)
 
CoffeeScript - JavaScript in a simple way
CoffeeScript - JavaScript in a simple wayCoffeeScript - JavaScript in a simple way
CoffeeScript - JavaScript in a simple way
 
Polymorphism in c++
Polymorphism in c++Polymorphism in c++
Polymorphism in c++
 
Tomáš Čorej: Configuration management & CFEngine3
Tomáš Čorej: Configuration management & CFEngine3Tomáš Čorej: Configuration management & CFEngine3
Tomáš Čorej: Configuration management & CFEngine3
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojure
 
Swift Tutorial 2
Swift Tutorial  2Swift Tutorial  2
Swift Tutorial 2
 
Using VI Java from Scala
Using VI Java from ScalaUsing VI Java from Scala
Using VI Java from Scala
 
DConf 2016: Keynote by Walter Bright
DConf 2016: Keynote by Walter Bright DConf 2016: Keynote by Walter Bright
DConf 2016: Keynote by Walter Bright
 
Java8 and Functional Programming
Java8 and Functional ProgrammingJava8 and Functional Programming
Java8 and Functional Programming
 
Transactional Memory for Smalltalk
Transactional Memory for SmalltalkTransactional Memory for Smalltalk
Transactional Memory for Smalltalk
 
CODEsign 2015
CODEsign 2015CODEsign 2015
CODEsign 2015
 

Viewers also liked

Design Tools
Design ToolsDesign Tools
Evaluation of a CIS
Evaluation of a CISEvaluation of a CIS
Evaluation of a CIS
kylerossakers
 
Stm in-clojure
Stm in-clojureStm in-clojure
Stm in-clojure
Tom Van Cutsem
 
Sabrinappt
SabrinapptSabrinappt
Presentation1
Presentation1Presentation1
Presentation1
nicolejamiee2
 

Viewers also liked (6)

Design Tools
Design ToolsDesign Tools
Design Tools
 
Evaluation of a CIS
Evaluation of a CISEvaluation of a CIS
Evaluation of a CIS
 
Graficasmatlab
GraficasmatlabGraficasmatlab
Graficasmatlab
 
Stm in-clojure
Stm in-clojureStm in-clojure
Stm in-clojure
 
Sabrinappt
SabrinapptSabrinappt
Sabrinappt
 
Presentation1
Presentation1Presentation1
Presentation1
 

Similar to Clojure concurrency overview

(map Clojure everyday-tasks)
(map Clojure everyday-tasks)(map Clojure everyday-tasks)
(map Clojure everyday-tasks)
Jacek Laskowski
 
Exploring Clojurescript
Exploring ClojurescriptExploring Clojurescript
Exploring Clojurescript
Luke Donnet
 
Dragoncraft Architectural Overview
Dragoncraft Architectural OverviewDragoncraft Architectural Overview
Dragoncraft Architectural Overview
jessesanford
 
Clojure intro
Clojure introClojure intro
Clojure intro
Basav Nagur
 
Java Concurrency in Practice
Java Concurrency in PracticeJava Concurrency in Practice
Java Concurrency in Practice
Alina Dolgikh
 
Functional programming in clojure
Functional programming in clojureFunctional programming in clojure
Functional programming in clojure
Juan-Manuel Gimeno
 
Lcna tutorial-2012
Lcna tutorial-2012Lcna tutorial-2012
Lcna tutorial-2012
Gluster.org
 
Lcna 2012-tutorial
Lcna 2012-tutorialLcna 2012-tutorial
Lcna 2012-tutorial
Gluster.org
 
ClojureScript for the web
ClojureScript for the webClojureScript for the web
ClojureScript for the web
Michiel Borkent
 
Let's Talk Locks!
Let's Talk Locks!Let's Talk Locks!
Let's Talk Locks!
C4Media
 
Dart the Better JavaScript
Dart the Better JavaScriptDart the Better JavaScript
Dart the Better JavaScript
Jorg Janke
 
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
Michiel Borkent
 
Getting started with Clojure
Getting started with ClojureGetting started with Clojure
Getting started with Clojure
John Stevenson
 
(3) cpp procedural programming
(3) cpp procedural programming(3) cpp procedural programming
(3) cpp procedural programming
Nico Ludwig
 
Clojure and Modularity
Clojure and ModularityClojure and Modularity
Clojure and Modularity
elliando dias
 
Pune Clojure Course Outline
Pune Clojure Course OutlinePune Clojure Course Outline
Pune Clojure Course Outline
Baishampayan Ghose
 
Clojure made-simple - John Stevenson
Clojure made-simple - John StevensonClojure made-simple - John Stevenson
Clojure made-simple - John Stevenson
JAX London
 
Concurrency Constructs Overview
Concurrency Constructs OverviewConcurrency Constructs Overview
Concurrency Constructs Overview
stasimus
 
Clojure 1.1 And Beyond
Clojure 1.1 And BeyondClojure 1.1 And Beyond
Clojure 1.1 And Beyond
Mike Fogus
 
C language
C languageC language
C language
Robo India
 

Similar to Clojure concurrency overview (20)

(map Clojure everyday-tasks)
(map Clojure everyday-tasks)(map Clojure everyday-tasks)
(map Clojure everyday-tasks)
 
Exploring Clojurescript
Exploring ClojurescriptExploring Clojurescript
Exploring Clojurescript
 
Dragoncraft Architectural Overview
Dragoncraft Architectural OverviewDragoncraft Architectural Overview
Dragoncraft Architectural Overview
 
Clojure intro
Clojure introClojure intro
Clojure intro
 
Java Concurrency in Practice
Java Concurrency in PracticeJava Concurrency in Practice
Java Concurrency in Practice
 
Functional programming in clojure
Functional programming in clojureFunctional programming in clojure
Functional programming in clojure
 
Lcna tutorial-2012
Lcna tutorial-2012Lcna tutorial-2012
Lcna tutorial-2012
 
Lcna 2012-tutorial
Lcna 2012-tutorialLcna 2012-tutorial
Lcna 2012-tutorial
 
ClojureScript for the web
ClojureScript for the webClojureScript for the web
ClojureScript for the web
 
Let's Talk Locks!
Let's Talk Locks!Let's Talk Locks!
Let's Talk Locks!
 
Dart the Better JavaScript
Dart the Better JavaScriptDart the Better JavaScript
Dart the Better JavaScript
 
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
 
Getting started with Clojure
Getting started with ClojureGetting started with Clojure
Getting started with Clojure
 
(3) cpp procedural programming
(3) cpp procedural programming(3) cpp procedural programming
(3) cpp procedural programming
 
Clojure and Modularity
Clojure and ModularityClojure and Modularity
Clojure and Modularity
 
Pune Clojure Course Outline
Pune Clojure Course OutlinePune Clojure Course Outline
Pune Clojure Course Outline
 
Clojure made-simple - John Stevenson
Clojure made-simple - John StevensonClojure made-simple - John Stevenson
Clojure made-simple - John Stevenson
 
Concurrency Constructs Overview
Concurrency Constructs OverviewConcurrency Constructs Overview
Concurrency Constructs Overview
 
Clojure 1.1 And Beyond
Clojure 1.1 And BeyondClojure 1.1 And Beyond
Clojure 1.1 And Beyond
 
C language
C languageC language
C language
 

Recently uploaded

20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
IndexBug
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
DianaGray10
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
kumardaparthi1024
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
Zilliz
 

Recently uploaded (20)

20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
 

Clojure concurrency overview

  • 1.
  • 3. Overview Rich Hickey https://twitter.com/#!/richhickey -about 2.5 years working on Clojure -first public release at 2007 -interview: http://www.simple-talk.com/opinion/geek-of-the- week/rich-hickey-geek-of-the-week/
  • 4. What is Clojure? Clojure is a dynamic, LISP-like programming language that runs on the JVM
  • 5. How does it look? Exactly! Like LISP
  • 6. A form is a list where the first symbol in the list has to be a special word that the compiler can understand - usually the name of a function
  • 7. Clojure Features ● Functional ○ Immutable, persistent data structures ○ Functions are objects ● Lisp ● Hosted on JVM ○ the same memory model ○ garbage collector ○ etc ● Supporting Concurrency ● Open source
  • 8. Clojure Features ● Dynamic development ○ REPL (read-eval-print-loop), on-the-fly compilation to JVM bytecode ● Full set of immutable, extensible, read-only data structures: ○ lists ○ maps ○ sets ○ vectors ● Macros
  • 9. Clojure Features Java interop ● Call java-methods, access fields ● Functions implement java.util.concurrent. Callable (defn func (smthng)) ● Proxy interfaces/classes ● Primitive types - int, long, byte, Char, String etc ● Clojure data structures implement Collection, Iterable, Comparable etc
  • 10. Concurrent programming ● Things are happening at the same time ● Number of CPU is growing fast
  • 11. Concurrent programming problems Visibility problem ● Multiple threads use shared data ● 2 or more threads are trying to change the same data at the same time
  • 12. Concurrent programming problems Basic solution is ● locks ● synchronized access ● only one thread can have lock, others blocks ● But it requires additional efforts ○ thread-coordination ○ avoiding deadlocks
  • 13. Concurrent programming problems Access problem ● several threads are trying to access and change the same shared data at the same time ● write/read at the same time ● readers block readers
  • 14. Clojure way ● There is no any possibility to change data by direct access. All data structures are immutable, remember? ● Only read, if we add element - new data structure will be created. ● No locks ● No access problems
  • 15. But what if we really want change data in Clojure? This opportunity is also provided by the language! All hard work done for us.
  • 16. Reference types ● Vars ● Refs ● Atoms ● Agents
  • 17. Vars ● Reference to mutable storage location ● Dynamically rebound on a per-thread basis ● Functions are vars, so they can be dynamically rebound too ● Ensure safe use via thread-isolation (def x 5) // root-binding (x) // 5 .... (binding [x 20] (+ x 1)) // 21 .... (x) // 5 again, not changed
  • 18. Changing Vars (set! var-name new-value) ● cannot change root-binding ● works only in thread-local context (in "binding") (def x 5) // root-binding (set! x 7) // exception will be thrown .... (binding [x 20] (println (+ x 1)) // 21 (set! x 10) (+ x 5)) // 15 .... (x) // 5 again, not changed
  • 19. Refs ● Based on Software Transactional Memory (STM) ● Change the value by applying a function to old value ● Refs can be changed within a transaction only ● Transactions will be retried automatically if conflict happens ● To make changes code must be surrounded with (dosync ...) ● All changes are atomic
  • 20. Refs (def r (ref '(1 2 3))) (deref r) // list (1 2 3) (dosync // transaction begins .... (alter r // change ref! (fn [_] '(10 9 8)) ... ) // transaction ends (deref r) // list (10 9 8)
  • 21. Refs lifecycle (def r (ref '(1 2 3))) (deref r) (dosync .... if any other transaction (alter r commutes - this (fn [_] '(10 9 8)) transaction is repeated ... ) Otherwise, that's ok and (deref r) programs continues execution
  • 22. Atoms ● Way to manage shared state synchronously ● Change the value by applying a function to old value ● This is done in an atomic manner by function swap! ● Internally, swap! reads the current value, appliesthe function to it, and attemprs to call compare-and-set! for this atom
  • 23. Atoms best practices by Rich Hickey (defn memoize [f] (let [mem (atom {})] (fn [& args] (if-let [e (find @mem args)] (val e) (let [ret (apply f args)] (swap! mem assoc args ret) ret)))))
  • 24. Atoms best practices by Rich Hickey (defn fib [n] (if (<= n 1) n (+ (fib (dec n)) (fib (- n 2))))) (time (fib 35)) user=> "Elapsed time: 941.445 msecs" (def fib (memoize fib)) (time (fib 35)) user=> "Elapsed time: 0.044 msecs"
  • 25. Agents ● Asynchronous changing. They are not blocking main execution, return immediately ● Change the value by applying a function to old value ● Agent action dispatches take the form (send agent fn args*) ● If other actions try to change data - they will be added to the queue
  • 26. Agents (def a (agent 5)) // create agent (deref a) // 5 (send a (fn [old-val] (+ old-val 5))) (deref a) // may be 5, or may be 10 (Thread/sleep 1000) (deref a) // 10
  • 27. Useful links ● https://groups.google.com/forum/#!forum/clojure ● http://clojure.org ● http://alexott.net/ru/clojure/index.html ● http://www.lisperati.com/clojure-spels/casting.html ● http://www.pluralsight-training. net/microsoft/courses/TableOfContents? courseName=clojure-concurrency-tutorial
  • 28. Q&A