Skip to content

Instantly share code, notes, and snippets.

@jgreco
Created August 29, 2019 01:02
Show Gist options
  • Save jgreco/03a81813824cdf034b8fb812d727c947 to your computer and use it in GitHub Desktop.
Save jgreco/03a81813824cdf034b8fb812d727c947 to your computer and use it in GitHub Desktop.
#lang racket
;; > (require "observer-app.rkt")
;; > (hash-set! (observers-namespace) '+ `(,(lambda (a . b) (printf "adding ~a to ~a~n" a b))))
;; > (+ 1 2 3)
;; adding 1 to (2 3)
;; 6
(provide (rename-out [observer-app #%app])
observers-namespace)
(define observers-namespace (make-parameter (make-hash)))
(define (lookup-observers x) (hash-ref (observers-namespace) x list))
(define-syntax (observer-app stx)
(syntax-case stx ()
[(_ f args ...)
(let ([sym (syntax-e #'f)])
#`((lambda (proc . args2 )
(for ([o (lookup-observers '#,sym)])
(apply o args2))
(apply proc args2 )) f args ...))]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment