aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorinoas <mail@inoas.com>2024-05-26 12:24:38 +0200
committerLouis Pilfold <louis@lpil.uk>2024-06-02 14:05:26 +0100
commit44701419fddf00e0ce1ef69ad1c2e10498e9e1b9 (patch)
tree221c443a04cb52d8b6e087cc40550e6a466cf437 /src
parent9b76c9a66f49524941484c223b27f77d308f6376 (diff)
downloadgleam_stdlib-44701419fddf00e0ce1ef69ad1c2e10498e9e1b9.tar.gz
gleam_stdlib-44701419fddf00e0ce1ef69ad1c2e10498e9e1b9.zip
createupsert
Diffstat (limited to 'src')
-rw-r--r--src/gleam/dict.gleam22
1 files changed, 18 insertions, 4 deletions
diff --git a/src/gleam/dict.gleam b/src/gleam/dict.gleam
index 548e602..270efb5 100644
--- a/src/gleam/dict.gleam
+++ b/src/gleam/dict.gleam
@@ -439,7 +439,7 @@ pub fn drop(from dict: Dict(k, v), drop disallowed_keys: List(k)) -> Dict(k, v)
}
}
-/// Creates a new dict with one entry updated using a given function.
+/// Creates a new dict with one entry inserted or updated using a given function.
///
/// If there was not an entry in the dict for the given key then the function
/// gets `None` as its argument, otherwise it gets `Some(value)`.
@@ -455,14 +455,14 @@ pub fn drop(from dict: Dict(k, v), drop disallowed_keys: List(k)) -> Dict(k, v)
/// }
/// }
///
-/// update(dict, "a", increment)
+/// upsert(dict, "a", increment)
/// // -> from_list([#("a", 1)])
///
-/// update(dict, "b", increment)
+/// upsert(dict, "b", increment)
/// // -> from_list([#("a", 0), #("b", 0)])
/// ```
///
-pub fn update(
+pub fn upsert(
in dict: Dict(k, v),
update key: k,
with fun: fn(Option(v)) -> v,
@@ -474,6 +474,20 @@ pub fn update(
|> insert(dict, key, _)
}
+/// This function with this signature is deprecated.
+///
+/// In future this fuction will return an `Ok(Dict)` if the given key existed and
+/// thus could be updated in the `Dict` or an an `Error(Nil)` if the key was not found.
+///
+@deprecated("Use `upsert` instead")
+pub fn update(
+ in dict: Dict(k, v),
+ update key: k,
+ with fun: fn(Option(v)) -> v,
+) -> Dict(k, v) {
+ upsert(dict, key, fun)
+}
+
fn do_fold(list: List(#(k, v)), initial: acc, fun: fn(acc, k, v) -> acc) -> acc {
case list {
[] -> initial