diff options
Diffstat (limited to 'stdlib/source/lux/data/collection/dictionary.lux')
-rw-r--r-- | stdlib/source/lux/data/collection/dictionary.lux | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/stdlib/source/lux/data/collection/dictionary.lux b/stdlib/source/lux/data/collection/dictionary.lux index 195640d66..c4c8efeb1 100644 --- a/stdlib/source/lux/data/collection/dictionary.lux +++ b/stdlib/source/lux/data/collection/dictionary.lux @@ -4,6 +4,9 @@ [hash (#+ Hash)] [equivalence (#+ Equivalence)] [functor (#+ Functor)]] + [control + ["." try (#+ Try)] + ["." exception (#+ exception:)]] [data ["." maybe] ["." product] @@ -569,12 +572,14 @@ #.None #0 (#.Some _) #1)) +(exception: #export key-already-exists) + (def: #export (try-put key val dict) {#.doc "Only puts the KV-pair if the key is not already present."} - (All [k v] (-> k v (Dictionary k v) (Dictionary k v))) + (All [k v] (-> k v (Dictionary k v) (Try (Dictionary k v)))) (case (get key dict) - #.None (put key val dict) - (#.Some _) dict)) + #.None (#try.Success (put key val dict)) + (#.Some _) (exception.throw ..key-already-exists []))) (def: #export (update key f dict) {#.doc "Transforms the value located at key (if available), using the given function."} |