8sync Examples Part 1

I want to play around with the 8sync framework, but the project is unfortunately lacking an API reference. So, I am writing example code to help me learn each of the modules, and thought I might as well share. Here is some example code for the (8sync agenda) module.

(define-module (8sync agenda examples))

(use-modules (8sync agenda)
             (srfi srfi-26)
             (srfi srfi-31))

(export make-stuff)

(define (make-something thing amt)
  ((rec (l n)
        (when (positive? n)
          (display "Making ") (display thing) (display "\n")
          (8yield)
          (l (- n 1)))) amt))

(define (make-stuff)
  (define my-agenda
    (make-agenda
     #:queue (make-q* (cut make-something "widget" 10)
                      (cut make-something "thingamabob" 5)
                      (cut make-something "sprocket" 5))))
  (run-agenda my-agenda))

This illustrates how an Agenda can be used to interweave the execution of computations, by yielding to other computations during your loop:

scheme@(guile-user)> ,use (8sync agenda examples)
scheme@(guile-user)> (make-stuff)
Making widget
Making thingamabob
Making sprocket
Making widget
Making thingamabob
Making sprocket
Making widget
Making thingamabob
Making sprocket
Making widget
Making thingamabob
Making sprocket
Making widget
Making thingamabob
Making sprocket
Making widget
Making widget
Making widget
Making widget
Making widget
$2 = done

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s