Map Sorter v0.1.18 MapSorter.SortSpecs View Source
Generates a compare function from a list of sort specs
(ascending/descending keys).
Link to this section Summary
Functions
Adapts string
to maybe
invoke the comparable/1
function
Converts a value
to a comparable format, if needed
Converts sort specs
to a compare function
Converts sort specs
to the AST of a compare function
Link to this section Types
Link to this type
comp_fun()
View Source
comp_fun() :: (Access.container(), Access.container() -> boolean())
Link to this section Functions
Adapts string
to maybe
invoke the comparable/1
function.
Examples
iex> alias MapSorter.SortSpecs
iex> doc =
...> """
...> &1[:dob] < ...
...> &2[:likes] -> ...
...> """
iex> SortSpecs.adapt_string(doc, true)
"""
Elixir.MapSorter.SortSpecs.comparable(&1[:dob]) < ...
Elixir.MapSorter.SortSpecs.comparable(&2[:likes]) -> ...
"""
iex> alias MapSorter.SortSpecs
iex> doc =
...> """
...> &1[:dob] < ...
...> &2[:likes] -> ...
...> """
iex> SortSpecs.adapt_string(doc, false)
"""
&1[:dob] < ...
&2[:likes] -> ...
"""
Converts a value
to a comparable format, if needed.
Examples
iex> alias MapSorter.SortSpecs
iex> SortSpecs.comparable(~T[15:41:33])
"15:41:33"
iex> alias MapSorter.SortSpecs
iex> SortSpecs.comparable(3.1416)
3.1416
Converts sort specs
to a compare function.
Examples
iex> alias MapSorter.SortSpecs
iex> Logger.configure(level: :info) # :debug → debug messages
iex> comp_fun = SortSpecs.to_comp_fun([:dob, desc: :likes])
iex> Logger.configure(level: :info)
iex> is_function(comp_fun, 2) and
...> comp_fun.(%{likes: "ski"}, %{likes: "art"}) == true and
...> comp_fun.(%{likes: "art"}, %{likes: "ski"}) == false
true
iex> alias MapSorter.SortSpecs
iex> Logger.configure(level: :info) # :debug → debug messages
iex> comp_fun = SortSpecs.to_comp_fun({:dob, :desc, :likes})
iex> Logger.configure(level: :info)
iex> is_function(comp_fun, 2) and
...> comp_fun.(:_1st, :_2nd) == true and
...> comp_fun.(:_2nd, :_1st) == true
true
iex> alias MapSorter.SortSpecs
iex> Logger.configure(level: :info) # :debug → debug messages
iex> comp_fun = SortSpecs.to_comp_fun([])
iex> Logger.configure(level: :info)
iex> is_function(comp_fun, 2) and
...> comp_fun.(:_1st, :_2nd) == true and
...> comp_fun.(:_2nd, :_1st) == true
true
iex> alias MapSorter.SortSpecs
iex> Logger.configure(level: :info) # :debug → debug messages
iex> comp_fun = SortSpecs.to_comp_fun(nil)
iex> Logger.configure(level: :info)
iex> is_function(comp_fun, 2) and
...> comp_fun.(:_1st, :_2nd) == true and
...> comp_fun.(:_2nd, :_1st) == true
true
Link to this function
to_quoted(sort_specs)
View Source
to_quoted([sort_spec()] | Macro.expr()) :: {:ok, Macro.expr()} | {:error, any()}
Converts sort specs
to the AST of a compare function.
Examples
iex> alias MapSorter.SortSpecs
iex> sort_specs = [:dob, desc: :likes]
iex> Logger.configure(level: :info) # :debug → debug messages
iex> {:ok, comp_fun} = SortSpecs.to_quoted(sort_specs)
iex> Logger.configure(level: :info)
iex> match?({:&, _meta, _args}, comp_fun) and
iex> match?([_, _], sort_specs)
true
iex> alias MapSorter.SortSpecs
iex> sort_specs = quote do: Tuple.to_list({:dob, {:desc, :likes}})
iex> Logger.configure(level: :info) # :debug → debug messages
iex> {:ok, comp_fun} = SortSpecs.to_quoted(sort_specs)
iex> Logger.configure(level: :info)
iex> match?({{:., _, _}, _meta, _args}, comp_fun) and
iex> match?({{:., _, _}, _meta, _args}, sort_specs)
true
iex> alias MapSorter.SortSpecs
iex> sort_specs = []
iex> Logger.configure(level: :info) # :debug → debug messages
iex> {:ok, comp_fun} = SortSpecs.to_quoted(sort_specs)
iex> Logger.configure(level: :info)
iex> match?({:&, _meta, _args}, comp_fun) and
iex> match?([], sort_specs)
true
iex> alias MapSorter.SortSpecs
iex> sort_specs = nil
iex> Logger.configure(level: :info) # :debug → debug messages
iex> {:error, bad_specs} = SortSpecs.to_quoted(sort_specs)
iex> Logger.configure(level: :info)
iex> bad_specs == sort_specs
true