2020-02-16 21:39:42 +01:00
|
|
|
#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.
|
2020-02-17 00:16:01 +01:00
|
|
|
;;;
|
|
|
|
;;; > (let ([ht #hash((a . 1) (b . 2))])
|
|
|
|
;;; (auto-hash-ref/explicit (ht a b) (+ a (* 2 b))))
|
|
|
|
;;; 5
|
|
|
|
;;;
|
2020-02-16 21:39:42 +01:00
|
|
|
(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)]))
|
|
|
|
|
|
|
|
|