Skip to content

Commit 6e63610

Browse files
committed
Mechanical refactor of Descr, avoid intermediate lists
1 parent 615751d commit 6e63610

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

lib/elixir/lib/module/types/descr.ex

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,7 +1103,7 @@ defmodule Module.Types.Descr do
11031103
[{name, [], arguments} | acc]
11041104
else
11051105
negs
1106-
|> Enum.map(fn {ty, lst} ->
1106+
|> map_or(fn {ty, lst} ->
11071107
args =
11081108
if subtype?(lst, @empty_list) do
11091109
[to_quoted(ty, opts)]
@@ -1113,7 +1113,6 @@ defmodule Module.Types.Descr do
11131113

11141114
{name, [], args}
11151115
end)
1116-
|> Enum.reduce(&{:or, [], [&2, &1]})
11171116
|> Kernel.then(
11181117
&[
11191118
{:and, [], [{name, [], arguments}, {:not, [], [&1]}]}
@@ -1853,10 +1852,10 @@ defmodule Module.Types.Descr do
18531852
end
18541853

18551854
# Use heuristics to normalize a map dnf for pretty printing.
1856-
defp map_normalize(dnf) do
1857-
dnf
1858-
|> Enum.reject(&map_empty?([&1]))
1859-
|> Enum.map(fn {tag, fields, negs} ->
1855+
defp map_normalize(dnfs) do
1856+
for dnf <- dnfs, not map_empty?([dnf]) do
1857+
{tag, fields, negs} = dnf
1858+
18601859
{fields, negs} =
18611860
Enum.reduce(negs, {fields, []}, fn neg = {neg_tag, neg_fields}, {acc_fields, acc_negs} ->
18621861
if map_empty_negation?(tag, acc_fields, neg) do
@@ -1874,7 +1873,7 @@ defmodule Module.Types.Descr do
18741873
end)
18751874

18761875
{tag, fields, negs}
1877-
end)
1876+
end
18781877
|> map_fusion()
18791878
end
18801879

@@ -1954,8 +1953,7 @@ defmodule Module.Types.Descr do
19541953

19551954
_ ->
19561955
negative_maps
1957-
|> Enum.map(&map_literal_to_quoted(&1, opts))
1958-
|> Enum.reduce(&{:or, [], [&2, &1]})
1956+
|> map_or(&map_literal_to_quoted(&1, opts))
19591957
|> Kernel.then(
19601958
&{:and, [], [map_literal_to_quoted({tag, positive_map}, opts), {:not, [], [&1]}]}
19611959
)
@@ -2308,8 +2306,7 @@ defmodule Module.Types.Descr do
23082306

23092307
_ ->
23102308
negative_tuples
2311-
|> Enum.map(&tuple_literal_to_quoted(&1, opts))
2312-
|> Enum.reduce(&{:or, [], [&2, &1]})
2309+
|> map_or(&tuple_literal_to_quoted(&1, opts))
23132310
|> Kernel.then(
23142311
&{:and, [], [tuple_literal_to_quoted({tag, positive_tuple}, opts), {:not, [], [&1]}]}
23152312
)
@@ -2574,8 +2571,9 @@ defmodule Module.Types.Descr do
25742571
none()
25752572
else
25762573
n..(m - 1)//1
2577-
|> Enum.map(&tuple_values(:closed, tuple_fill(elements, &1), negs))
2578-
|> Enum.reduce(none(), &union/2)
2574+
|> Enum.reduce(none(), fn i, acc ->
2575+
tuple_values(:closed, tuple_fill(elements, i), negs) |> union(acc)
2576+
end)
25792577
|> union(
25802578
if neg_tag == :open do
25812579
none()
@@ -2970,4 +2968,8 @@ defmodule Module.Types.Descr do
29702968
end
29712969

29722970
defp iterator_non_disjoint_intersection?(:none, _map), do: false
2971+
2972+
defp map_or([head | tail], fun) do
2973+
Enum.reduce(tail, fun.(head), &{:or, [], [&2, fun.(&1)]})
2974+
end
29732975
end

0 commit comments

Comments
 (0)