Next: , Previous: , Up: actor-lib: A standard library of sorts   [Contents][Index]


6.7 On

The (goblins actor-lib on) module provides convenient syntax to facilitate promise pipelining.

Syntax: let-on ((var exp) …) body

Syntax sugar around on, all-of, lambda, and match-lambda to allow using promise pipelining with a let-like syntax. Each exp (an asynchronous invocation of an actor resulting in a promise) is evaluated concurrently; the resulting promises are bound to their corresponding vars; and finally body is evaluated in an environment where all promises are resolved.

For example, given a doubler object defined as:

(define (^doubler bcom)
  (lambda (x)
    (* x 2)))
(define doubler (spawn ^doubler))

This use of let-on

(let-on ((four (<- doubler 2))
         (eight (<- doubler 4)))
  (+ four eight))

is equivalent to…

(use-modules (goblins actor-lib joiners))
(on (all-of (<- doubler 2)
            (<- doubler 4))
  (match-lambda*
    ((four eight)
     (+ four eight))))
Syntax: let*-on ((var exp) …) body

Like let-on, except paralleling let*; each exp is resolved sequentially rather than concurrently, and each var is available in the body of the succeeding exps. This is equivalent to nested on and lambda calls, without using match-lambda or all-of.

So this use of let*-on

(let*-on ((four (<- doubler 2))
          (eight (<- doubler four)))
  (+ four eight))

…is equivalent to…

(on (<- doubler 2)
    (lambda (four)
      (on (<- doubler four)
          (lambda (eight)
            (+ four eight))
          #:promise? #t))
    #:promise? #t)
Procedure: on-each proc vow

This procedure is equivalent to an on with vow, where vow resolves to a list, then doing a for-each with proc and the resolved value. It does not return any values to the caller.

(define my-cell (spawn ^cell (list 1 2 3)))
(on-each pk (<- my-cell))
;;; (1)
;;; (2)
;;; (3)
Procedure: on-map proc vow

This procedure is equivalent to an on with vow, where vow resolves to a list, then doing a map. proc will be invoked with each value in the list. The procedure will return a promise which will resolve to the return value of map.

(define my-cell (spawn ^cell (list 1 2 3)))
(define (double n) (* n 2))
(define doubled-numbers-vow
  (on-map double (<- my-cell)))
(on doubled-numbers-vow pk)
;;; ((2 4 8))
Procedure: on-filter pred vow

This procedure is equivalent to an on with vow, where vow resolves to a list, then doing a filter. pred will be applied with each value in the list to determine if they will appear in the result. The procedure will return a promise which will resolve to the return value of the filter.

(define my-cell (spawn ^cell (list 1 2 3 4)))
(define even-numbers-vow
  (on-filter even? (<- my-cell)))
(on even-numbers-vow pk)
;;; ((2 4))
Syntax: on-match vow match-clause …

This is syntax sugar around on, match-lambda to allow for easier matching on the result of promise values. It’s equivalent to doing an on with vow where the on handler is a match-lambda with match-clauses.

(define my-cell (spawn ^cell (list 'result 42)))
(define my-result-vow
  (on-match (<- my-cell)
    (('result some-result) some-result)
    (('no-result) (error "Got no result"))))
(on my-result-vow pk)
;;; (42)

Next: Methods, Previous: Joiners, Up: actor-lib: A standard library of sorts   [Contents][Index]