-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.rkt
34 lines (27 loc) · 1.21 KB
/
hash.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#lang racket
(module+ test
(require rackunit))
;; libquiche/hash - tools for working with hashmaps ================================
(provide hash-path) ;; Access child elements of a hash map by a list of keys, which forms a path
;; Implementations ============================================================
(define (hash-path hash keys)
(if (empty? keys)
hash
(hash-path (hash-ref hash (first keys)) (rest keys))))
(module+ test
(define test-hash
#hash((Languages .
#hash((ALGOL .
#hash((BASIC . "c64emu")
(C . "gcc")
(C++ . "g++")))
(Lisp .
#hash(("Common Lisp" . "gnucl")
("Emacs Lisp" . "emacs")
(Racket . "racket")))))))
(check-equal? (hash-path test-hash '(Languages ALGOL C++))
"g++")
(check-equal? (hash-path test-hash '(Languages Lisp "Emacs Lisp"))
"emacs")
(check-equal? (hash-path test-hash '(Languages ALGOL))
#hash((BASIC . "c64emu") (C . "gcc") (C++ . "g++"))))