Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added merge function to Enum module to extend functionality simmilar to function uniq #4854

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 52 additions & 0 deletions lib/elixir/lib/enum.ex
Expand Up @@ -1362,6 +1362,43 @@ defmodule Enum do
end
end

@doc """
Enumerates the `enumerable`, merging all duplicated elements
into one using the function `merge_fun`.
## Examples
iex> Enum.merge([1, 2, 3, 3, 2, 1], fn (x, y) -> x + y end)
[2, 4, 6]

# This is simmilar to `Enum.uniq/1`
iex> Enum.merge([1, 2, 3, 3, 2, 1], fn (x, _y) -> x end)
[1, 2, 3]
"""
@spec merge(t, (element, element -> term)) :: list
def merge(enumerable, merge_fun) do
do_merge(enumerable, %{}, merge_fun, fn x -> x end, [])
end

@doc """
Enumerates the `enumerable`, by merging the elements for which
function `fun` returned duplicate items.
The function `fun` maps every element to a term which is used to
determine if two elements are duplicates.
Merging applies by using the function `merge_fun`.
## Example
iex> Enum.merge_by([{1, :x}, {2, :y}, {1, :z}], fn (x, _) -> x end, fn {x, _} -> x end)
[{1, :x}, {2, :y}]
iex> Enum.merge_by([a: {:tea, 2}, b: {:tea, 2}, c: {:coffee, 1}], fn (x, _) -> x end, fn {_, y} -> y end)
[a: {:tea, 2}, c: {:coffee, 1}]
iex> Enum.merge_by([%{k: "a", v: 1}, %{k: "b", v: 2}, %{k: "a", v: 3}, %{k: "b", v: 4}], \
fn(t1, t2) -> %{t1 | v: t1.v + t2.v} end, fn s -> s.k end)
[%{k: "a", v: 4}, %{k: "b", v: 6}]

"""
@spec merge_by(t, (element, element -> term), (element -> term)) :: list
def merge_by(enumerable, merge_fun, fun) do
do_merge(enumerable, %{}, merge_fun, fun, [])
end

@doc """
Returns the minimal element in the enumerable according
to Erlang's term ordering.
Expand Down Expand Up @@ -2593,6 +2630,21 @@ defmodule Enum do
default
end

## merge

defp do_merge([h | t], set, merge_fun, fun, acc) do
value = fun.(h)
case set do
%{^value => index} ->
acc = List.update_at(acc, index, &(merge_fun.(&1, h)))
do_merge(t, set, merge_fun, fun, acc)
%{} -> do_merge(t, Map.put(set, value, Enum.count(acc)), merge_fun, fun, acc ++ [h])
end
end
defp do_merge([], _set, _merge_fun, _fun, acc) do
acc
end

## shuffle

defp unwrap([{_, h} | enumerable], t) do
Expand Down
14 changes: 14 additions & 0 deletions lib/elixir/test/elixir/enum_test.exs
Expand Up @@ -296,6 +296,20 @@ defmodule EnumTest do
refute Enum.member?([1, 2, 3], 0)
end

test "merge/2" do
assert Enum.merge([], fn x, _y -> x end) == []
assert Enum.merge([1], fn x, _y -> x end) == [1]
assert Enum.merge([1, -1], fn x, _y -> x end) == [1, -1]
assert Enum.merge([1, 1], fn x, _y -> x end) == [1]
assert Enum.merge([1, 2, 3, 3, 2, 1], fn x, y -> x + y end) == [2, 4, 6]
end

test "merge/3" do
list = [%{k: "a", v: 1}, %{k: "b", v: 2}, %{k: "a", v: 3}, %{k: "b", v: 4}]
assert Enum.merge_by(list, fn(t1, _t2) -> t1 end, fn s -> s.k end) == [%{k: "a", v: 1}, %{k: "b", v: 2}]
assert Enum.merge_by(list, fn(t1, t2) -> %{t1 | v: t1.v + t2.v} end, fn s -> s.k end) == [%{k: "a", v: 4}, %{k: "b", v: 6}]
end

test "min/1" do
assert Enum.min([1]) == 1
assert Enum.min([1, 2, 3]) == 1
Expand Down