Skip to content

Commit 362ff69

Browse files
author
José Valim
committed
Move more behaviour to Enum
1 parent ba43183 commit 362ff69

File tree

8 files changed

+71
-56
lines changed

8 files changed

+71
-56
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
* [Kernel] Ensure proper stacktraces when showing deprecations
1818

1919
* deprecations
20-
* [Enum] `Enum.qsort` is deprecated and `List.sort` in favor of `Enum.sort`
20+
* [Enum] `Enum.qsort` is deprecated in favor of `Enum.sort`
2121
* [ExUnit] `assert left in right` is deprecated in favor of `assert left inlist right`
22+
* [List] `List.sort` and `List.uniq` have been deprecated in favor of their `Enum` counterparts
2223
* [Record] Default-based generated functions are deprecated
2324
* [Typespec] Enhancements and deprecations to the `@spec/@callback` and the fun type syntax
2425

lib/elixir/lib/enum.ex

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,29 @@ defmodule Enum do
845845
end
846846
end
847847

848+
@doc """
849+
Iterates the enumerable removing all duplicated items.
850+
851+
## Examples
852+
853+
Enum.uniq [1,2,3,2,1]
854+
#=> [1, 2, 3]
855+
856+
"""
857+
@spec uniq(t) :: list
858+
def uniq(collection) when is_list(collection) do
859+
do_uniq(collection, [])
860+
end
861+
862+
def uniq(collection) do
863+
case I.iterator(collection) do
864+
{ iterator, pointer } ->
865+
do_uniq(pointer, iterator, [])
866+
list when is_list(list) ->
867+
do_uniq(list, [])
868+
end
869+
end
870+
848871
@doc """
849872
Zips corresponding elements from two collections into one list
850873
of tuples. The number of elements in the resulting list is
@@ -1541,6 +1564,30 @@ defmodule Enum do
15411564
[]
15421565
end
15431566

1567+
## uniq
1568+
1569+
defp do_uniq([h|t], acc) do
1570+
case :lists.member(h, acc) do
1571+
true -> do_uniq(t, acc)
1572+
false -> [h|do_uniq(t, [h|acc])]
1573+
end
1574+
end
1575+
1576+
defp do_uniq([], _acc) do
1577+
[]
1578+
end
1579+
1580+
defp do_uniq({ h, next }, iterator, acc) do
1581+
case :lists.member(h, acc) do
1582+
true -> do_uniq(iterator.(next), iterator, acc)
1583+
false -> [h|do_uniq(iterator.(next), iterator, [h|acc])]
1584+
end
1585+
end
1586+
1587+
defp do_uniq(:stop, _, _acc) do
1588+
[]
1589+
end
1590+
15441591
## zip
15451592

15461593
defp do_zip([h1|next1], other) do

lib/elixir/lib/kernel/cli.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ defmodule Kernel.CLI do
241241

242242
defp process_command({:parallel_require, pattern}, _config) when is_binary(pattern) do
243243
files = File.wildcard(pattern)
244-
files = List.uniq(files)
244+
files = Enum.uniq(files)
245245
files = Enum.filter files, File.regular?(&1)
246246
Kernel.ParallelRequire.files(files)
247247
end
@@ -250,7 +250,7 @@ defmodule Kernel.CLI do
250250
File.mkdir_p(config.output)
251251

252252
files = Enum.map patterns, File.wildcard(&1)
253-
files = List.uniq(List.concat(files))
253+
files = Enum.uniq(List.concat(files))
254254
files = Enum.filter files, File.regular?(&1)
255255

256256
Code.compiler_options(config.compiler_options)

lib/elixir/lib/list.ex

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -252,25 +252,11 @@ defmodule List do
252252
:lists.keydelete(key, position + 1, list)
253253
end
254254

255-
@doc """
256-
Returns a list of integers in the given range (both ends included when
257-
possible). An optional step can be provided as well (defaults to 1).
258-
259-
If first > last and no step is provided, the numbers will be in descending
260-
order.
261-
262-
## Examples
263-
264-
List.range 1, 3 #=> [1,2,3]
265-
List.range 1, 8, 2 #=> [1,3,5,7]
266-
List.range 1, 0 #=> []
267-
List.range 3, 1 #=> [3,2,1]
268-
List.range 5, 1, -2 #=> [5, 3, 1]
269-
270-
"""
255+
@doc false
271256
def range(first, last, step // nil)
272257

273258
def range(first, last, step) when is_integer(first) and is_integer(last) and first <= last do
259+
IO.write "[WARNING] List.range is deprecated, please use ranges instead\n#{Exception.formatted_stacktrace}"
274260
case step do
275261
nil ->
276262
:lists.seq(first, last, 1)
@@ -282,6 +268,7 @@ defmodule List do
282268
end
283269

284270
def range(first, last, step) when is_integer(first) and is_integer(last) and first > last do
271+
IO.write "[WARNING] List.range is deprecated, please use ranges instead\n#{Exception.formatted_stacktrace}"
285272
case step do
286273
nil ->
287274
:lists.seq(first, last, -1)
@@ -304,16 +291,9 @@ defmodule List do
304291
:lists.sort fun, list
305292
end
306293

307-
@doc """
308-
Returns a list without duplicated items.
309-
310-
## Examples
311-
312-
List.uniq [1,2,3,2,1]
313-
#=> [1,2,3]
314-
315-
"""
294+
@doc false
316295
def uniq(list) when is_list(list) do
296+
IO.write "[WARNING] List.uniq is deprecated, please use Enum.uniq instead\n#{Exception.formatted_stacktrace}"
317297
do_uniq(list, [])
318298
end
319299

@@ -379,10 +359,8 @@ defmodule List do
379359

380360
defp do_uniq([h|t], acc) do
381361
case :lists.member(h, acc) do
382-
true ->
383-
do_uniq(t, acc)
384-
false ->
385-
[h|do_uniq(t, [h|acc])]
362+
true -> do_uniq(t, acc)
363+
false -> [h|do_uniq(t, [h|acc])]
386364
end
387365
end
388366

@@ -397,10 +375,8 @@ defmodule List do
397375
{mlist, heads} = :lists.mapfoldl converter, [], list
398376

399377
case heads do
400-
nil ->
401-
:lists.reverse acc
402-
_ ->
403-
do_zip mlist, [list_to_tuple(:lists.reverse(heads))|acc]
378+
nil -> :lists.reverse acc
379+
_ -> do_zip mlist, [list_to_tuple(:lists.reverse(heads))|acc]
404380
end
405381
end
406382

lib/elixir/test/elixir/enum_test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ defmodule EnumTest.List do
218218
assert Enum.take_while([], fn(_) -> true end) == []
219219
end
220220

221+
test :uniq do
222+
assert Enum.uniq([1,2,3,2,1]) == [1,2,3]
223+
end
224+
221225
test :zip do
222226
assert Enum.zip([:a, :b], [1, 2]) == [{:a, 1}, {:b, 2}]
223227
assert Enum.zip([:a, :b], [1, 2, 3, 4]) == [{:a, 1}, {:b, 2}]
@@ -703,6 +707,10 @@ defmodule EnumTest.Range do
703707
assert Enum.take_while([], fn(_) -> true end) == []
704708
end
705709

710+
test :uniq do
711+
assert Enum.uniq(1..3) == [1,2,3]
712+
end
713+
706714
test :zip do
707715
assert Enum.zip([:a, :b], 1..2) == [{:a, 1}, {:b, 2}]
708716
assert Enum.zip([:a, :b], 1..4) == [{:a, 1}, {:b, 2}]

lib/elixir/test/elixir/list_test.exs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,12 @@ defmodule ListTest do
5353
assert List.foldr([1,2,3,4], 0, fn x,y -> x - y end) == -2
5454
end
5555

56-
def test_member? do
56+
test :member? do
5757
assert List.member? [1,2,3], 1
5858
refute List.member? [1,2,3], 0
5959
refute List.member? [], 0
6060
end
6161

62-
test :range do
63-
assert List.range(1,3) == [1,2,3]
64-
assert List.range(1, 1) == [1]
65-
assert List.range(5, 0) == [5,4,3,2,1,0]
66-
assert List.range(1, 0, -1) == [1,0]
67-
assert List.range(1,8,2) == [1,3,5,7]
68-
assert List.range(7,-1,-3) == [7,4,1]
69-
assert List.range(2,1,1) == []
70-
assert List.range(8,1,1) == []
71-
assert List.range(1,8,-1) == []
72-
assert List.range(1,1,-1) == []
73-
end
74-
7562
test :concat_1 do
7663
assert List.concat([[1,[2],3], [4], [5,6]]) == [1,[2],3,4,5,6]
7764
end
@@ -84,10 +71,6 @@ defmodule ListTest do
8471
assert Enum.reverse([1,2,3]) == [3,2,1]
8572
end
8673

87-
test :uniq do
88-
assert List.uniq([1,2,3,2,1]) == [1,2,3]
89-
end
90-
9174
test :duplicate do
9275
assert List.duplicate(1, 3) == [1,1,1]
9376
assert List.duplicate([1], 1) == [[1]]

lib/mix/lib/mix/tasks/run.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ defmodule Mix.Tasks.Run do
3535
end
3636

3737
defp filter_patterns(pattern) do
38-
Enum.filter(List.uniq(File.wildcard(pattern)), File.regular?(&1))
38+
Enum.filter(Enum.uniq(File.wildcard(pattern)), File.regular?(&1))
3939
end
4040
end

lib/mix/lib/mix/utils.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ defmodule Mix.Utils do
116116
files = List.concat(lc path inlist paths do
117117
if File.regular?(path), do: [path], else: File.wildcard("#{path}/**/#{pattern}")
118118
end)
119-
files /> exclude_files /> List.uniq
119+
files /> exclude_files /> Enum.uniq
120120
end
121121

122122
defp exclude_files(files) do

0 commit comments

Comments
 (0)