Last active
December 12, 2015 05:28
-
-
Save cbarrett/4721602 to your computer and use it in GitHub Desktop.
Using delimited continuations to implement RAC's signal type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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