Map Sorter v0.1.17 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 type sort_dir() View Source
sort_dir() :: :asc | :desc
Link to this type sort_spec() View Source
sort_spec() :: any() | {sort_dir(), any()}

Link to this section Functions

Link to this function adapt_string(string, maybe) View Source
adapt_string(String.t(), boolean()) :: String.t()

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] -> ...
"""
Link to this function comparable(value) View Source
comparable(any()) :: any()

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
Link to this function to_comp_fun(sort_specs) View Source
to_comp_fun([sort_spec()]) :: comp_fun()

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
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 = {:dob, :name, :likes}
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