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.
Right Money Management App For Your Financial Goals
Applying the paradigms of core.async in Clojure and ClojureScript
1. Paradigms
of
core.async
LambdaJam
Brisbane
Workshop
May
2014
Julian
Gamble
@juliansgamble
!
Github
repo
http://bit.ly/ylj14-‐clojure
2. Paradigms
of
core.async
LambdaJam
Brisbane
Workshop
May
2014
Julian
Gamble
@juliansgamble
!
Github
repo
http://bit.ly/ylj14-‐clojure
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. 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. 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. Requirements
• Prerequisites are high! (No intro-to-lang classes)
• Text editor
• Leiningen installed
• Internet connection preferable but not essential
(Cloning github repo)
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. 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. 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. 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. 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. 2-basic-example
• Open the PDF and then the code
-‐main
function
first
go
block
second
go
>! my-‐q block <!
26. 6-swanodette-10K-processes
[idx
v] [idx
v]
let
block ‘render’ render-‐loop ‘queue’ render!
core.async
channel parameter
passed
in
during
function
call
35. 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
36.
37.
38. 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
39. Questions?
LambdaJam
Brisbane
Workshop
May
2014
Julian
Gamble
@juliansgamble
!
Github
repo
http://bit.ly/ylj14-‐clojure
[Then the final slide]