Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Paradigms 
of 
core.async 
Clojure 
Conj 
Washington 
Nov 
2014 
Julian 
Gamble 
@juliansgamble
Paradigms 
of 
core.async 
Clojure 
Conj 
Washington 
Nov 
2014 
Julian 
Gamble 
@juliansgamble
Paradigms 
of 
core.async 
Clojure 
Conj 
Washington 
Nov 
2014 
Julian 
Gamble 
@juliansgamble
Core.async is about:
Core.async is about: 
• queues in your application
Core.async is about: 
• queues in your application 
• making your application simpler to reason 
about
Benefits 
In this talk you will gain an understanding of: 
• go blocks and how they do concurrency 
• core.async queues
Benefits 
In this talk you will gain an understanding of: 
• go blocks and how they do concurrency 
• core.async queues 
•...
Benefits 
In this talk you will gain an understanding of: 
• go blocks and how they do concurrency 
• core.async queues 
•...
Benefits 
In this talk you will gain an understanding of: 
• converting a Clojure code-base to ClojureScript 
• some tips ...
What is core.async?
What is core.async? 
• A set of primitives for creating, reading and 
writing to queues 
• A code walking macro that splic...
What is core.async? 
• A set of primitives for creating, reading and 
writing to queues 
• A code walking macro that splic...
What does core.async say about 
Clojure? 
• core.async is implemented using a sophisticated Lisp 
macro - something only p...
What is CSP? 
• Communicating Sequential Processes 
• Comes out of process calculi – an attempt in the 
1960’s and 1970’s ...
What is CSP? 
• Communicating Sequential Processes 
• Comes out of process calculi – an attempt in the 
1960’s and 1970’s ...
basic example 
-­‐main 
function 
first 
go 
block 
second 
go 
>! my-­‐q block <!
2-basic-example
2-basic-example
2-basic-example
2-basic-example
2-basic-example
basic example 
[Demo]
basic example
basic multi-channel example 
-­‐main 
function 
first 
go 
block 
third 
go 
block 
>! my-­‐q1 
alts! 
second 
go 
block m...
basic multi-channel example 
[DEMO]
basic multi-channel example
Tim Baldridge 10K 
processes 
make-­‐scene 
function 
make-­‐cell 
function 
10K 
go 
block 
1. Set 
colour 
2. Paint 
can...
[DEMO]
not 10K processes 
makeScene 
function 
10K 
makeCell 
function 
mainLoop 
function 
100
[DEMO]
[DEMO]
swanodette 10K processes 
[go 
block] 
let 
block 
[idx 
v] [idx 
v] 
[go 
block] 
render-­‐loop ‘render’ ‘queue’ render! ...
swanodette 10K processes 
[go 
block] 
let 
block 
[idx 
v] [idx 
v] 
[go 
block] 
render-­‐loop ‘render’ ‘queue’ render! ...
[DEMO]
Events with core.async 
-­‐main 
function 
listen 
to 
DOM 
event 
and 
return 
channel 
[go 
block] 
print 
queue 
messag...
JS event queue
Parallelism is not 
Concurrency 
Imagine streams of execution in your program
Parallelism is not 
Concurrency 
Imagine streams of execution in your program
Parallelism is not 
Concurrency 
Imagine streams of execution in your program
Parallelism 
1. For core.async "use it where ever you'd use a queue" 
2. In the Tim Baldridge 10K processes example above ...
Parallelism 
1. For core.async "use it where ever you'd use a queue" 
2. In the Tim Baldridge 10K processes example above ...
Parallelism 
1. For core.async "use it where ever you'd use a queue" 
2. In the Tim Baldridge 10K processes example above ...
Quick recap 
• core.async is all about queues
Quick recap 
• core.async is all about queues 
• queues can used in your application for: 
• parallelism 
• separating the...
[DEMO]
Rich Hickey original ants 
demo
Rich Hickey original ants 
demo 
-­‐main 
function 
send-­‐off send-­‐off send-­‐off 
animation 
agent 
ant 
behaviour 
ag...
Converting to ClojureScript 
Missing functions in ClojureScript:
Converting to ClojureScript 
Missing functions in ClojureScript: 
• defstruct - replace with a map 
• alter - replace with...
Converting to ClojureScript 
Missing functions in ClojureScript: 
• defstruct - replace with a map 
• alter - replace with...
ants cljs no async 
animate 
function 
requestAnimationFrame 
single 
call single 
call single 
send-­‐off-­‐ 
animation 
...
[DEMO]
Adding core.async
Adding core.async 
• You can use a go block with a timeout queue in 
the same way you’d use a thread with a sleep 
functio...
adding core.async
adding core.async
[DEMO]
Using optimisations from David 
Nolen’s chambered example 
• Macros for creating arrays and for-loops 
• Underlying assump...
Using optimisations from David 
Nolen’s chambered example
Using optimisations from David 
Nolen’s chambered example
[DEMO]
Back to The Italian Job 
• Who or what was the hero of the story?
Summary 
• core.async is about using queues 
• core.async is about making your application simpler to reason about
Summary 
• core.async is about using queues 
• core.async is about making your application simpler to reason about 
• clas...
Summary 
• core.async is about using queues 
• core.async is about making your application simpler to reason about 
• clas...
Questions? 
Clojure 
Conj 
Nov 
2014 
Julian 
Gamble 
@juliansgamble 
github.com/juliangamble/clojure-­‐conj-­‐2014-­‐para...
Clojure Conj 2014 - Paradigms of core.async - Julian Gamble
Clojure Conj 2014 - Paradigms of core.async - Julian Gamble
Clojure Conj 2014 - Paradigms of core.async - Julian Gamble
Clojure Conj 2014 - Paradigms of core.async - Julian Gamble
Clojure Conj 2014 - Paradigms of core.async - Julian Gamble
Clojure Conj 2014 - Paradigms of core.async - Julian Gamble
Clojure Conj 2014 - Paradigms of core.async - Julian Gamble
Clojure Conj 2014 - Paradigms of core.async - Julian Gamble
Clojure Conj 2014 - Paradigms of core.async - Julian Gamble
Clojure Conj 2014 - Paradigms of core.async - Julian Gamble
Clojure Conj 2014 - Paradigms of core.async - Julian Gamble
Clojure Conj 2014 - Paradigms of core.async - Julian Gamble
Clojure Conj 2014 - Paradigms of core.async - Julian Gamble
Clojure Conj 2014 - Paradigms of core.async - Julian Gamble
Clojure Conj 2014 - Paradigms of core.async - Julian Gamble
Clojure Conj 2014 - Paradigms of core.async - Julian Gamble
Clojure Conj 2014 - Paradigms of core.async - Julian Gamble
Clojure Conj 2014 - Paradigms of core.async - Julian Gamble
Clojure Conj 2014 - Paradigms of core.async - Julian Gamble
Clojure Conj 2014 - Paradigms of core.async - Julian Gamble
Clojure Conj 2014 - Paradigms of core.async - Julian Gamble
Clojure Conj 2014 - Paradigms of core.async - Julian Gamble
Clojure Conj 2014 - Paradigms of core.async - Julian Gamble
Clojure Conj 2014 - Paradigms of core.async - Julian Gamble
Clojure Conj 2014 - Paradigms of core.async - Julian Gamble
Clojure Conj 2014 - Paradigms of core.async - Julian Gamble
Upcoming SlideShare
Loading in …5
×

Clojure Conj 2014 - Paradigms of core.async - Julian Gamble

1,267 views

Published on

This is a talk given at Clojure Conj 2014 in Washington DC. The title was Paradigms of core.async and it was given by Julian Gamble.
The code and demos are available here: https://github.com/juliangamble/clojure-conj-2014-paradigms-of-core-async

Published in: Technology
  • Be the first to comment

Clojure Conj 2014 - Paradigms of core.async - Julian Gamble

  1. 1. Paradigms of core.async Clojure Conj Washington Nov 2014 Julian Gamble @juliansgamble
  2. 2. Paradigms of core.async Clojure Conj Washington Nov 2014 Julian Gamble @juliansgamble
  3. 3. Paradigms of core.async Clojure Conj Washington Nov 2014 Julian Gamble @juliansgamble
  4. 4. Core.async is about:
  5. 5. Core.async is about: • queues in your application
  6. 6. Core.async is about: • queues in your application • making your application simpler to reason about
  7. 7. Benefits In this talk you will gain an understanding of: • go blocks and how they do concurrency • core.async queues
  8. 8. Benefits In this talk you will gain an understanding of: • go blocks and how they do concurrency • core.async queues • go block timers • alts! macro and how it enables reading multiple queues
  9. 9. Benefits In this talk you will gain an understanding of: • go blocks and how they do concurrency • core.async queues • go block timers • alts! functions and how it enables reading multiple queues • how core.async processes work in ClojureScript • the visual impacts of how the core.async function enables simple ‘process-level’ pauses • how to use core.async to separate out the calculation and display parts of your code
  10. 10. Benefits In this talk you will gain an understanding of: • converting a Clojure code-base to ClojureScript • some tips for optimising ClojureScript
  11. 11. What is core.async?
  12. 12. What is core.async? • A set of primitives for creating, reading and writing to queues • A code walking macro that splices go blocks into state machines
  13. 13. What is core.async? • A set of primitives for creating, reading and writing to queues • A code walking macro that splices go blocks into state machines • A mechanism for asynchronous computation • A library in both Clojure and ClojureScript
  14. 14. What does core.async say about Clojure? • core.async is implemented using a sophisticated Lisp macro - something only possible in a functional language • in non-functional languages like Go and C# - CSP is achieved by compiler-extensions - whereas in a functional language like Clojure - this functionality comes as a mere library
  15. 15. What is CSP? • Communicating Sequential Processes • Comes out of process calculi – an attempt in the 1960’s and 1970’s to optimise computer usage through specialised algebra.
  16. 16. What is CSP? • Communicating Sequential Processes • Comes out of process calculi – an attempt in the 1960’s and 1970’s to optimise computer usage through specialised algebra. • Based on message passing via channels • Subject of 1978 Book by C.A.R. Hoare
  17. 17. basic example -­‐main function first go block second go >! my-­‐q block <!
  18. 18. 2-basic-example
  19. 19. 2-basic-example
  20. 20. 2-basic-example
  21. 21. 2-basic-example
  22. 22. 2-basic-example
  23. 23. basic example [Demo]
  24. 24. basic example
  25. 25. basic multi-channel example -­‐main function first go block third go block >! my-­‐q1 alts! second go block my-­‐q2 >!
  26. 26. basic multi-channel example [DEMO]
  27. 27. basic multi-channel example
  28. 28. Tim Baldridge 10K processes make-­‐scene function make-­‐cell function 10K go block 1. Set colour 2. Paint canvas cell 3. Pause for random interval 4. Loop
  29. 29. [DEMO]
  30. 30. not 10K processes makeScene function 10K makeCell function mainLoop function 100
  31. 31. [DEMO]
  32. 32. [DEMO]
  33. 33. swanodette 10K processes [go block] let block [idx v] [idx v] [go block] render-­‐loop ‘render’ ‘queue’ render! core.async channel parameter passed in during function call
  34. 34. swanodette 10K processes [go block] let block [idx v] [idx v] [go block] render-­‐loop ‘render’ ‘queue’ render! core.async channel parameter passed in during function call No more than 1024 pending puts are allowed on a single channel.
  35. 35. [DEMO]
  36. 36. Events with core.async -­‐main function listen to DOM event and return channel [go block] print queue message put! out/clicks <!
  37. 37. JS event queue
  38. 38. Parallelism is not Concurrency Imagine streams of execution in your program
  39. 39. Parallelism is not Concurrency Imagine streams of execution in your program
  40. 40. Parallelism is not Concurrency Imagine streams of execution in your program
  41. 41. Parallelism 1. For core.async "use it where ever you'd use a queue" 2. In the Tim Baldridge 10K processes example above - it is not explicitly using a queue (<! (timeout (rand-int 1000)))))))
  42. 42. Parallelism 1. For core.async "use it where ever you'd use a queue" 2. In the Tim Baldridge 10K processes example above - it is not explicitly using a queue (<! (timeout (rand-int 1000))))))) 3. But in the same example - it is implicitly using the queue of the core.async process scheduler - so you're still using a queue. 4. Go blocks are the lightweight equivalent of 'heavyweight' threads
  43. 43. Parallelism 1. For core.async "use it where ever you'd use a queue" 2. In the Tim Baldridge 10K processes example above - it is not explicitly using a queue (<! (timeout (rand-int 1000))))))) 3. But in the same example - it is implicitly using the queue of the core.async process scheduler - so you're still using a queue. 4. Go blocks are the lightweight equivalent of 'heavyweight' threads 5. Heavyweight threads implicitly listen to the queue of the OS 'ready queue' 6. You're already implicitly using queues whenever you use threads.
  44. 44. Quick recap • core.async is all about queues
  45. 45. Quick recap • core.async is all about queues • queues can used in your application for: • parallelism • separating the calculation from the display logic • event handling
  46. 46. [DEMO]
  47. 47. Rich Hickey original ants demo
  48. 48. Rich Hickey original ants demo -­‐main function send-­‐off send-­‐off send-­‐off animation agent ant behaviour agent evaporation agent world symbol (vector of vectors)
  49. 49. Converting to ClojureScript Missing functions in ClojureScript:
  50. 50. Converting to ClojureScript Missing functions in ClojureScript: • defstruct - replace with a map • alter - replace with swap!
  51. 51. Converting to ClojureScript Missing functions in ClojureScript: • defstruct - replace with a map • alter - replace with swap! • sync • dosync • agent
  52. 52. ants cljs no async animate function requestAnimationFrame single call single call single send-­‐off-­‐ animation function behave-­‐ants function (callback) evaporate function world symbol (vector of vectors) call
  53. 53. [DEMO]
  54. 54. Adding core.async
  55. 55. Adding core.async • You can use a go block with a timeout queue in the same way you’d use a thread with a sleep function
  56. 56. adding core.async
  57. 57. adding core.async
  58. 58. [DEMO]
  59. 59. Using optimisations from David Nolen’s chambered example • Macros for creating arrays and for-loops • Underlying assumptions: • cljs data structures are not yet fully performant - so consider replacing with arrays for speed • The cljs compiler does not yet fully optimise higher order functions - the most efficient looping construct will be a for-loop macro that uses the loop function
  60. 60. Using optimisations from David Nolen’s chambered example
  61. 61. Using optimisations from David Nolen’s chambered example
  62. 62. [DEMO]
  63. 63. Back to The Italian Job • Who or what was the hero of the story?
  64. 64. Summary • core.async is about using queues • core.async is about making your application simpler to reason about
  65. 65. Summary • core.async is about using queues • core.async is about making your application simpler to reason about • classic applications are: user interface events, presentation loops, parallelism in non-parallel environments • you can consider a multithreaded application a queue listener even if it doesn’t appear to use queues
  66. 66. Summary • core.async is about using queues • core.async is about making your application simpler to reason about • classic applications are: user interface events, presentation loops, parallelism in non-parallel environments • you can consider a multithreaded application a queue listener even if it doesn’t appear to use queues • in porting an application from Clojure to ClojureScript you have to rethink your concurrency • There are lots of optimisations available to make ClojureScript applications run faster • core.async is not a magic sauce you can sprinkle everywhere - know when to use it
  67. 67. Questions? Clojure Conj Nov 2014 Julian Gamble @juliansgamble github.com/juliangamble/clojure-­‐conj-­‐2014-­‐paradigms-­‐of-­‐core-­‐async

×