Futures - Spicery/Nutmeg GitHub Wiki

"Futures" are placeholders for the results of computation that happen 'out of sequence'. This computation is forced to happen on-demand when the actual value is required (e.g. because of a run-time type-check). Technically this makes them implicit futures. If an executing thread needs the value of a future, it will automatically block until that future has been evaluated.

There is a simple syntax for creating futures:

$( EXPRESSION )               # A future that when forced (implicitly) must yield a single value, equivalent to $( EXPRESSION:: _ )
$( EXPRESSION:: PATTERN)      # A future that when forced (implicitly) must match the pattern.
PATTERN := $( EXPRESSION )    # Binds the variables of the pattern to futures, the expression must yield a matching result.

The futures created by this syntax are lazy, meaning that their evaluation is deferred as long as possible in the same execution-thread as they were created. However, Nutmeg also has concurrently evaluated futures via message sending. Messages are sent asynchronously and their results are represented by futures. These futures are filled-in when the message-handler has finished, as opposed to being evaluated lazily.

Sometimes you want to iterate over a collection of futures in the order they are available. This is made possible via the FutureCollection.

# Create a collection of futures
val fcollection = FutureCollection( for i in L do $(f(i)) end )
# How many are ready?
val count = fcollection.countReady
# Pick the first one to be ready, removing it from the collection.
val v = fcollection.popReady