Skip to content

Commit f1b2980

Browse files
author
José Valim
committed
Merge pull request #979 from meh/enum-to_list
Implement Enum.to_list, closes #977
2 parents 52f36a4 + 8a4b550 commit f1b2980

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

lib/elixir/lib/enum.ex

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,33 @@ defmodule Enum do
884884
end
885885
end
886886

887+
@doc """
888+
Convert `collection` to a list.
889+
890+
## Examples
891+
892+
iex> Enum.to_list(1 .. 3)
893+
[1, 2, 3]
894+
895+
"""
896+
@spec to_list(t) :: [term]
897+
def to_list(collection) when is_list collection do
898+
collection
899+
end
900+
901+
def to_list(collection) do
902+
case I.iterator(collection) do
903+
{ _iterator, :stop } ->
904+
[]
905+
906+
{ iterator, pointer } ->
907+
do_to_list(pointer, iterator)
908+
909+
list ->
910+
list
911+
end
912+
end
913+
887914
@doc """
888915
Iterates the enumerable removing all duplicated items.
889916
@@ -932,14 +959,6 @@ defmodule Enum do
932959
defp iterator(collection) when is_list(collection), do: collection
933960
defp iterator(collection), do: I.iterator(collection)
934961

935-
defp to_list({ h, next }, iterator) do
936-
[h|to_list(iterator.(next), iterator)]
937-
end
938-
939-
defp to_list(:stop, _) do
940-
[]
941-
end
942-
943962
defp iterate_and_count(collection, count) do
944963
{ list, total_items } = do_iterate_and_count(collection)
945964
{ list, max(0, total_items - abs(count)) }
@@ -1068,7 +1087,7 @@ defmodule Enum do
10681087
end
10691088

10701089
defp do_drop(extra, iterator, 0) do
1071-
to_list(extra, iterator)
1090+
do_to_list(extra, iterator)
10721091
end
10731092

10741093
defp do_drop(:stop, _, _) do
@@ -1093,7 +1112,7 @@ defmodule Enum do
10931112
if fun.(h) do
10941113
do_drop_while(iterator.(next), iterator, fun)
10951114
else
1096-
to_list(extra, iterator)
1115+
do_to_list(extra, iterator)
10971116
end
10981117
end
10991118

@@ -1485,7 +1504,7 @@ defmodule Enum do
14851504
end
14861505

14871506
defp do_split(extra, iterator, 0, acc) do
1488-
{ :lists.reverse(acc), to_list(extra, iterator) }
1507+
{ :lists.reverse(acc), do_to_list(extra, iterator) }
14891508
end
14901509

14911510
defp do_split(:stop, _, _, acc) do
@@ -1510,7 +1529,7 @@ defmodule Enum do
15101529
if fun.(h) do
15111530
do_split_while(iterator.(next), iterator, fun, [h|acc])
15121531
else
1513-
{ :lists.reverse(acc), to_list(extra, iterator) }
1532+
{ :lists.reverse(acc), do_to_list(extra, iterator) }
15141533
end
15151534
end
15161535

@@ -1574,6 +1593,16 @@ defmodule Enum do
15741593
[]
15751594
end
15761595

1596+
## to_list
1597+
1598+
defp do_to_list({ h, next }, iterator) do
1599+
[h | do_to_list(iterator.(next), iterator)]
1600+
end
1601+
1602+
defp do_to_list(:stop, _) do
1603+
[]
1604+
end
1605+
15771606
## uniq
15781607

15791608
defp do_uniq([h|t], acc) do

lib/elixir/test/elixir/enum_test.exs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,11 @@ defmodule EnumTest.List do
240240
assert Enum.take_while([], fn(_) -> true end) == []
241241
end
242242

243+
test :to_list do
244+
assert Enum.to_list([]) == []
245+
assert Enum.to_list(1 .. 3) == [1, 2, 3]
246+
end
247+
243248
test :uniq do
244249
assert Enum.uniq([1,2,3,2,1]) == [1,2,3]
245250
end

0 commit comments

Comments
 (0)