diff options
author | Michal Muskala <michal@muskala.eu> | 2015-10-01 00:54:59 +0200 |
---|---|---|
committer | Michal Muskala <michal@muskala.eu> | 2015-10-01 00:56:07 +0200 |
commit | ed666461872a71a019cc01275fca1682487b7d60 (patch) | |
tree | ee6cef907c655c314f6a0909d750c9a606071105 | |
parent | f70192bb6e5009e8533e60252f25f48e24b82d3b (diff) | |
download | elixir-ed666461872a71a019cc01275fca1682487b7d60.tar.gz |
Optimize Enum.map/2 for lists
Out of 3 options:
* using for comprehensions
* using explicit recursion with accumulator
* using simple consing
The third option seems to be the fastest. That's also the one that
is used by :lists.map/2
-rw-r--r-- | lib/elixir/lib/enum.ex | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/lib/elixir/lib/enum.ex b/lib/elixir/lib/enum.ex index 5694bad40..0eafe97f1 100644 --- a/lib/elixir/lib/enum.ex +++ b/lib/elixir/lib/enum.ex @@ -1075,8 +1075,14 @@ defmodule Enum do """ @spec map(t, (element -> any)) :: list - def map(enumerable, fun) when is_list(enumerable) do - for item <- enumerable, do: fun.(item) + def map(enumerable, fun) + + def map([], _fun) do + [] + end + + def map([head | tail], fun) do + [fun.(head) | map(tail, fun)] end def map(enumerable, fun) do |