Question

I'm trying to test an algorithm that builds up a HashDict, but I can't get my "expected" to actually equal my "actual." For example,

iex> expected = HashDict.new([ key: 12 ])
#HashDict<[foo: 12]>

iex> actual = Dict.put(HashDict.new, "key", 12)
#HashDict<[{"foo", 12}]>

I can get the second "style" of HashDict in a literal by declaring it in a much more obtuse way:

iex> HashDict.new([ { "key", 12 } ])
#HashDict<[{"foo", 12}]>

I really like the simple Dict literal syntax for my test case, but my implementation builds up the Dict recursively. How can I make these equal?

Was it helpful?

Solution

In the first case the key is an atom, but in the second case it is a string.

You can do this on iex:

expected = HashDict.new([key: 12])
actual = Dict.put(HashDict.new, :key, 12)
Dict.equals? actual, expected # returns true

For more information visit: http://elixir-lang.org/docs/stable/

OTHER TIPS

This is an old question (HashDict has been deprecated for quite a few Elixir releases at this point), but I thought I'd update it for current Elixir releases in case anyone else finds it in the future.

Instead of HashDict, you should be using Map these days.

The Map literal syntax can support strings as keys like this:

%{"foo" => 12}

This is, of course, different from using an atom key in a Map literal, which is idiomatically declared like this:

%{foo: 12}

That's just syntactic sugar around declaring it like this:

%{ :foo => 12 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top