Skip to content

Instantly share code, notes, and snippets.

@samdphillips
Created April 1, 2022 20:42
Show Gist options
  • Save samdphillips/e8fdc90b284a5519976127cd902dc312 to your computer and use it in GitHub Desktop.
Save samdphillips/e8fdc90b284a5519976127cd902dc312 to your computer and use it in GitHub Desktop.
threaded box thingy
#lang racket/base
(require racket/match)
(struct holder (thd req-ch))
(define (make-holder initial-value)
(define req-ch (make-channel))
(define thd
(thread
(lambda ()
(define (run current-value)
(match-define (list update return) (channel-get req-ch))
(define new-value (update current-value))
(return current-value new-value)
(run new-value))
(run initial-value))))
(holder thd req-ch))
(define (holder-send h update return)
(channel-put (holder-req-ch h) (list update return)))
(define (holder-send/sync h update)
(define rsp-ch (make-channel))
(define (reply old new)
(channel-put rsp-ch (list old new)))
(holder-send h update reply)
(channel-get rsp-ch))
(define (holder-send/async h update)
(holder-send h update void))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment