Stream.uniq_by
You're seeing just the function
uniq_by
, go back to Stream module for more information.
Specs
uniq_by(Enumerable.t(), (element() -> term())) :: Enumerable.t()
Creates a stream that only emits elements if they are unique, by removing the
elements for which function fun
returned duplicate elements.
The function fun
maps every element to a term which is used to
determine if two elements are duplicates.
Keep in mind that, in order to know if an element is unique or not, this function needs to store all unique values emitted by the stream. Therefore, if the stream is infinite, the number of elements stored will grow infinitely, never being garbage-collected.
Example
iex> Stream.uniq_by([{1, :x}, {2, :y}, {1, :z}], fn {x, _} -> x end) |> Enum.to_list()
[{1, :x}, {2, :y}]
iex> Stream.uniq_by([a: {:tea, 2}, b: {:tea, 2}, c: {:coffee, 1}], fn {_, y} -> y end) |> Enum.to_list()
[a: {:tea, 2}, c: {:coffee, 1}]