(examples-for hash-replace-keys

("changes strings to symbols"
 (let h (hash-replace-keys λk(sym k) { "a" 1 "b" 2 "c" 3 })
   (list (hash-keys h) (hash-values h)))
 ((a b c) (1 2 3)))

("appends a character to keys"
 (let h (hash-replace-keys λk(string-pieces k "!") { "a" 1 "b" 2 "c" 3 })
   (list (hash-keys h) (hash-values h)))
 (("a!" "b!" "c!") (1 2 3))))

(examples-for hash

("builds a hash"
 (let h (hash "a" 1 'b 2 '(x y) 3)
   (list (hash-keys h) (hash-values h)))
 (("a" b (x y)) (1 2 3))))

(examples-for hash-values

("returns the list of values of the given hash"
 (hash-values { a 1 b 2 c 'c d "hello" })
 (1 2 c "hello")))

(examples-for hash?

("nil for a cons"    (hash? '(1 a 2 b 3 c))  nil)
("nil for a number"  (hash? 1)               nil)
("nil for a string"  (hash? "werwe")         nil)
("  t for a hash"    (hash? {a 1 b 2})       t  )
("nil for a symbol"  (hash? 'foo)            nil))

(examples-for hash-to-array

("returns empty list for empty hash"
 (hash-to-array {})
 ())

("returns one-element list for single-element hash"
 (hash-to-array { a 1})
 ((a 1)))

("returns a list of two-element lists"
 (hash-to-array { a 1 b 2 c 3 })
 ((a 1) (b 2) (c 3))))

(examples-for map-hash

("converts a hash to a list of objects"
 (to-string:map-hash (fn (k v) { name k price v }) { a 1 b 2 c 3 })
 "({:name=>:a, :price=>1} {:name=>:b, :price=>2} {:name=>:c, :price=>3})"))

(examples-for hash-slice

("returns a new hash containing only the specified keys"
 (let h (hash-slice { a 1 b 2 c 3 d 4 } '(a b e f))
   (list (hash-keys h) (hash-values h)))
 ((a b) (1 2))))

(examples-for hash-merge

("merge with symbol keys"
 (let h (hash-merge { a 1 b 2 c 3 } { a 99 c 98 d 97 })
   (list h.a h.b h.c h.d (hash-keys h) ))
 (99 2 98 97 (a b c d )))

("merge with mixed symbol and string and int keys"
 (let h (hash-merge { a 1 "b" 2 3 'c } { a 99 b 98 3 "foo" })
   (cons (hash-keys h) (mapx (hash-keys h) k h.,k)))
 ((a "b" 3 b) 99 2 "foo" 98)))

(examples-for hash-keys

("returns nil for a number"  (hash-keys 1)                 nil)
("returns nil for nil"       (hash-keys nil)               nil)
("returns nil for a string"  (hash-keys "hello")           nil)
("returns nil for a symbol"  (hash-keys 'lipstick)         nil)
("returns nil for a date"    (hash-keys (date 2015 11 18)) nil)

;; ("returns date operations"
;;  (sort:hash-keys (date 2015 11 18))
;;  (age
;;   beginning_of_month
;;   beginning_of_week
;;   beginning_of_year
;;   day
;;   end_of_month
;;   end_of_week
;;   end_of_year
;;   friday?
;;   last_month
;;   last_week
;;   last_year
;;   monday?
;;   month
;;   next_month
;;   next_week
;;   next_year
;;   saturday?
;;   sunday?
;;   thursday?
;;   tomorrow
;;   tuesday?
;;   wednesday?
;;   week_day
;;   year
;;   yesterday))

("returns keys of a given hash"
 (hash-keys {a 1 b 2 c { x 99 y 98 z 97 }})
 (a b c)))

(examples-for hash-key?

("detects key presence"
 (hash-key? { foo 1 bar 2 } 'foo)
 t)

("detects key absence"
 (hash-key? { foo 1 bar 2 } 'zed)
 nil))

(examples-for hash-cons

("is useful for storing a list in a hash"
 (let h {}
   (hash-cons h 'foo 1)
   (hash-cons h 'foo 2)
   (hash-cons h 'foo 3)
   h.foo)
 (3 2 1)))

(examples-for brace-list

("with no args builds an empty hash"
 (let thing {}
   (list (type-of thing) (hash-keys thing)))
 (hash nil))

("build a hash table from brace-list syntax"
 (let hsh { foo 1 bar 2 }
   (list 'foo hsh.foo 'bar hsh.bar))
 (foo 1 bar 2))

("single-item brace list is just the thing itself"
 (let zi 10 "finds the ~{zi}th item")
 "finds the 10th item")

("unquotes hash keys"
 (with (zi 'foo chi 'bar yi 'grr)
       (let hsh { ,zi 10 ,chi 11 ,yi 12 }
         (list zi hsh.foo chi hsh.bar yi hsh.grr)))
 (foo 10 bar 11 grr 12))

("allows literal and invocation hash keys"
 (with (zi "hello" chi "world")
       (let hsh { (joinstr " " zi chi) 10 "yesterday" 11 }
         (list "hello world" (hash-get hsh "hello world") "yesterday" (hash-get hsh "yesterday"))))
 ("hello world" 10 "yesterday" 11)))

(examples-for hash-transform-values

("returns a new hash with the same keys as the given hash, where each corresponding value is transformed by the given transform function"
 (let h { a 1 b 2 c 3 d 4 }
   (to-list (hash-transform-values λkvi(* v v) h)))
 ((a 1) (b 4) (c 9) (d 16))))