1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- (def h (hash %a 21 2 5 "c" 'c'))
- // testing hget
- (assert (== 21 (hget h %a)))
- (assert (== 5 (hget h 2)))
- (assert (== 'c' (hget h "c")))
- // default value
- (assert (== 0 (hget h 22 0)))
- // testing set
- (hset h 'a' 3)
- (assert (== 3 (hget h 'a')))
- // testing confict resolution
- // b and its symbol number have same hash value
- (hset h %b 13)
- (assert (== 13 (hget h %b)))
- // testing delete
- (hdel h %a)
- // %a should be gone
- (assert (== 0 (hget h %a 0)))
- (pretty true)
- (assert (== (str h)
- ` (hash
- 2:5
- c:'c'
- 'a':3
- b:13 )
- `))
- (hdel h (symnum %b))
- // b should still be in there
- (assert (== 13 (hget h %b)))
- (assert (hash? h))
- (assert (empty? (hash)))
- (assert (not (empty? h)))
- // clojure like field access
- (defmap ranch)
- (def lazy8 (ranch cowboy:"Jim" cowgirl:"Jane"))
- (assert (== (:cowboy lazy8) "Jim"))
- // not present should return default
- (assert (== (:joker lazy8 "gotaway") "gotaway"))
- // nested access with (->)
- // notice that we do field: while clojure does :field
- //
- (defmap bunkhouse)
- (hset lazy8 bunk1:(bunkhouse bed1:"Howie" bed2: "Mamie"))
- (assert (== (-> lazy8 bunk1: bed2:) "Mamie"))
- (defmap closet)
- (hset (:bunk1 lazy8) closet1:(closet broom:"Nimbuz2"))
- (assert (== (-> lazy8 bunk1: closet1: broom:) "Nimbuz2"))
- (assert (== (-> lazy8 bunk1:closet1:broom:) "Nimbuz2"))
- // delete and print should be okay
- (def h (hash key1:"val1" key2:222))
- (hget h key1:)
- (hset h key3:(list 5 6 7))
- (hget h key3:)
- h
- (hdel h key2:)
- h
- // hashes should accept arrays as indexes
- (hset h [0 0] %a)
- (hset h [0 1] %b)
- (hset h [1 0] %c)
- (hset h [1 1] %d)
- (assert (== {h[0 0]} %a))
- {h2=(hash [4 4]:%slicekey)}
- (hash [4 4]:%slicekey)
- // calling functions defined in hashes should work
- {h.b = (fn [] 42)}
- (assert (== (h.b) 42))
|