Sequence and parallel combinators
I've been re-reading the Lambda, the ultimate... papers again for my podcast. These papers show how functions and function calls can model many of the imperative constructs in programming languages. One practical application of that is to build a compiler whose main construct is the function call, which is a common way to implement Scheme.
In this challenge, we'd like to make constructs for two kinds of execution: sequential and parallel.
Imagine we had actions a
and b
, which are functions which depend on when they are called. If we want them to run in order, meaning a
completes before b
begins, we can create a new action a>b
, like so:
(def a>b (sequential a b))
We can then call (a>b)
to run them in the right order.
If we want them to run in parallel, we can similarly write:
(def a-b (parallel a b))
Then when we call (a-b)
, they will run "at the same time".
Your task is to write sequential
and parallel
, using threads (or some other construct) if needed.
Bonus: devise a way to have return values that you can block on. sequential
should act like do
and return the return value of the last argument. parallel
should return both return values.
Please submit your solutions as comments on this gist.
pmap
and friends limit the number of threads, but usingfuture
directly seems to give the desired behaviour:Here is a demo showing 50 sequences starting and stopping together:
Note that the following version of
parallel
does not start all 50 sequences at the same time:Edited: Simplified
sequential
definitionEdited: Refactored
parallel
to use(mapv ...)
instead of(doall (map ...))
to force evaluation of sequence elements