Inline expressions when possible

This commit is contained in:
José Valim
2017-10-31 10:53:14 +01:00
parent b3992aa574
commit 2ec40468a4
4 changed files with 34 additions and 89 deletions
+1 -6
View File
@@ -363,12 +363,7 @@ defmodule Access do
raise_undefined_behaviour(
exception,
module,
{
^module,
:get_and_update,
[^container, ^key, ^fun],
_
}
{^module, :get_and_update, [^container, ^key, ^fun], _}
)
end
+2 -8
View File
@@ -492,11 +492,7 @@ defmodule Code do
end
defp validate_aliases(kind, aliases) do
valid =
is_list(aliases) and
Enum.all?(aliases, fn {k, v} ->
is_atom(k) and is_atom(v)
end)
valid = is_list(aliases) and Enum.all?(aliases, fn {k, v} -> is_atom(k) and is_atom(v) end)
unless valid do
raise ArgumentError,
@@ -509,9 +505,7 @@ defmodule Code do
is_list(imports) and
Enum.all?(imports, fn {k, v} ->
is_atom(k) and is_list(v) and
Enum.all?(v, fn {name, arity} ->
is_atom(name) and is_integer(arity)
end)
Enum.all?(v, fn {name, arity} -> is_atom(name) and is_integer(arity) end)
end)
unless valid do
+20 -58
View File
@@ -13,17 +13,10 @@ defmodule Code.Identifier do
@spec unary_op(atom) :: {:non_associative, precedence :: pos_integer} | :error
def unary_op(op) do
cond do
op in [:&] ->
{:non_associative, 100}
op in [:!, :^, :not, :+, :-, :~~~] ->
{:non_associative, 300}
op in [:@] ->
{:non_associative, 320}
true ->
:error
op in [:&] -> {:non_associative, 100}
op in [:!, :^, :not, :+, :-, :~~~] -> {:non_associative, 300}
op in [:@] -> {:non_associative, 320}
true -> :error
end
end
@@ -39,53 +32,22 @@ defmodule Code.Identifier do
@spec binary_op(atom) :: {:left | :right, precedence :: pos_integer} | :error
def binary_op(op) do
cond do
op in [:<-, :\\] ->
{:left, 40}
op in [:when] ->
{:right, 50}
op in [:::] ->
{:right, 60}
op in [:|] ->
{:right, 70}
op in [:=] ->
{:right, 90}
op in [:||, :|||, :or] ->
{:left, 130}
op in [:&&, :&&&, :and] ->
{:left, 140}
op in [:==, :!=, :=~, :===, :!==] ->
{:left, 150}
op in [:<, :<=, :>=, :>] ->
{:left, 160}
op in [:|>, :<<<, :>>>, :<~, :~>, :<<~, :~>>, :<~>, :<|>, :^^^] ->
{:left, 170}
op in [:in] ->
{:left, 180}
op in [:++, :--, :.., :<>] ->
{:right, 200}
op in [:+, :-] ->
{:left, 210}
op in [:*, :/] ->
{:left, 220}
op in [:.] ->
{:left, 310}
true ->
:error
op in [:<-, :\\] -> {:left, 40}
op in [:when] -> {:right, 50}
op in [:::] -> {:right, 60}
op in [:|] -> {:right, 70}
op in [:=] -> {:right, 90}
op in [:||, :|||, :or] -> {:left, 130}
op in [:&&, :&&&, :and] -> {:left, 140}
op in [:==, :!=, :=~, :===, :!==] -> {:left, 150}
op in [:<, :<=, :>=, :>] -> {:left, 160}
op in [:|>, :<<<, :>>>, :<~, :~>, :<<~, :~>>, :<~>, :<|>, :^^^] -> {:left, 170}
op in [:in] -> {:left, 180}
op in [:++, :--, :.., :<>] -> {:right, 200}
op in [:+, :-] -> {:left, 210}
op in [:*, :/] -> {:left, 220}
op in [:.] -> {:left, 310}
true -> :error
end
end
+11 -17
View File
@@ -505,7 +505,7 @@ defmodule Enum do
@spec concat(t) :: t
def concat(enumerables) do
fun = &[&1 | &2]
reduce(enumerables, [], &reduce(&1, &2, fun)) |> :lists.reverse()
enumerables |> reduce([], &reduce(&1, &2, fun)) |> :lists.reverse()
end
@doc """
@@ -553,7 +553,7 @@ defmodule Enum do
value
{:error, module} ->
module.reduce(enumerable, {:cont, 0}, fn _, acc -> {:cont, acc + 1} end) |> elem(1)
enumerable |> module.reduce({:cont, 0}, fn _, acc -> {:cont, acc + 1} end) |> elem(1)
end
end
@@ -769,7 +769,8 @@ defmodule Enum do
value == 0
{:error, module} ->
module.reduce(enumerable, {:cont, true}, fn _, _ -> {:halt, false} end)
enumerable
|> module.reduce({:cont, true}, fn _, _ -> {:halt, false} end)
|> elem(1)
end
end
@@ -1120,18 +1121,14 @@ defmodule Enum do
@spec intersperse(t, element) :: list
def intersperse(enumerable, element) do
list =
reduce(enumerable, [], fn x, acc ->
[x, element | acc]
end)
enumerable
|> reduce([], fn x, acc -> [x, element | acc] end)
|> :lists.reverse()
# Head is a superfluous intersperser element
case list do
[] ->
[]
# Head is a superfluous intersperser element
[_ | t] ->
t
[] -> []
[_ | t] -> t
end
end
@@ -1841,11 +1838,8 @@ defmodule Enum do
def reduce(enumerable, fun) do
result =
Enumerable.reduce(enumerable, {:cont, :first}, fn
x, :first ->
{:cont, {:acc, x}}
x, {:acc, acc} ->
{:cont, {:acc, fun.(x, acc)}}
x, :first -> {:cont, {:acc, x}}
x, {:acc, acc} -> {:cont, {:acc, fun.(x, acc)}}
end)
|> elem(1)