Skip to content

Commit c0547a1

Browse files
author
José Valim
committed
Ensure Enum.to_list and Dict.to_list return exactly the same result
1 parent 3918b88 commit c0547a1

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed

lib/elixir/lib/hash_dict.ex

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ defmodule HashDict do
227227
end
228228

229229
def to_list(dict) do
230-
dict_fold(dict, [], [&1|&2])
230+
dict_fold(dict, [], [&1|&2]) |> :lists.reverse
231231
end
232232

233233
@doc """
@@ -393,29 +393,25 @@ defmodule HashDict do
393393
end
394394

395395
defp dict_equal?(dict1, dict2) do
396-
fun = fn({ key, value }, acc) ->
397-
case fetch(dict2, key) do
398-
{ _ok, ^value } ->
399-
acc
400-
{ _ok, _other} ->
401-
throw(:error)
402-
:error ->
403-
throw(:error)
404-
end
405-
end
406396
try do
407-
reduce(dict1, true, fun)
397+
reduce(dict1, true, fn({ key, value }, acc) ->
398+
case fetch(dict2, key) do
399+
{ _ok, ^value } ->
400+
acc
401+
_ ->
402+
throw(:error)
403+
end
404+
end)
408405
catch
409-
:error ->
410-
false
406+
:error -> false
411407
end
412408
end
413409

414410
## Bucket helpers
415411

416412
# Get value from the bucket
417413
defp bucket_get([{k,_}|_bucket], key) when k > key do
418-
:false
414+
false
419415
end
420416

421417
defp bucket_get([{key,_}=e|_bucket], key) do
@@ -427,7 +423,7 @@ defmodule HashDict do
427423
end
428424

429425
defp bucket_get([], _key) do
430-
:false
426+
false
431427
end
432428

433429
# Puts a value in the bucket

lib/elixir/test/elixir/hash_dict_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,13 @@ defmodule HashDictTest do
111111
list = dict |> HashDict.to_list
112112
assert length(list) == 20
113113
assert { 1, 1 } in list
114-
assert :lists.keysort(1, list) == :lists.keysort(1, Enum.to_list(dict))
114+
assert list == Enum.to_list(dict)
115115

116116
dict = filled_dict(120)
117117
list = dict |> HashDict.to_list
118118
assert length(list) == 120
119119
assert { 1, 1 } in list
120-
assert :lists.keysort(1, list) == :lists.keysort(1, Enum.to_list(dict))
120+
assert list == Enum.to_list(dict)
121121
end
122122

123123
test :keys do

0 commit comments

Comments
 (0)