Skip to content

Instantly share code, notes, and snippets.

Created April 21, 2019 09:44
What would you like to do?
#lang racket
(require syntax/parse/define "foods.rkt" (for-syntax "foods.rkt"))
(add-delicious-food! "pineapple")
(add-delicious-food! "sushi")
(add-delicious-food! "cheesecake")
(define-simple-macro (add-food-combinations! [fst:string ...]
[snd:string ...])
#:do [(for* ([fst-str (in-list (syntax->datum #'[fst ...]))]
[snd-str (in-list (syntax->datum #'[snd ...]))])
(add-delicious-food! (string-append fst-str " " snd-str)))]
; should add “fried chicken,” “roasted chicken”, “fried potato,” and “roasted potato”
(add-food-combinations! ["fried" "roasted"] ["chicken" "potato"])
#:args [food-to-check]
(if (delicious-food? food-to-check)
(printf "~a is a delicious food.\n" food-to-check)
(printf "~a is not delicious.\n" food-to-check)))
#lang racket
(require (for-syntax racket/syntax)
(provide define/cross-phase)
(define-simple-macro (define/cross-phase x:id e:expr)
#:with topic-mod-name (generate-temporary 'cross-phase-topic-key)
(module topic-mod-name '#%kernel
(#%declare #:cross-phase-persistent)
(#%provide topic)
(define-values [topic] (gensym "cross-phase")))
(require 'topic-mod-name)
(define x (make-cross-phase topic (λ () e)))))
(define root-logger (current-logger))
(define (make-cross-phase topic thunk)
(define receiver (make-log-receiver root-logger 'debug topic))
(define chan (make-channel))
(define executor (make-will-executor))
(let ()
(define canary (gensym 'canary))
(will-register executor canary (λ (v) 'collected))
(log-message root-logger 'debug topic ""
(vector-immutable canary chan) #f)
(let loop ()
(match (sync receiver)
[(vector _ _ (vector _ (== chan eq?)) _)
(define execute-evt (wrap-evt executor will-execute))
(define result (let loop ([n 0])
(or (sync/timeout 0 chan execute-evt)
(collect-garbage (if (< n 3) 'minor 'major))
(loop (add1 n))))))
(match result
[(vector _ value)
(define value (thunk))
(λ ()
(let loop ()
(match (sync receiver)
[(vector _ _ (vector canary chan) _)
(thread (λ () (channel-put chan (vector-immutable canary value))))
#lang racket
(require "define-cross-phase.rkt")
(provide delicious-food? add-delicious-food!)
(define/cross-phase cross:set-member? set-member?)
(define/cross-phase cross:set-add! set-add!)
(define/cross-phase delicious-foods (mutable-set))
(define (delicious-food? food)
(cross:set-member? delicious-foods food))
(define (add-delicious-food! new-food)
(cross:set-add! delicious-foods new-food))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment