Skip to content
This repository
tree: b48876c832
Fetching contributors…

Cannot retrieve contributors at this time

file 41 lines (26 sloc) 1.154 kb
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

###########
# hashmaps!
###########

addHash k v hashmap = cons (cons k v) (removeHash k hashmap)

hashFromList l = if (null? l) nil
  addHash (head l) (head (tail l)) (hashFromList (tail (tail l)))

key cons = head cons
value cons = tail cons

getPair k hashmap = findIf (\x . eq (head x) k) hashmap

# getPairOpt returns a 2Valued option
getPairOpt k l = l (\h t D . h (\kk vv . (eq k kk) (some2 kk vv) (getPairOpt k t))) none

getHashValue k hashmap = (\pair. if (eq pair nil) nil (value pair)) (getPair k hashmap)

getHashValueDefault k default hashmap = do
  val = getHashValue k hashmap
  if (neq val nil) val default


getKeys hashmap = map (\cell . (key cell)) hashmap
numKeys hashmap = length (getKeys hashmap)


firstPair hashmap = head hashmap
restPairs hashmap = tail hashmap

mergeHashKeys hm1 hm2 keys = if (null? keys) hm1
  if (null? (getPair (head keys) hm1) )
    cons (getPair (head keys) hm2) (mergeHashKeys hm1 hm2 (tail keys))
    mergeHashKeys hm1 hm2 (tail keys)

# keys in hm2 take precedence over keys in hm1
mergeHash hm1 hm2 = mergeHashKeys hm1 hm2 (getKeys hm2)

removeHash k hashmap = removeIf (\x . eq (key x) k) hashmap
Something went wrong with that request. Please try again.