diff options
author | José Valim <jose.valim@plataformatec.com.br> | 2015-04-29 13:55:23 +0200 |
---|---|---|
committer | José Valim <jose.valim@plataformatec.com.br> | 2015-04-29 13:55:23 +0200 |
commit | 3fdb47c7a001784f98e0b057d275be2a9d60ade5 (patch) | |
tree | 2f319aa13b4dbaa0f2e60de05bc6950503e90e8a | |
parent | e15a977a494d935212481a6f0c4f4824d2057316 (diff) | |
download | elixir-3fdb47c7a001784f98e0b057d275be2a9d60ade5.tar.gz |
Document dedup uses ===, remove usort
-rw-r--r-- | CHANGELOG.md | 4 | ||||
-rw-r--r-- | lib/elixir/lib/enum.ex | 40 | ||||
-rw-r--r-- | lib/elixir/lib/stream.ex | 2 | ||||
-rw-r--r-- | lib/elixir/test/elixir/enum_test.exs | 19 | ||||
-rw-r--r-- | lib/elixir/test/elixir/stream_test.exs | 1 |
5 files changed, 8 insertions, 58 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f1a6b1f8..d8564fe8d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,7 +42,7 @@ and is using Erlang 17.1, remember to update to at least Erlang 17.3. * [Dict] Add `Dict.get_and_update/3` which behaves similar to the now deprecated Access protocol * [Dict] Add `Dict.get_lazy/3`, `Dict.pop_lazy/3` and `Dict.put_new_lazy/3` * [EEx] Add `:trim` option to EEx that automatically trims the left side of `<%` and right side `%>` if only spaces and new lines preceed/follow them - * [Enum] Add `Enum.random/1`, `Enum.minmax/1`, `Enum.minmax_by/2`, `Enum.reverse_slice/3`, `Enum.dedup/1`, `Enum.dedup_by/2`, `Enum.usort/1` and `Enum.usort_by/2` + * [Enum] Add `Enum.random/1`, `Enum.minmax/1`, `Enum.minmax_by/2`, `Enum.reverse_slice/3`, `Enum.dedup/1` and `Enum.dedup_by/2` * [Enum] Inline common map usage in `Enum` functions for performance * [ExUnit] Add number of skipped tests to `ExUnit` output * [ExUnit] Make timeout configurable for the whole test suite via the `:timeout` configuration @@ -61,7 +61,7 @@ and is using Erlang 17.1, remember to update to at least Erlang 17.3. * [Mix] Allow rebar dependencies to be specified via `:path` * [Record] Expand attributes and macros when extracting records * [Set] Introduce `MapSet` data type. This new data type uses maps behind the scenes and is useful for storing a dozens of items in Erlang 17. In future versions when maps efficiently support large collections, it is meant to be the main Set abstraction in Elixir - * [Stream] Add `Stream.dedup/1`, `Stream.dedup_by/2`, `Stream.usort/1` and `Stream.usort_by/2` + * [Stream] Add `Stream.dedup/1` and `Stream.dedup_by/2` * [String] Support calculation of the jaro distance between strings (usually names) via `String.jaro_distance/2`. This is used by Mix to support "Did you mean?" feature when a task does not exist * [StringIO] `StringIO.flush/1` was added to flush the output of a StringIO device * [URI] Default ports were added for "ws" and "wss" schemas diff --git a/lib/elixir/lib/enum.ex b/lib/elixir/lib/enum.ex index 0ff96d110..f759e203b 100644 --- a/lib/elixir/lib/enum.ex +++ b/lib/elixir/lib/enum.ex @@ -466,6 +466,8 @@ defmodule Enum do Enumerates the collection, returning a list where all consecutive duplicated elements are collapsed to a single element. + Elements are compared using `===`. + ## Examples iex> Enum.dedup([1, 2, 3, 3, 2, 1]) @@ -2101,44 +2103,6 @@ defmodule Enum do end @doc """ - Sorts the collection, eliminating duplicate elements (one element is kept - from each group of duplicates). - - ## Examples - - iex> Enum.usort([5, 1, 2, 3, 2, 1]) - [1, 2, 3, 5] - - """ - @spec usort(t) :: list - def usort(collection) when is_list(collection) do - :lists.usort(collection) - end - - def usort(collection) do - collection |> sort |> dedup - end - - @doc """ - Sorts the collection, eliminating duplicate elements (one element is kept - from each group of duplicates). - - The function `fun` maps every element to a term which is used to - sort and dedup by. - - ## Examples - - iex> Enum.usort_by([5, 1, 2, 3, 2, 1], fn x -> x > 2 end) - [1, 5] - - """ - - @spec usort_by(t, (element -> term)) :: list - def usort_by(collection, fun) do - collection |> sort_by(fun) |> dedup_by(fun) - end - - @doc """ Opposite of `Enum.zip/2`; takes a list of two-element tuples and returns a tuple with two lists, each of which is formed by the first and second element of each tuple, respectively. diff --git a/lib/elixir/lib/stream.ex b/lib/elixir/lib/stream.ex index 1a3f55b7d..2d14a14e4 100644 --- a/lib/elixir/lib/stream.ex +++ b/lib/elixir/lib/stream.ex @@ -211,6 +211,8 @@ defmodule Stream do This function only ever needs to store the last emitted element. + Elements are compared using `===`. + ## Examples iex> Stream.dedup([1, 2, 3, 3, 2, 1]) |> Enum.to_list diff --git a/lib/elixir/test/elixir/enum_test.exs b/lib/elixir/test/elixir/enum_test.exs index 46f33ba94..6830a698c 100644 --- a/lib/elixir/test/elixir/enum_test.exs +++ b/lib/elixir/test/elixir/enum_test.exs @@ -98,6 +98,7 @@ defmodule EnumTest.List do assert Enum.dedup([1, 1, 2, 1, 1, 2, 1]) == [1, 2, 1, 2, 1] assert Enum.dedup([2, 1, 1, 2, 1]) == [2, 1, 2, 1] assert Enum.dedup([1, 2, 3, 4]) == [1, 2, 3, 4] + assert Enum.dedup([1, 1.0, 2.0, 2]) == [1, 1.0, 2.0, 2] assert Enum.dedup([]) == [] assert Enum.dedup([nil, nil, true, {:value, true}]) == [nil, true, {:value, true}] assert Enum.dedup([nil]) == [nil] @@ -458,14 +459,6 @@ defmodule EnumTest.List do assert Enum.uniq([1, 2, 3, 2, 1], fn x -> x end) == [1, 2, 3] end - test :usort do - assert Enum.usort([5, 1, 2, 3, 2, 1]) == [1, 2, 3, 5] - end - - test :usort_by do - assert Enum.usort_by([5, 1, 2, 3, 2, 1], fn x -> x > 2 end) == [1, 5] - 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}] @@ -1111,16 +1104,6 @@ defmodule EnumTest.Range do assert Enum.uniq(1..3, fn x -> x end) == [1, 2, 3] end - test :usort do - assert Enum.usort(1..3) == [1, 2, 3] - assert Stream.cycle(3..1) |> Stream.take(10) |> Enum.usort == [1, 2, 3] - end - - test :usort_by do - assert Enum.usort_by(1..3, fn x -> x > 1 end) == [1, 2] - assert Stream.cycle(3..1) |> Stream.take(10) |> Enum.usort_by(fn x -> x > 1 end) == [1, 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}] diff --git a/lib/elixir/test/elixir/stream_test.exs b/lib/elixir/test/elixir/stream_test.exs index fc57757a4..dc7ac1ce8 100644 --- a/lib/elixir/test/elixir/stream_test.exs +++ b/lib/elixir/test/elixir/stream_test.exs @@ -163,6 +163,7 @@ defmodule StreamTest do assert Stream.dedup([1, 1, 2, 1, 1, 2, 1]) |> Enum.to_list == [1, 2, 1, 2, 1] assert Stream.dedup([2, 1, 1, 2, 1]) |> Enum.to_list == [2, 1, 2, 1] assert Stream.dedup([1, 2, 3, 4]) |> Enum.to_list == [1, 2, 3, 4] + assert Stream.dedup([1, 1.0, 2.0, 2]) |> Enum.to_list == [1, 1.0, 2.0, 2] assert Stream.dedup([]) |> Enum.to_list == [] assert Stream.dedup([nil, nil, true, {:value, true}]) |> Enum.to_list == [nil, true, {:value, true}] |