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 
LambdaJam 
Brisbane 
Workshop 
May 
2014 
Julian 
Gamble 
@juliansgamble 
! 
Github 
repo 
http...
Paradigms 
of 
core.async 
LambdaJam 
Brisbane 
Workshop 
May 
2014 
Julian 
Gamble 
@juliansgamble 
! 
Github 
repo 
http...
Getting set up 
• Clone the repository at: 
• http://github.com/juliangamble/lambdajam-2014- 
core.async-workshop 
or http...
Benefits 
In this workshop you will gain an understanding of: 
• go blocks and how they do concurrency 
• core.async queue...
Benefits 
In this workshop you will gain an understanding of: 
• how to use core.async to send a queue of updates to the 
...
Requirements 
• Prerequisites are high! (No intro-to-lang classes) 
• Text editor 
• Leiningen installed 
• Internet conne...
functional programming in 
Australia
functional programming in 
Australia
Overview 
• We have 7 items to work through: 
Item Description 
1-­‐references API 
Reference 
material 
2-­‐basic-­‐examp...
How do the exercises work? 
Inside each of the 7 directories is a PDF with the 
following headings: 
• Code to Read 
• Thi...
What is core.async? 
• A set of primitives for creating, reading and 
writing to queues 
• A code walking macro that splic...
Why does it relate to functional 
programming? 
• Clojure is a functional language 
• Clojure’s immutable data structures ...
What is CSP? 
• Communicating Sequential Processes 
• Comes out of process calculi – an attempt in the 
1970’s [Transputer...
2-basic-example 
• Open the PDF and then the code 
-­‐main 
function 
first 
go 
block 
second 
go 
>! my-­‐q block <!
2-basic-example
2-basic-example
3-basic-multi-chan-example 
-­‐main 
function 
first 
go 
block 
third 
go 
block 
>! my-­‐q1 
alts! 
second 
go 
block my...
3-basic-multi-chan-example
4-tim-baldridge-10K-processes 
make-­‐scene 
function 
make-­‐cell 
function 
10K 
go 
block 
1. Set 
colour 
2. Paint 
ca...
5-not-10K-processes 
makeScene 
function 
10K 
makeCell 
function 
mainLoop 
function 
100
6-swanodette-10K-processes 
[idx 
v] [idx 
v] 
let 
block ‘render’ render-­‐loop ‘queue’ render! 
core.async 
channel para...
7-rich-hickey-original-ants-demo 
-­‐main 
function 
send-­‐off send-­‐off send-­‐off 
animation 
agent 
ant 
behaviour 
a...
8-ants-cljs-no-async 
animate 
function 
requestAnimationFrame 
single 
call single 
call single 
send-­‐off-­‐ 
animation...
Workshop Activity 
• Make the changes described in pdf #8 to 
change this to a core.async ClojureScript 
program 
• Then w...
Questions? 
LambdaJam 
Brisbane 
Workshop 
May 
2014 
Julian 
Gamble 
@juliansgamble 
! 
Github 
repo 
http://bit.ly/ylj14...
Applying the paradigms of core.async in Clojure and ClojureScript
Applying the paradigms of core.async in Clojure and ClojureScript
Applying the paradigms of core.async in Clojure and ClojureScript
Applying the paradigms of core.async in Clojure and ClojureScript
Applying the paradigms of core.async in Clojure and ClojureScript
Applying the paradigms of core.async in Clojure and ClojureScript
Applying the paradigms of core.async in Clojure and ClojureScript
Applying the paradigms of core.async in Clojure and ClojureScript
Applying the paradigms of core.async in Clojure and ClojureScript
Applying the paradigms of core.async in Clojure and ClojureScript
Applying the paradigms of core.async in Clojure and ClojureScript
Applying the paradigms of core.async in Clojure and ClojureScript
Applying the paradigms of core.async in Clojure and ClojureScript
Applying the paradigms of core.async in Clojure and ClojureScript
Applying the paradigms of core.async in Clojure and ClojureScript
Upcoming SlideShare
Loading in …5
×

Applying the paradigms of core.async in Clojure and ClojureScript

1,051 views

Published on

In this workshop you’ll see a Clojure program that visually demonstrates Clojure’s concurrency semantics and then see how to solve the same underlying concurrency problems using core.async. You’ll see the mapping between programming paradigms you already know and core.async concepts. You’ll apply this in the browser with ClojureScript.

Published in: Software
  • Be the first to comment

Applying the paradigms of core.async in Clojure and ClojureScript

  1. 1. Paradigms of core.async LambdaJam Brisbane Workshop May 2014 Julian Gamble @juliansgamble ! Github repo http://bit.ly/ylj14-­‐clojure
  2. 2. Paradigms of core.async LambdaJam Brisbane Workshop May 2014 Julian Gamble @juliansgamble ! Github repo http://bit.ly/ylj14-­‐clojure
  3. 3. Getting set up • Clone the repository at: • http://github.com/juliangamble/lambdajam-2014- core.async-workshop or http://bit.ly/ylj14-clojure • Or get a copy of the repository and the maven libraries from a usb (or sharing on my computer)
  4. 4. Benefits In this workshop 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
  5. 5. Benefits In this workshop you will gain an understanding of: • how to use core.async to send a queue of updates to the display layer • the original ants implementation Rich Hickey used to demonstrate concurrency features in Clojure • converting a Clojure code-base to ClojureScript • using shared data structures in ClojureScript • a working knowledge of converting a single-threaded ClojureScript application to a concurrent one using core.async
  6. 6. Requirements • Prerequisites are high! (No intro-to-lang classes) • Text editor • Leiningen installed • Internet connection preferable but not essential (Cloning github repo)
  7. 7. functional programming in Australia
  8. 8. functional programming in Australia
  9. 9. Overview • We have 7 items to work through: Item Description 1-­‐references API Reference material 2-­‐basic-­‐example core.async Hello World 3-­‐basic-­‐multi-­‐chan-­‐example Handling multiple channels 4-­‐tim-­‐baldridge-­‐10K-­‐processes Using core.async in ClojureScript 5-­‐not-­‐10K-­‐processes Trying to mimic core.async in raw js 6-­‐swanodette-­‐10K-­‐processes Using core.async for an animation pipeline in js 7-­‐rich-­‐hickey-­‐original-­‐ants-­‐demo The Swing demo Rich Hickey used to demo Clojure concurrency 8-­‐ants-­‐cljs-­‐no-­‐async The actual workshop (don’t look at solution unless you really need it)
  10. 10. How do the exercises work? Inside each of the 7 directories is a PDF with the following headings: • Code to Read • Things to Note In the Code • Code Model • Activities • Questions for Reflection
  11. 11. 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
  12. 12. Why does it relate to functional programming? • Clojure is a functional language • Clojure’s immutable data structures are based on Okasaki’s book – Purely Functional Data Structures • We’ll be comparing different methods of solving concurrency problems in a functional language (STM vs CSP) • 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
  13. 13. What is CSP? • Communicating Sequential Processes • Comes out of process calculi – an attempt in the 1970’s [Transputer era] to optimise computer usage through specialised algebra. • Based on message passing via channels • Subject of 1978 Book by C.A.R. Hoare
  14. 14. 2-basic-example • Open the PDF and then the code -­‐main function first go block second go >! my-­‐q block <!
  15. 15. 2-basic-example
  16. 16. 2-basic-example
  17. 17. 3-basic-multi-chan-example -­‐main function first go block third go block >! my-­‐q1 alts! second go block my-­‐q2 >!
  18. 18. 3-basic-multi-chan-example
  19. 19. 4-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
  20. 20. 5-not-10K-processes makeScene function 10K makeCell function mainLoop function 100
  21. 21. 6-swanodette-10K-processes [idx v] [idx v] let block ‘render’ render-­‐loop ‘queue’ render! core.async channel parameter passed in during function call
  22. 22. 7-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)
  23. 23. 8-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
  24. 24. Workshop Activity • Make the changes described in pdf #8 to change this to a core.async ClojureScript program • Then work through the challenges in the ‘Questions for Reflection’ question
  25. 25. Questions? LambdaJam Brisbane Workshop May 2014 Julian Gamble @juliansgamble ! Github repo http://bit.ly/ylj14-­‐clojure [Then the final slide]

×