Skip to content

Commit 5761460

Browse files
committed
use map_size guard, add test
1 parent d881d89 commit 5761460

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

lib/elixir/lib/enum.ex

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -838,16 +838,15 @@ defmodule Enum do
838838
list ++ to_list(collection)
839839
end
840840

841+
def into(collection, %{} = map) when is_list(collection) and (map_size(map) == 0) do
842+
:maps.from_list(collection)
843+
end
844+
841845
def into(collection, collectable) do
842-
case is_list(collection) and is_map(collectable) and (:maps.size(collectable) == 0) do
843-
true ->
844-
:maps.from_list(collection)
845-
false ->
846-
{ initial, fun } = Collectable.into(collectable)
847-
into(collection, initial, fun, fn x, acc ->
848-
fun.(acc, { :cont, x })
849-
end)
850-
end
846+
{ initial, fun } = Collectable.into(collectable)
847+
into(collection, initial, fun, fn x, acc ->
848+
fun.(acc, { :cont, x })
849+
end)
851850
end
852851

853852
@doc """

lib/elixir/test/elixir/enum_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ defmodule EnumTest.List do
187187

188188
test :into do
189189
assert Enum.into([a: 1, b: 2], %{}) == %{a: 1, b: 2}
190+
assert Enum.into([a: 1, b: 2], %{c: 3}) == %{a: 1, b: 2, c: 3}
190191
assert Enum.into(%{a: 1, b: 2}, []) == [a: 1, b: 2]
191192
assert Enum.into([1, 2, 3], "numbers: ", &to_string/1) == "numbers: 123"
192193
assert Enum.into([1, 2, 3], fn

0 commit comments

Comments
 (0)