Skip to content

Commit

Permalink
Merge pull request #3811 from michalmuskala/optimize_map
Browse files Browse the repository at this point in the history
Optimize Enum.map/2 for lists

Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
  • Loading branch information
josevalim authored and José Valim committed Oct 1, 2015
1 parent 448f473 commit 05fecbc
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/elixir/lib/enum.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1039,8 +1039,14 @@ defmodule Enum do
"""
@spec map(t, (element -> any)) :: list
def map(collection, fun) when is_list(collection) do
for item <- collection, 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(collection, fun) do
Expand Down

0 comments on commit 05fecbc

Please sign in to comment.