Skip to content

A peer-to-peer stack for code collaboration

3.4 KB
Raw Blame
{:module  'prelude/dict
 :doc     "Functions for manipualting dicts."
 :exports '[dict-from-seq keys values rekey modify-map delete-many lookup-default lookup-maybe safe-modify-map group-by]}

(import prelude/patterns :unqualified)

(def dict-from-seq
  "Creates a dictionary from a list of key-value pairs."
  (fn [xs]
    (foldr (fn [kv acc] (insert (first kv) (first (rest kv)) acc)) (dict) xs)))

(:test "dict-from-seq"
  [ (dict-from-seq (list (list :one 1) (list :two 2)))
    ==> { :one 1 :two 2 } ])

;; keys

(def keys
  "Given a dict `d`, returns a vector of its keys."
  (fn [d]
    (map first (seq d))))

(:test "keys"
  [ (keys {:one 1 :two 2}) ==> [:one :two] ]
)

(def values
  "Given a dict `d`, returns a vector of its values."
  (fn [d]
    (map (fn [x] (nth 1 x)) (seq d))))

(:test "values"
  [ (values {:a 0 :b 1}) ==> [0 1] ]
)

;; rekey
(def rekey
  "Change the key from `old-key` to `new-key` in a dict `d`. If `new-key` already exists, it is overwritten."
  (fn [old-key new-key d]
    (def old-val (lookup old-key d))
    (delete old-key (insert new-key old-val d))))

(:test "rekey"
  [ (rekey :a :b {:a 5}) ==> {:b 5} ])

;; modify-map
(def modify-map
  "Given a key `k`, a function `f` and a dict `d`, applies the function to the value associated to that key."
  (fn [k f d]
    (insert k (f (lookup k d)) d)))

(:test "modify-map"
  [ (modify-map :a (fn [x] (+ x 1)) {:a 5})
     ==> {:a 6} ])

(def delete-many
  "Delete several keys `ks` from a dict `d`."
  (fn [ks d]
    (foldr (fn [k d] (delete k d)) d ks)))

(def lookup-default
  "Like `lookup` but returns `default` if the key is not in the map."
  (fn [key default dict]
    (if (member? key dict)
      (lookup key dict)
      default)))

(def lookup-maybe
  "Like `lookup` but returns `[:just x]` if the key is not in the map and
  `:nothing` otherwise."
  (fn [key dict]
    (if (member? key dict)
      [:just (lookup key dict)]
      :nothing)))

(:test "lookup-default"
  [ (lookup-default :one 2 {:one 1}) ==> 1 ]
  [ (lookup-default :two 2 {:one 1}) ==> 2 ])

(def safe-modify-map
  "Modifies the association of a value to a key `k` in a dict `d`. The function
  `f` will receive `[:just v]` if `(eq? (lookup k d) v)`, otherwise it will
  receive `:nothing`. It should return `[:just new-v]` to change the value, and
  `:nothing` to remove it."
  (fn [k f d]
    (match (f (lookup-maybe k d))
           :nothing   (delete k d)
           [:just 'v] (insert k v d))))

(:test "safe-modify-map"
  [ (safe-modify-map :foo (fn [x] (match x :nothing [:just 0] [:just 'y] [:just (+ 1 y)])) {}) ==> {:foo 0} ]
  [ (safe-modify-map :foo (fn [x] (match x :nothing [:just 0] [:just 'y] [:just (+ 1 y)])) {:foo 42}) ==> {:foo 43} ]
  [ (safe-modify-map :foo (fn [x] (match x :nothing [:just 0] [:just _] :nothing)) {:foo 42}) ==> {}])

(def group-by
  "Partitions the values of a sequence `xs` according to the images under `f`. The
  partitions are returned in a dict keyed by the return value of `f`."
  (fn [f xs]
    (foldr (fn [x acc]
             (def k (f x))
             (match (lookup-maybe k acc)
                    [:just 'ys] (insert k (cons x ys) acc)
                    :nothing    (insert k [x] acc)))
           {}
           xs)))

(:test "group-by"
       [ (group-by length []) ==> {} ]
       [ (group-by length ["foo" "foofoo" "bar" "foobar" "bar" "barbar"]) ==> {3 ["foo" "bar" "bar"]
                                                                               6 ["foofoo" "foobar" "barbar"]} ]
       )