View Source MapSorter.Compare (Map Sorter v0.2.62)
A compare function and a heredoc to become a compare function.
Link to this section Summary
Types
Compare function
Functions
Generates an Enum.sort/2
compare function from a list of sort specs.
Generates a cond/1
expression as a heredoc to become a compare function.
Link to this section Types
@type comp_fun() :: (Access.container(), Access.container() -> boolean())
Compare function
Link to this section Functions
@spec fun(MapSorter.SortSpecs.t()) :: comp_fun()
Generates an Enum.sort/2
compare function from a list of sort specs.
examples
Examples
iex> alias MapSorter.Compare
iex> sort_specs = [:dob, desc: :likes]
iex> fun = Compare.fun(sort_specs)
iex> is_function(fun, 2)
true
# Compare function always true => no reordering...
iex> alias MapSorter.Compare
iex> sort_specs = {:dob, desc: :likes}
iex> fun = Compare.fun(sort_specs)
iex> is_function(fun, 2) and fun.(1, 2) and fun.(:any, 'any')
true
iex> alias MapSorter.Compare
iex> sort_specs = fn -> [:dob, desc: :likes] end
iex> fun = Compare.fun(sort_specs.())
iex> is_function(fun, 2)
true
iex> alias MapSorter.Compare
iex> sort_specs = Tuple.to_list({:dob, {:desc, :likes}})
iex> fun = Compare.fun(sort_specs)
iex> is_function(fun, 2)
true
@spec heredoc(MapSorter.SortSpecs.t()) :: String.t()
Generates a cond/1
expression as a heredoc to become a compare function.
The heredoc may be converted into its quoted form at compile time or else have its contents evaluated at runtime.
This function cannot be named cond
as it is among the Kernel.SpecialForms
.
examples
Examples
iex> alias MapSorter.Compare
iex> Compare.heredoc([])
"""
& cond do
true -> true or &1 * &2
end
"""
iex> alias MapSorter.Compare
iex> sort_specs = [:name, {:desc, :dob}]
iex> Compare.heredoc(sort_specs)
"""
& cond do
&1[:name] < &2[:name] -> true
&1[:name] > &2[:name] -> false
&1[:dob] > &2[:dob] -> true
&1[:dob] < &2[:dob] -> false
true -> true or &1 * &2
end
"""
iex> alias MapSorter.Compare
iex> sort_specs = [:name, {:desc, {:dob, Date}}]
iex> Compare.heredoc(sort_specs)
"""
& cond do
&1[:name] < &2[:name] -> true
&1[:name] > &2[:name] -> false
&1[:dob] != nil and Date.compare(&1[:dob], &2[:dob]) == :gt -> true
&1[:dob] != nil and Date.compare(&1[:dob], &2[:dob]) == :lt -> false
true -> true or &1 * &2
end
"""
iex> alias MapSorter.Compare
iex> sort_specs = [:name, {:desc, {:account, Path}}]
iex> Compare.heredoc(sort_specs)
"""
& cond do
&1[:name] < &2[:name] -> true
&1[:name] > &2[:name] -> false
&1[{:account, Path}] > &2[{:account, Path}] -> true
&1[{:account, Path}] < &2[{:account, Path}] -> false
true -> true or &1 * &2
end
"""