Stream: beginners

Topic: Help with Dict.update


view this post on Zulip Luke Boswell (Oct 24 2022 at 09:15):

Looking closer at Dict and I noticed Dict.update has a different signature than what I was expecting. Can someone help me understand why it takes a function that checks if something is present?

update : Dict k v, k, ([Present v, Missing] -> [Present v, Missing]) -> Dict k v | k has Eq

I thought the Dict.update would mean something similar to "update this dictionary so it now has X value, I don't care what was there before".

From what I can tell the use case for this might be if you decide something is already there and don't want to update it, but I feel like that is two separate actions so would be find to just use a Dict.get. The other option doesn't seem all that useful, when there is nothing there then leave it as is.

Maybe this is something to do with the Eq Ability?

So I guess what I was expecting was.

update : Dict k v, k, v -> Dict k v | k has Eq

view this post on Zulip Chris Duncan (Oct 24 2022 at 09:21):

The following function already exists:

insert : Dict k v, k, v -> Dict k v | k has Eq

view this post on Zulip Luke Boswell (Oct 24 2022 at 09:24):

Thank you, that makes more sense. That would also overwrite any value that is already there.

view this post on Zulip Folkert de Vries (Oct 24 2022 at 09:24):

the real use-case for update is performance. A combination of get and insert would have to find the correct spot twice, which is unfortunate

view this post on Zulip Folkert de Vries (Oct 24 2022 at 09:25):

in rust we have a bunch of uses of entry(key).or_default() and entry(key).or_insert(default_value)

view this post on Zulip Folkert de Vries (Oct 24 2022 at 09:26):

so you can provide a default value for a missing key, and then continue "piping"

view this post on Zulip Luke Boswell (Oct 24 2022 at 09:27):

Great, that makes sense. I'll add that in the doc comments.

view this post on Zulip Luke Boswell (Oct 24 2022 at 09:30):

@Folkert de Vries missing key, just to confirm you meant missing value?

view this post on Zulip Folkert de Vries (Oct 24 2022 at 09:39):

both?

view this post on Zulip Folkert de Vries (Oct 24 2022 at 09:39):

of the key is missing, you'd insert the key with a default value

view this post on Zulip Musab Nazir (May 22 2024 at 03:06):

I came across this func on the website docs and I see that this func is recommended for performance over doing a get and an insert separately. However I'm having trouble understanding the func signature and what I need to use the update func.

I have a Dict Str (List F32) and I need to just append to the list of floats in case I already have a key there. If its a new key just make a new list. Something like the following:

when Dict.get dict key is
    # existing k v in the map
    Ok values -> Dict.insert dict key (List.append values val)
    # new key so make new list
    Err _ -> Dict.insert dict key (List.single val)

I can't figure out how I can achieve this using the Dict.update.

Update: I think I got it. This is my current attempt (though I'd be lying if I said I completely understand it)

Dict.update dict key (addReading val)

addReading : F32 -> ([Present (List F32), Missing] -> [Present (List F32), Missing])
addReading = \val -> \possibleValue ->
        when possibleValue is
            Missing -> Present (List.single val)
            Present readings -> Present (List.append readings val)

Shoutout to @Luke Boswell and his AOC repo. I found some example uses


Last updated: Jul 06 2025 at 12:14 UTC