Skip to content

Instantly share code, notes, and snippets.

@cbarrett
Last active December 12, 2015 05:28
Show Gist options
  • Save cbarrett/4721602 to your computer and use it in GitHub Desktop.
Save cbarrett/4721602 to your computer and use it in GitHub Desktop.
Using delimited continuations to implement RAC's signal type
#lang scheme
(require racket/control)
(require racket/match)
(define (create-signal didSubscribe)
(lambda (next error complete)
(match (shift k (didSubscribe k))
[(list 'next v) (next v)]
[(list 'error e) (error e)]
[(list 'complete) (complete)])))
;; helpers
(define (add-subscriber s n e c) (s n e c))
(define (send-next k v) (k (list 'next v)))
(define (send-error k e) (k (list 'error e)))
(define (send-complete k) (k (list 'complete)))
;; example usage. prints "hello, ""world!""done"
(let ([signal (create-signal (lambda (subscriber)
(send-next subscriber "hello, ")
(send-next subscriber "world!")
(send-complete subscriber)))])
(add-subscriber signal
(lambda (next) (displayln next))
(lambda (error) (displayln (string-append "! " error)))
(lambda () (displayln "done"))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment