hashmaps are now first class objects in the language

This commit is contained in:
Felix Brendel
2019-10-04 17:48:31 +02:00
parent ac70c14e5b
commit 5e82f9175d
15 changed files with 166 additions and 51 deletions
+3
View File
@@ -1,3 +1,6 @@
(define hm/set! hash-map-set!)
(define hm/get hash-map-get)
(define-syntax (pe expr)
`(print ',expr "evaluates to" ,expr))
+4
View File
@@ -1,3 +1,7 @@
(define hm/set! hash-map-set!)
(define hm/get hash-map-get)
(define-syntax (pe expr) `(print ',expr "evaluates to" ,expr))
(define-syntax (when condition . body) :doc "Special form for when multiple actions should be done if a\ncondition is true.\n\n{{{example_start}}}\n(when (not ())\n (print "Hello ")\n (print "from ")\n (print "when!"))\n\n(when ()\n (print "Goodbye ")\n (print "World!"))\n{{{example_end}}}\n" (if (= (rest body) ()) `(if ,condition (unquote-splicing body) nil) `(if ,condition (begin (unquote-splicing body)) nil)))
+25
View File
@@ -0,0 +1,25 @@
(define hm1 (create-hash-map))
(hm/set! hm1 1 "a")
(hm/set! hm1 "a" (lambda (x) (+ x 1)))
(assert (= ((hm/get hm1 (hm/get hm1 1)) 2) 3))
(define hm2 (create-hash-map))
(hm/set! hm2 'yes :yes)
(hm/set! hm2 :yes 'yes)
(assert (= (hm/get hm2 'yes) :yes))
(assert (= (hm/get hm2 :yes) 'yes))
(assert (= (hm/get hm2 (hm/get hm2 'yes)) 'yes))
(assert (= (hm/get hm2 (hm/get hm2 :yes)) :yes))
(assert (= (hm/get hm2 (hm/get hm2 (hm/get hm2 'yes))) :yes))
(assert (= (hm/get hm2 (hm/get hm2 (hm/get hm2 :yes))) 'yes))
(define hm3 (create-hash-map))
(hm/set! hm3 + 'plus)
(hm/set! hm3 - 'minus)
(assert (= (hm/get hm3 +) 'plus))
(assert (= (hm/get hm3 -) 'minus))