diff options
Diffstat (limited to 'stdlib/source/lux/data/struct/dict.lux')
-rw-r--r-- | stdlib/source/lux/data/struct/dict.lux | 50 |
1 files changed, 30 insertions, 20 deletions
diff --git a/stdlib/source/lux/data/struct/dict.lux b/stdlib/source/lux/data/struct/dict.lux index 56ab6ca64..3b153d229 100644 --- a/stdlib/source/lux/data/struct/dict.lux +++ b/stdlib/source/lux/data/struct/dict.lux @@ -560,17 +560,20 @@ {#hash Hash<K> #root empty}) -(def: #export (put key val [Hash<K> node]) +(def: #export (put key val dict) (All [K V] (-> K V (Dict K V) (Dict K V))) - [Hash<K> (put' root-level (:: Hash<K> hash key) key val Hash<K> node)]) + (let [[Hash<K> node] dict] + [Hash<K> (put' root-level (:: Hash<K> hash key) key val Hash<K> node)])) -(def: #export (remove key [Hash<K> node]) +(def: #export (remove key dict) (All [K V] (-> K (Dict K V) (Dict K V))) - [Hash<K> (remove' root-level (:: Hash<K> hash key) key Hash<K> node)]) + (let [[Hash<K> node] dict] + [Hash<K> (remove' root-level (:: Hash<K> hash key) key Hash<K> node)])) -(def: #export (get key [Hash<K> node]) +(def: #export (get key dict) (All [K V] (-> K (Dict K V) (Maybe V))) - (get' root-level (:: Hash<K> hash key) key Hash<K> node)) + (let [[Hash<K> node] dict] + (get' root-level (:: Hash<K> hash key) key Hash<K> node))) (def: #export (contains? key table) (All [K V] (-> K (Dict K V) Bool)) @@ -615,29 +618,35 @@ kvs)) (do-template [<name> <elem-type> <side>] - [(def: #export <name> + [(def: #export (<name> dict) (All [K V] (-> (Dict K V) (List <elem-type>))) - (|>. entries (List/map <side>)))] + (|> dict entries (List/map <side>)))] [keys K product;left] [values V product;right] ) (def: #export (merge dict2 dict1) + {#;doc "Merges 2 dictionaries. + + If any collisions with keys occur, the values of dict2 will overwrite those of dict1."} (All [K V] (-> (Dict K V) (Dict K V) (Dict K V))) (List/fold (lambda [[key val] dict] (put key val dict)) dict1 (entries dict2))) -(def: #export (merge-with f dict1 dict2) +(def: #export (merge-with f dict2 dict1) + {#;doc "Merges 2 dictionaries. + + If any collisions with keys occur, a new value will be computed by applying 'f' to the values of dict2 and dict1."} (All [K V] (-> (-> V V V) (Dict K V) (Dict K V) (Dict K V))) - (List/fold (lambda [[key val] dict] + (List/fold (lambda [[key val2] dict] (case (get key dict) #;None - (put key val dict) + (put key val2 dict) - (#;Some val') - (put key (f val' val) dict))) + (#;Some val1) + (put key (f val2 val1) dict))) dict1 (entries dict2))) @@ -652,15 +661,16 @@ (remove from-key) (put to-key val)))) -(def: #export (select keys (^@ old-dict [Hash<K> _])) +(def: #export (select keys dict) {#;doc "Creates a sub-set of the given dict, with only the specified keys."} (All [K V] (-> (List K) (Dict K V) (Dict K V))) - (List/fold (lambda [key new-dict] - (case (get key old-dict) - #;None new-dict - (#;Some val) (put key val new-dict))) - (new Hash<K>) - keys)) + (let [[Hash<K> _] dict] + (List/fold (lambda [key new-dict] + (case (get key dict) + #;None new-dict + (#;Some val) (put key val new-dict))) + (new Hash<K>) + keys))) ## [Structures] (struct: #export (Eq<Dict> Eq<v>) (All [k v] (-> (Eq v) (Eq (Dict k v)))) |