Is there a way to create a Dict
from a List
?
I think this is something I would find very useful particularly for creating expectations to test functions.
Maybe something like
list1 = Dict.fromList [Pair 1 "one", Pair 2 "two"]
# OR
list2 = @Dict [Pair 1 "Keep Me", Pair 2 "And Me"]
Wow yeah we don't appear to have that, I'm surprised! The first form would be more like what we'd support. I'd guess the only reason we don't have it is because we don't have tuple syntax yet so you have to pick a tag name like Pair
.
You could do it with a List.walk
that starts with Dict.empty
and does a Dict.insert
in the callback function.
I've been adding examples to the Docs. So i'm currently doing things like this, which works, but maybe could be neater.
first =
Dict.single 1 "Keep Me"
|> Dict.insert 2 "And Me"
second =
Dict.single 1 "Not Me"
|> Dict.insert 3 "Me Too"
|> Dict.insert 4 "And Also Me"
expected =
Dict.single 1 "Keep Me"
|> Dict.insert 2 "And Me"
|> Dict.insert 3 "Me Too"
|> Dict.insert 4 "And Also Me"
expect
Dict.insertAll first second == expected
Interesting, I feel like that function used to exist.
Oh, set has fromList
, but dict doesn't. That's probably why I thought it existed
this might be because we don't have tuples yet :big_smile:
which would be the obvious choices for what should go in that List
we could do List [T key value]
for now
I guess when I update dict to be hash based, I can fold that in.
niiiiiice - that's unblocked now, right?
since Ayaz landed Eq & Hash
Yep
so exciting!!!
Last updated: Jul 06 2025 at 12:14 UTC