-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.lisp
59 lines (43 loc) · 1.66 KB
/
hash.lisp
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
;;; hash.lisp - Demonstrate working with hashes.
;; Create a hash, with some details
(set! person { :name "Steve"
:age (- 2022 1976)
:location "Helsinki"
})
(print "Keys of person: %s" (keys person))
(print "Values of person: %s" (vals person))
(if (contains? person :age)
(print "\tThe person has an age attribute"))
(if (contains? person ":location")
(print "\tThe person has an location attribute"))
(if (contains? person :cake)
(print "\tThe person has a cake preference"))
;; This function is used as a callback by apply-hash.
(set! hash-element (fn* (key val)
(print "KEY:%s VAL:%s" key (str val))))
;; The `apply-hash` function will trigger a callback for each key and value
;; within a hash.
;;
;; It is similar to the `apply` function which will apply a function to every
;; element of a lisp.
(apply-hash person hash-element)
;; Here we see a type-restriction, the following function can only be
;; invoked with a hash-argument.
(set! blah (fn* (h:hash) (print "Got argument of type %s" (type h))))
;; Call it
(blah person)
;; Use get/set to update the hash properties
(print "Original name: %s" (get person :name))
(set person :name "Bobby")
(print "Updated name: %s" (get person :name))
;; The (env) function returns a list of hashes, one for each value in
;; the environment.
;;
;; Here we filter the output to find any functions that match the
;; regular expression /int/
(set! out (filter (env) (lambda (x) (match "int" (get x :name)))))
;; Show the results
(print "Values in the environment matching the regexp /int/\n%s" out)
;;
(print "Function in env. matching regexp /int/:")
(apply out (lambda (x) (print "\t%s" (get x :name))))