1 Exception utilities
2 Number utilities
3 String utilities
4 Bytes utilities
5 Symbol utilities
6 List utilities
7 PLT 4x hash utilities
8 PLT 3x hash utilities
9 Contract utilities
10 File and path utilities
11 Parameter utilities
12 Syntax utilities
13 SRFI19 time utilities
14 Generators
15 Generators (short names)
16 Pipelines
17 Write-through cache
18 Yieldable procedures
19 Debugging tools
20 Profiling tools
21 Logging tools
On this page:
make-yieldable
yieldable
Version: 3.99.0.23

 

18 Yieldable procedures

 (require (planet untyped/unlib/yield))

Implements the "yield" operator of Ruby / Python using continuations. The "yield" command pauses the execution of a procedure and returns a result. Execution continues from the same point in the next invocation of the procedure (rather from the beginning of the procedure as usual).

Supports procedures with multiple arguments and return types.

Examples:

  > (define calc

      (make-yieldable

       (lambda (yield)

         (lambda (a b)

           (define-values (c d)

             (yield a b))

           (values (* c 2) (* d 2))))))

  > (calc 1 2)

  1

  2

  > (calc 1 2)

  2

  4

(make-yieldable yield->proc)  target-proc

  yield->proc : (yield-proc -> target-proc)

Creates a target procedure that can use yield-proc to suspend operation. yield-proc and target-proc have symmetric contracts:

yield-proc : a b c -> d e

target-proc : d e -> a b c

(yieldable yield-id expr ...)

A syntactic form of make-yieldable that avoids writing so many lambdas.

Examples:

  > (define calc

      (yieldable yield

        (lambda (a b)

          (define-values (c d)

            (yield a b))

          (values (* c 2) (* d 2)))))

  > (calc 1 2)

  1

  2

  > (calc 1 2)

  2

  4