applying the paradigms of core.async in clojure and clojurescript

40
Paradigms of core.async LambdaJam Brisbane Workshop May 2014 Julian Gamble @juliansgamble Github repo http://bit.ly/ylj14clojure

Upload: julian-gamble

Post on 15-Jul-2015

291 views

Category:

Software


0 download

TRANSCRIPT

Paradigms  of  core.async

LambdaJam  Brisbane  Workshop  May  2014  Julian  Gamble  @juliansgamble  

!

Github  repo  http://bit.ly/ylj14-­‐clojure  

Paradigms  of  core.async

LambdaJam  Brisbane  Workshop  May  2014  Julian  Gamble  @juliansgamble  

!

Github  repo  http://bit.ly/ylj14-­‐clojure  

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)

BenefitsIn 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

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

Requirements

• Prerequisites are high! (No intro-to-lang classes)

• Text editor

• Leiningen installed

• Internet connection preferable but not essential (Cloning github repo)

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-­‐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)

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

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

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

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

2-basic-example• Open the PDF and then the code

-­‐main  function

first  go  block

second  go  blockmy-­‐q>! <!

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-­‐q2>!

3-basic-multi-chan-example

4-tim-baldridge-10K-processes

make-­‐scene  function

make-­‐cell  function10K

go  block  1. Set  colour  2. Paint  canvas  cell  3. Pause  for  random  interval  4. Loop

5-not-10K-processes

makeScene  function10K

makeCell  function

mainLoop  function

100

6-swanodette-10K-processes

let  block render-­‐loop render!‘render’ ‘queue’

[idx  v] [idx  v]

core.async  channel parameter  passed  in  during  function  call

7-rich-hickey-original-ants-demo-­‐main  function

animation  agent

ant  behaviour  agent

evaporation  agent

world  symbol  (vector  of  vectors)

send-­‐off send-­‐offsend-­‐off

8-ants-cljs-no-asyncanimate  function

send-­‐off-­‐animation  function

behave-­‐ants  function

evaporate  function

world  symbol  (vector  of  vectors)

single  call single  callsingle  call

requestAnimationFrame  (callback)

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

Questions?

LambdaJam  Brisbane  Workshop  May  2014  Julian  Gamble  @juliansgamble  

!

Github  repo  http://bit.ly/ylj14-­‐clojure  [Then the final slide]