Skip to content

Commit 8b6ec66

Browse files
committed
Inline List.flatten/1 and List.flatten/2
1 parent 82a60e6 commit 8b6ec66

File tree

6 files changed

+16
-14
lines changed

6 files changed

+16
-14
lines changed

lib/elixir/lib/list.ex

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ defmodule List do
194194
195195
Empty list elements are discarded.
196196
197+
Inlined by the compiler.
198+
197199
## Examples
198200
199201
iex> List.flatten([1, [[2], 3]])
@@ -204,9 +206,7 @@ defmodule List do
204206
205207
"""
206208
@spec flatten(deep_list) :: list when deep_list: [any | deep_list]
207-
def flatten(list) do
208-
:lists.flatten(list)
209-
end
209+
defdelegate flatten(list), to: :lists
210210

211211
@doc """
212212
Flattens the given `list` of nested lists.
@@ -216,6 +216,8 @@ defmodule List do
216216
Empty list elements from `list` are discarded,
217217
but not the ones from `tail`.
218218
219+
Inlined by the compiler.
220+
219221
## Examples
220222
221223
iex> List.flatten([1, [[2], 3]], [4, 5])
@@ -226,9 +228,7 @@ defmodule List do
226228
227229
"""
228230
@spec flatten(deep_list, [elem]) :: [elem] when elem: var, deep_list: [elem | deep_list]
229-
def flatten(list, tail) do
230-
:lists.flatten(list, tail)
231-
end
231+
defdelegate flatten(list, tail), to: :lists
232232

233233
@doc """
234234
Folds (reduces) the given list from the left with

lib/elixir/src/elixir_rewrite.erl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ inline(Mod, Fun, Arity) -> inner_inline(ex_to_erl, Mod, Fun, Arity).
139139
?inline(?kernel, trunc, 1, erlang, trunc);
140140
?inline(?kernel, tuple_size, 1, erlang, tuple_size);
141141

142+
?inline(?list, flatten, 1, lists, flatten);
143+
?inline(?list, flatten, 2, lists, flatten);
142144
?inline(?list, to_atom, 1, erlang, list_to_atom);
143145
?inline(?list, to_existing_atom, 1, erlang, list_to_existing_atom);
144146
?inline(?list, to_float, 1, erlang, list_to_float);

lib/elixir/test/elixir/code_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ defmodule CodeTest do
467467

468468
test "disables tail call optimization at the root" do
469469
try do
470-
Code.compile_string("List.flatten(123)")
470+
Code.compile_string("List.last(123)")
471471
rescue
472472
_ -> assert Enum.any?(__STACKTRACE__, &match?({_, :__FILE__, 1, _}, &1))
473473
end

lib/elixir/test/elixir/kernel/expansion_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,8 +1134,8 @@ defmodule Kernel.ExpansionTest do
11341134
end
11351135

11361136
test "expands remotes" do
1137-
assert expand(quote(do: &List.flatten/2)) ==
1138-
quote(do: &:"Elixir.List".flatten/2)
1137+
assert expand(quote(do: &List.first/2)) ==
1138+
quote(do: &:"Elixir.List".first/2)
11391139
|> clean_meta([:imports, :context])
11401140

11411141
assert expand(quote(do: &Kernel.is_atom/1)) ==

lib/elixir/test/elixir/kernel/fn_test.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ defmodule Kernel.FnTest do
9090

9191
test "capture with variable module" do
9292
mod = List
93-
assert (&mod.flatten(&1)).([1, [2], 3]) == [1, 2, 3]
94-
assert (&mod.flatten/1).([1, [2], 3]) == [1, 2, 3]
95-
assert (&mod.flatten/1) == (&List.flatten/1)
93+
assert (&mod.first(&1)).([1, 2, 3]) == 1
94+
assert (&mod.first/1).([1, 2, 3]) == 1
95+
assert (&mod.first/1) == (&List.first/1)
9696
end
9797

9898
test "local partial application" do

lib/elixir/test/erlang/function_test.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ function_call_with_parens_args_and_nested_when_test() ->
120120
%% Partial application
121121

122122
require_partial_application_test() ->
123-
{Fun, _} = eval("&List.flatten(&1)"),
124-
Fun = fun 'Elixir.List':flatten/1.
123+
{Fun, _} = eval("&List.first(&1)"),
124+
Fun = fun 'Elixir.List':first/1.
125125

126126
import_partial_application_test() ->
127127
{Fun, _} = eval("&is_atom(&1)"),

0 commit comments

Comments
 (0)