From 2ec40468a455c23b4d483623d5bcf2166f880d29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 31 Oct 2017 10:53:14 +0100 Subject: [PATCH] Inline expressions when possible --- lib/elixir/lib/access.ex | 7 +-- lib/elixir/lib/code.ex | 10 +--- lib/elixir/lib/code/identifier.ex | 78 ++++++++----------------------- lib/elixir/lib/enum.ex | 28 +++++------ 4 files changed, 34 insertions(+), 89 deletions(-) diff --git a/lib/elixir/lib/access.ex b/lib/elixir/lib/access.ex index 3ec724ebb4..253e6e3474 100644 --- a/lib/elixir/lib/access.ex +++ b/lib/elixir/lib/access.ex @@ -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 diff --git a/lib/elixir/lib/code.ex b/lib/elixir/lib/code.ex index 84993ec9c1..deff5afa86 100644 --- a/lib/elixir/lib/code.ex +++ b/lib/elixir/lib/code.ex @@ -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 diff --git a/lib/elixir/lib/code/identifier.ex b/lib/elixir/lib/code/identifier.ex index b3278ca4ff..ddf08805c7 100644 --- a/lib/elixir/lib/code/identifier.ex +++ b/lib/elixir/lib/code/identifier.ex @@ -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 diff --git a/lib/elixir/lib/enum.ex b/lib/elixir/lib/enum.ex index 27a7d4f40d..e651b21d1b 100644 --- a/lib/elixir/lib/enum.ex +++ b/lib/elixir/lib/enum.ex @@ -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)