Skip to content

Commit e61ed47

Browse files
committed
Deprecate map/filter/reject in Map and Keyword
1 parent 33f9d04 commit e61ed47

File tree

2 files changed

+42
-171
lines changed

2 files changed

+42
-171
lines changed

lib/elixir/lib/keyword.ex

Lines changed: 12 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,84 +1317,21 @@ defmodule Keyword do
13171317
length(keywords)
13181318
end
13191319

1320-
@doc """
1321-
Returns a keyword list containing only the entries from `keywords`
1322-
for which the function `fun` returns a truthy value.
1323-
1324-
See also `reject/2` which discards all entries where the function
1325-
returns a truthy value.
1326-
1327-
## Examples
1328-
1329-
iex> Keyword.filter([one: 1, two: 2, three: 3], fn {_key, val} -> rem(val, 2) == 1 end)
1330-
[one: 1, three: 3]
1331-
1332-
"""
1333-
@doc since: "1.13.0"
1334-
@spec filter(t, ({key, value} -> as_boolean(term))) :: t
1335-
def filter(keywords, fun) when is_list(keywords) and is_function(fun, 1) do
1336-
do_filter(keywords, fun)
1337-
end
1338-
1339-
defp do_filter([], _fun), do: []
1340-
1341-
defp do_filter([{_, _} = entry | entries], fun) do
1342-
if fun.(entry) do
1343-
[entry | do_filter(entries, fun)]
1344-
else
1345-
do_filter(entries, fun)
1346-
end
1347-
end
1348-
1349-
@doc """
1350-
Returns a keyword list excluding the entries from `keywords`
1351-
for which the function `fun` returns a truthy value.
1352-
1353-
See also `filter/2`.
1354-
1355-
## Examples
1356-
1357-
iex> Keyword.reject([one: 1, two: 2, three: 3], fn {_key, val} -> rem(val, 2) == 1 end)
1358-
[two: 2]
1359-
1360-
"""
1361-
@doc since: "1.13.0"
1362-
@spec reject(t, ({key, value} -> as_boolean(term))) :: t
1363-
def reject(keywords, fun) when is_list(keywords) and is_function(fun, 1) do
1364-
do_reject(keywords, fun)
1365-
end
1366-
1367-
defp do_reject([], _fun), do: []
1368-
1369-
defp do_reject([{_, _} = entry | entries], fun) do
1370-
if fun.(entry) do
1371-
do_reject(entries, fun)
1372-
else
1373-
[entry | do_reject(entries, fun)]
1374-
end
1320+
@doc false
1321+
@deprecated "Use Enum.filter/2 instead"
1322+
def filter(keywords, fun) when is_list(keywords) do
1323+
Enum.filter(keywords, fun)
13751324
end
13761325

1377-
@doc """
1378-
Maps the function `fun` over all key-value pairs in `keywords`,
1379-
returning a keyword list with all the values replaced with
1380-
the result of the function.
1381-
1382-
## Examples
1383-
1384-
iex> Keyword.map([one: 1, two: 2, three: 3], fn {_key, val} -> to_string(val) end)
1385-
[one: "1", two: "2", three: "3"]
1386-
1387-
"""
1388-
@doc since: "1.13.0"
1389-
@spec map(t, ({key, value} -> value)) :: t
1390-
def map(keywords, fun) when is_list(keywords) and is_function(fun, 1) do
1391-
do_map(keywords, fun)
1326+
@doc false
1327+
@deprecated "Use Keyword.new/2 instead"
1328+
def reject(keywords, fun) when is_list(keywords) do
1329+
Enum.reject(keywords, fun)
13921330
end
13931331

1394-
defp do_map([], _fun), do: []
1395-
1396-
defp do_map([{key, value} | rest], fun) do
1397-
new_value = fun.({key, value})
1398-
[{key, new_value} | do_map(rest, fun)]
1332+
@doc false
1333+
@deprecated "Use Keyword.new/2 instead"
1334+
def map(keywords, fun) when is_list(keywords) do
1335+
Enum.map(keywords, fn {k, v} -> {k, fun.({k, v})} end)
13991336
end
14001337
end

lib/elixir/lib/map.ex

Lines changed: 30 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,24 @@ defmodule Map do
214214
215215
"""
216216
@spec new(Enumerable.t(), (term -> {key, value})) :: map
217-
def new(enumerable, transform) when is_function(transform, 1) do
217+
def new(enumerable, transform)
218+
def new(%_{} = enumerable, transform), do: new_from_enum(enumerable, transform)
219+
def new(%{} = map, transform), do: new_from_map(map, transform)
220+
def new(enumerable, transform), do: new_from_enum(enumerable, transform)
221+
222+
defp new_from_map(map, transform) when is_function(transform, 1) do
223+
iter = :maps.iterator(map)
224+
next = :maps.next(iter)
225+
:maps.from_list(do_map(next, transform))
226+
end
227+
228+
defp do_map(:none, _fun), do: []
229+
230+
defp do_map({key, value, iter}, transform) do
231+
[transform.({key, value}) | do_map(:maps.next(iter), transform)]
232+
end
233+
234+
defp new_from_enum(enumerable, transform) when is_function(transform, 1) do
218235
enumerable
219236
|> Enum.map(transform)
220237
|> :maps.from_list()
@@ -972,104 +989,21 @@ defmodule Map do
972989
map_size(map)
973990
end
974991

975-
@doc """
976-
Returns a map containing only those pairs from `map`
977-
for which `fun` returns a truthy value.
978-
979-
`fun` receives the key and value of each of the
980-
elements in the map as a key-value pair.
981-
982-
See also `reject/2` which discards all elements where the
983-
function returns a truthy value.
984-
985-
> Note: if you find yourself doing multiple calls to `Map.map/2`
986-
> and `Map.filter/2` in a pipeline, it is likely more efficient
987-
> to use `Enum.map/2` and `Enum.filter/2` instead and convert to
988-
> a map at the end using `Map.new/1`.
989-
990-
## Examples
991-
992-
iex> Map.filter(%{one: 1, two: 2, three: 3}, fn {_key, val} -> rem(val, 2) == 1 end)
993-
%{one: 1, three: 3}
994-
995-
"""
996-
@doc since: "1.13.0"
997-
@spec filter(map, ({key, value} -> as_boolean(term))) :: map
998-
def filter(map, fun) when is_map(map) and is_function(fun, 1) do
999-
iter = :maps.iterator(map)
1000-
next = :maps.next(iter)
1001-
:maps.from_list(do_filter(next, fun))
1002-
end
1003-
1004-
defp do_filter(:none, _fun), do: []
1005-
1006-
defp do_filter({key, value, iter}, fun) do
1007-
if fun.({key, value}) do
1008-
[{key, value} | do_filter(:maps.next(iter), fun)]
1009-
else
1010-
do_filter(:maps.next(iter), fun)
1011-
end
1012-
end
1013-
1014-
@doc """
1015-
Returns map excluding the pairs from `map` for which `fun` returns
1016-
a truthy value.
1017-
1018-
See also `filter/2`.
1019-
1020-
## Examples
1021-
1022-
iex> Map.reject(%{one: 1, two: 2, three: 3}, fn {_key, val} -> rem(val, 2) == 1 end)
1023-
%{two: 2}
1024-
1025-
"""
1026-
@doc since: "1.13.0"
1027-
@spec reject(map, ({key, value} -> as_boolean(term))) :: map
1028-
def reject(map, fun) when is_map(map) and is_function(fun, 1) do
1029-
iter = :maps.iterator(map)
1030-
next = :maps.next(iter)
1031-
:maps.from_list(do_reject(next, fun))
1032-
end
1033-
1034-
defp do_reject(:none, _fun), do: []
1035-
1036-
defp do_reject({key, value, iter}, fun) do
1037-
if fun.({key, value}) do
1038-
do_reject(:maps.next(iter), fun)
1039-
else
1040-
[{key, value} | do_reject(:maps.next(iter), fun)]
1041-
end
992+
@doc false
993+
@deprecated "Use Enum.filter/2 instead"
994+
def filter(map, fun) when is_map(map) do
995+
Enum.filter(map, fun) |> :maps.from_list()
1042996
end
1043997

1044-
@doc """
1045-
Maps the function `fun` over all key-value pairs in `map`
1046-
1047-
It returns a map with all the values replaced with the result
1048-
of the function.
1049-
1050-
> Note: if you find yourself doing multiple calls to `Map.map/2`
1051-
> and `Map.filter/2` in a pipeline, it is likely more efficient
1052-
> to use `Enum.map/2` and `Enum.filter/2` instead and convert to
1053-
> a map at the end using `Map.new/1`.
1054-
1055-
## Examples
1056-
1057-
iex> Map.map(%{1 => "joe", 2 => "mike", 3 => "robert"}, fn {_key, val} -> String.capitalize(val) end)
1058-
%{1 => "Joe", 2 => "Mike", 3 => "Robert"}
1059-
1060-
"""
1061-
@doc since: "1.13.0"
1062-
@spec map(map, ({key, value} -> value)) :: map
1063-
def map(map, fun) when is_map(map) and is_function(fun, 1) do
1064-
iter = :maps.iterator(map)
1065-
next = :maps.next(iter)
1066-
:maps.from_list(do_map(next, fun))
998+
@doc false
999+
@deprecated "Use Enum.reject/2 instead"
1000+
def reject(map, fun) when is_map(map) do
1001+
Enum.reject(map, fun) |> :maps.from_list()
10671002
end
10681003

1069-
defp do_map(:none, _fun), do: []
1070-
1071-
defp do_map({key, value, iter}, fun) do
1072-
new_value = fun.({key, value})
1073-
[{key, new_value} | do_map(:maps.next(iter), fun)]
1004+
@doc false
1005+
@deprecated "Use Map.new/2 instead"
1006+
def map(map, fun) when is_map(map) do
1007+
new(map, fn {k, v} -> {k, fun.({k, v})} end)
10741008
end
10751009
end

0 commit comments

Comments
 (0)