MapSorter.SortSpecs (Map Sorter v0.2.45) View Source
Generates the AST of a compare function from a list of sort specs
.
Link to this section Summary
Functions
Converts sort specs
into the AST of a compare function
(compile time or runtime).
Link to this section Types
Specs
t() :: [MapSorter.SortSpec.t()]
Link to this section Functions
Specs
Converts sort specs
into the AST of a compare function
(compile time or runtime).
Examples
iex> alias MapSorter.SortSpecs
iex> sort_specs = [:dob, desc: :likes]
iex> {:ok, comp_fun_ast} = SortSpecs.to_quoted(sort_specs)
iex> {:&, _meta, [args]} = comp_fun_ast
iex> Macro.to_string(args)
"""
cond do
(&1)[:dob] < (&2)[:dob] ->
true
(&1)[:dob] > (&2)[:dob] ->
false
(&1)[:likes] > (&2)[:likes] ->
true
(&1)[:likes] < (&2)[:likes] ->
false
true ->
true or &1 * &2
end
"""
|> String.trim_trailing()
iex> alias MapSorter.SortSpecs
iex> sort_specs = quote do: Tuple.to_list({:dob, {:desc, :likes}})
iex> {:ok, comp_fun_ast} = SortSpecs.to_quoted(sort_specs)
iex> {{:., _, _}, _meta, [args]} = comp_fun_ast
iex> Macro.to_string(args)
"Tuple.to_list({:dob, {:desc, :likes}})"