Stream: beginners

Topic: Creating a Dict from a List


view this post on Zulip Luke Boswell (Oct 25 2022 at 06:39):

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.

view this post on Zulip Luke Boswell (Oct 25 2022 at 06:43):

Maybe something like

list1 = Dict.fromList [Pair 1 "one", Pair 2 "two"]

# OR

list2 = @Dict [Pair 1 "Keep Me", Pair 2 "And Me"]

view this post on Zulip Brian Carroll (Oct 25 2022 at 06:46):

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.

view this post on Zulip Luke Boswell (Oct 25 2022 at 07:07):

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

view this post on Zulip Brendan Hansknecht (Oct 25 2022 at 13:48):

Interesting, I feel like that function used to exist.

view this post on Zulip Brendan Hansknecht (Oct 25 2022 at 13:49):

Oh, set has fromList, but dict doesn't. That's probably why I thought it existed

view this post on Zulip Richard Feldman (Oct 25 2022 at 18:24):

this might be because we don't have tuples yet :big_smile:

view this post on Zulip Richard Feldman (Oct 25 2022 at 18:24):

which would be the obvious choices for what should go in that List

view this post on Zulip Richard Feldman (Oct 25 2022 at 18:25):

we could do List [T key value] for now

view this post on Zulip Brendan Hansknecht (Oct 25 2022 at 18:59):

I guess when I update dict to be hash based, I can fold that in.

view this post on Zulip Richard Feldman (Oct 25 2022 at 19:36):

niiiiiice - that's unblocked now, right?

view this post on Zulip Richard Feldman (Oct 25 2022 at 19:36):

since Ayaz landed Eq & Hash

view this post on Zulip Brendan Hansknecht (Oct 25 2022 at 20:19):

Yep

view this post on Zulip Richard Feldman (Oct 25 2022 at 20:34):

so exciting!!!


Last updated: Jul 06 2025 at 12:14 UTC