Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Move more behaviour to Enum

  • Loading branch information...
commit 362ff69e653f9d6a05523a9a8f0cfa916094bdec 1 parent ba43183
@josevalim josevalim authored
View
3  CHANGELOG.md
@@ -17,8 +17,9 @@
* [Kernel] Ensure proper stacktraces when showing deprecations
* deprecations
- * [Enum] `Enum.qsort` is deprecated and `List.sort` in favor of `Enum.sort`
+ * [Enum] `Enum.qsort` is deprecated in favor of `Enum.sort`
* [ExUnit] `assert left in right` is deprecated in favor of `assert left inlist right`
+ * [List] `List.sort` and `List.uniq` have been deprecated in favor of their `Enum` counterparts
* [Record] Default-based generated functions are deprecated
* [Typespec] Enhancements and deprecations to the `@spec/@callback` and the fun type syntax
View
47 lib/elixir/lib/enum.ex
@@ -846,6 +846,29 @@ defmodule Enum do
end
@doc """
+ Iterates the enumerable removing all duplicated items.
+
+ ## Examples
+
+ Enum.uniq [1,2,3,2,1]
+ #=> [1, 2, 3]
+
+ """
+ @spec uniq(t) :: list
+ def uniq(collection) when is_list(collection) do
+ do_uniq(collection, [])
+ end
+
+ def uniq(collection) do
+ case I.iterator(collection) do
+ { iterator, pointer } ->
+ do_uniq(pointer, iterator, [])
+ list when is_list(list) ->
+ do_uniq(list, [])
+ end
+ end
+
+ @doc """
Zips corresponding elements from two collections into one list
of tuples. The number of elements in the resulting list is
dictated by the first enum. In case the second list is shorter,
@@ -1541,6 +1564,30 @@ defmodule Enum do
[]
end
+ ## uniq
+
+ defp do_uniq([h|t], acc) do
+ case :lists.member(h, acc) do
+ true -> do_uniq(t, acc)
+ false -> [h|do_uniq(t, [h|acc])]
+ end
+ end
+
+ defp do_uniq([], _acc) do
+ []
+ end
+
+ defp do_uniq({ h, next }, iterator, acc) do
+ case :lists.member(h, acc) do
+ true -> do_uniq(iterator.(next), iterator, acc)
+ false -> [h|do_uniq(iterator.(next), iterator, [h|acc])]
+ end
+ end
+
+ defp do_uniq(:stop, _, _acc) do
+ []
+ end
+
## zip
defp do_zip([h1|next1], other) do
View
4 lib/elixir/lib/kernel/cli.ex
@@ -241,7 +241,7 @@ defmodule Kernel.CLI do
defp process_command({:parallel_require, pattern}, _config) when is_binary(pattern) do
files = File.wildcard(pattern)
- files = List.uniq(files)
+ files = Enum.uniq(files)
files = Enum.filter files, File.regular?(&1)
Kernel.ParallelRequire.files(files)
end
@@ -250,7 +250,7 @@ defmodule Kernel.CLI do
File.mkdir_p(config.output)
files = Enum.map patterns, File.wildcard(&1)
- files = List.uniq(List.concat(files))
+ files = Enum.uniq(List.concat(files))
files = Enum.filter files, File.regular?(&1)
Code.compiler_options(config.compiler_options)
View
42 lib/elixir/lib/list.ex
@@ -252,25 +252,11 @@ defmodule List do
:lists.keydelete(key, position + 1, list)
end
- @doc """
- Returns a list of integers in the given range (both ends included when
- possible). An optional step can be provided as well (defaults to 1).
-
- If first > last and no step is provided, the numbers will be in descending
- order.
-
- ## Examples
-
- List.range 1, 3 #=> [1,2,3]
- List.range 1, 8, 2 #=> [1,3,5,7]
- List.range 1, 0 #=> []
- List.range 3, 1 #=> [3,2,1]
- List.range 5, 1, -2 #=> [5, 3, 1]
-
- """
+ @doc false
def range(first, last, step // nil)
def range(first, last, step) when is_integer(first) and is_integer(last) and first <= last do
+ IO.write "[WARNING] List.range is deprecated, please use ranges instead\n#{Exception.formatted_stacktrace}"
case step do
nil ->
:lists.seq(first, last, 1)
@@ -282,6 +268,7 @@ defmodule List do
end
def range(first, last, step) when is_integer(first) and is_integer(last) and first > last do
+ IO.write "[WARNING] List.range is deprecated, please use ranges instead\n#{Exception.formatted_stacktrace}"
case step do
nil ->
:lists.seq(first, last, -1)
@@ -304,16 +291,9 @@ defmodule List do
:lists.sort fun, list
end
- @doc """
- Returns a list without duplicated items.
-
- ## Examples
-
- List.uniq [1,2,3,2,1]
- #=> [1,2,3]
-
- """
+ @doc false
def uniq(list) when is_list(list) do
+ IO.write "[WARNING] List.uniq is deprecated, please use Enum.uniq instead\n#{Exception.formatted_stacktrace}"
do_uniq(list, [])
end
@@ -379,10 +359,8 @@ defmodule List do
defp do_uniq([h|t], acc) do
case :lists.member(h, acc) do
- true ->
- do_uniq(t, acc)
- false ->
- [h|do_uniq(t, [h|acc])]
+ true -> do_uniq(t, acc)
+ false -> [h|do_uniq(t, [h|acc])]
end
end
@@ -397,10 +375,8 @@ defmodule List do
{mlist, heads} = :lists.mapfoldl converter, [], list
case heads do
- nil ->
- :lists.reverse acc
- _ ->
- do_zip mlist, [list_to_tuple(:lists.reverse(heads))|acc]
+ nil -> :lists.reverse acc
+ _ -> do_zip mlist, [list_to_tuple(:lists.reverse(heads))|acc]
end
end
View
8 lib/elixir/test/elixir/enum_test.exs
@@ -218,6 +218,10 @@ defmodule EnumTest.List do
assert Enum.take_while([], fn(_) -> true end) == []
end
+ test :uniq do
+ assert Enum.uniq([1,2,3,2,1]) == [1,2,3]
+ end
+
test :zip do
assert Enum.zip([:a, :b], [1, 2]) == [{:a, 1}, {:b, 2}]
assert Enum.zip([:a, :b], [1, 2, 3, 4]) == [{:a, 1}, {:b, 2}]
@@ -703,6 +707,10 @@ defmodule EnumTest.Range do
assert Enum.take_while([], fn(_) -> true end) == []
end
+ test :uniq do
+ assert Enum.uniq(1..3) == [1,2,3]
+ end
+
test :zip do
assert Enum.zip([:a, :b], 1..2) == [{:a, 1}, {:b, 2}]
assert Enum.zip([:a, :b], 1..4) == [{:a, 1}, {:b, 2}]
View
19 lib/elixir/test/elixir/list_test.exs
@@ -53,25 +53,12 @@ defmodule ListTest do
assert List.foldr([1,2,3,4], 0, fn x,y -> x - y end) == -2
end
- def test_member? do
+ test :member? do
assert List.member? [1,2,3], 1
refute List.member? [1,2,3], 0
refute List.member? [], 0
end
- test :range do
- assert List.range(1,3) == [1,2,3]
- assert List.range(1, 1) == [1]
- assert List.range(5, 0) == [5,4,3,2,1,0]
- assert List.range(1, 0, -1) == [1,0]
- assert List.range(1,8,2) == [1,3,5,7]
- assert List.range(7,-1,-3) == [7,4,1]
- assert List.range(2,1,1) == []
- assert List.range(8,1,1) == []
- assert List.range(1,8,-1) == []
- assert List.range(1,1,-1) == []
- end
-
test :concat_1 do
assert List.concat([[1,[2],3], [4], [5,6]]) == [1,[2],3,4,5,6]
end
@@ -84,10 +71,6 @@ defmodule ListTest do
assert Enum.reverse([1,2,3]) == [3,2,1]
end
- test :uniq do
- assert List.uniq([1,2,3,2,1]) == [1,2,3]
- end
-
test :duplicate do
assert List.duplicate(1, 3) == [1,1,1]
assert List.duplicate([1], 1) == [[1]]
View
2  lib/mix/lib/mix/tasks/run.ex
@@ -35,6 +35,6 @@ defmodule Mix.Tasks.Run do
end
defp filter_patterns(pattern) do
- Enum.filter(List.uniq(File.wildcard(pattern)), File.regular?(&1))
+ Enum.filter(Enum.uniq(File.wildcard(pattern)), File.regular?(&1))
end
end
View
2  lib/mix/lib/mix/utils.ex
@@ -116,7 +116,7 @@ defmodule Mix.Utils do
files = List.concat(lc path inlist paths do
if File.regular?(path), do: [path], else: File.wildcard("#{path}/**/#{pattern}")
end)
- files /> exclude_files /> List.uniq
+ files /> exclude_files /> Enum.uniq
end
defp exclude_files(files) do
Please sign in to comment.
Something went wrong with that request. Please try again.