Skip to content

Instantly share code, notes, and snippets.

@vyzo
Created December 29, 2017 18:30
Show Gist options
  • Save vyzo/2945f7de5f6d88f46f2416438f8823d8 to your computer and use it in GitHub Desktop.
Save vyzo/2945f7de5f6d88f46f2416438f8823d8 to your computer and use it in GitHub Desktop.
(defsyntax (let-hash stx)
(syntax-case stx ()
((macro expr body ...)
(with-syntax ((@ref (stx-identifier #'macro '%%ref)))
#'(let (hash expr)
(let-syntax
((var-ref
(syntax-rules ()
((_ id) (@ref id)))))
(let-syntax
((@ref
(lambda (stx)
(syntax-case stx ()
((_ id)
(let (str (symbol->string (stx-e #'id)))
(if (eq? (string-ref str 0) #\.)
(with-syntax ((sym (string->symbol (substring str 1 (string-length str)))))
(if (eq? (string-ref str 1) #\.)
;; quoted (multiple dots)
#'(var-ref sym)
#'(hash-ref hash 'sym)))
#'(var-ref id))))))))
body ...)))))))
@vyzo
Copy link
Author

vyzo commented Feb 1, 2018

(def my-hash (hash (a 1) (b 2) (c (hash (a 11) (b 12) (c 13)))))
(let-hash my-hash [.a .b (let-hash .c [.a .b .c ..a ..b])])
=> '(1 2 (11 12 13 1 2))

@vyzo
Copy link
Author

vyzo commented Feb 1, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment