dds/utils.rkt

33 lines
888 B
Racket
Raw Normal View History

#lang typed/racket
;;; dds/utils
;;; Various utilities.
(require (for-syntax syntax/parse))
(provide auto-hash-ref/explicit)
;;; HashTable Injection
;;; This section of the file contains some utilities to streamline the
;;; usage of hash tables mapping symbols to values. The goal is
;;; essentially to avoid having to write explicit hash-ref calls.
;;; Given a (HashTable Symbol a) and a sequence of symbols, binds
;;; these symbols to the values they are associated to in the hash
;;; table, then puts the body in the context of these bindings.
;;;
;;; > (let ([ht #hash((a . 1) (b . 2))])
;;; (auto-hash-ref/explicit (ht a b) (+ a (* 2 b))))
;;; 5
;;;
(define-syntax (auto-hash-ref/explicit stx)
(syntax-parse stx
[(_ (ht:id xs:id ...) body:expr)
#`(let #,(for/list ([x (syntax->list #'(xs ...))])
#`[#,x (hash-ref ht '#,x)])
body)]))