Skip to content

Commit 5cee190

Browse files
author
José Valim
committed
Enum.join/2 and Enum.map_join/3 now uses iolists
1 parent 3496681 commit 5cee190

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

lib/elixir/lib/enum.ex

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -937,13 +937,13 @@ defmodule Enum do
937937

938938
def join(collection, joiner) when is_binary(joiner) do
939939
reduced = reduce(collection, :first, fn
940-
entry, :first -> to_string(entry)
941-
entry, acc -> acc <> joiner <> to_string(entry)
940+
entry, :first -> [enum_to_string(entry)]
941+
entry, acc -> [enum_to_string(entry), joiner|acc]
942942
end)
943943
if reduced == :first do
944944
""
945945
else
946-
reduced
946+
IO.chardata_to_string :lists.reverse(reduced)
947947
end
948948
end
949949

@@ -996,14 +996,14 @@ defmodule Enum do
996996

997997
def map_join(collection, joiner, mapper) when is_binary(joiner) do
998998
reduced = reduce(collection, :first, fn
999-
entry, :first -> to_string(mapper, entry)
1000-
entry, acc -> acc <> joiner <> to_string(mapper, entry)
999+
entry, :first -> [enum_to_string(mapper.(entry))]
1000+
entry, acc -> [enum_to_string(mapper.(entry)), joiner|acc]
10011001
end)
10021002

10031003
if reduced == :first do
10041004
""
10051005
else
1006-
reduced
1006+
IO.chardata_to_string :lists.reverse(reduced)
10071007
end
10081008
end
10091009

@@ -1839,7 +1839,7 @@ defmodule Enum do
18391839

18401840
## Helpers
18411841

1842-
@compile {:inline, to_string: 2}
1842+
@compile {:inline, enum_to_string: 1}
18431843

18441844
defp enumerate_and_count(collection, count) when is_list(collection) do
18451845
{collection, length(collection) - abs(count)}
@@ -1849,12 +1849,8 @@ defmodule Enum do
18491849
map_reduce(collection, -abs(count), fn(x, acc) -> {x, acc + 1} end)
18501850
end
18511851

1852-
defp to_string(mapper, entry) do
1853-
case mapper.(entry) do
1854-
x when is_binary(x) -> x
1855-
o -> String.Chars.to_string(o)
1856-
end
1857-
end
1852+
defp enum_to_string(entry) when is_binary(entry), do: entry
1853+
defp enum_to_string(entry), do: String.Chars.to_string(entry)
18581854

18591855
## Implementations
18601856

0 commit comments

Comments
 (0)