Map Sorter v0.2.28 MapSorter.SortSpec 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 (compile time or runtime)

Link to this section Types

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.SortSpec
iex> doc =
...>   """
...>   &1[:dob] < ...
...>   &2[:likes] -> ...
...>   """
iex> SortSpec.adapt_string(doc, true)
"""
Elixir.MapSorter.SortSpec.comparable(&1[:dob]) < ...
Elixir.MapSorter.SortSpec.comparable(&2[:likes]) -> ...
"""

iex> alias MapSorter.SortSpec
iex> doc =
...>   """
...>   &1[:dob] < ...
...>   &2[:likes] -> ...
...>   """
iex> SortSpec.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.SortSpec
iex> SortSpec.comparable(~T[15:41:33])
"15:41:33"

iex> alias MapSorter.SortSpec
iex> SortSpec.comparable(3.1416)
3.1416
Link to this function to_comp_fun(sort_specs) View Source
to_comp_fun([t()]) :: comp_fun()

Converts sort specs to a compare function.

Examples

iex> alias MapSorter.SortSpec
iex> comp_fun = SortSpec.to_comp_fun([:dob, desc: :likes])
iex> is_function(comp_fun, 2)
true
Link to this function to_quoted(sort_specs) View Source
to_quoted([t()] | Macro.expr()) :: {:ok, Macro.expr()} | {:error, any()}

Converts sort specs to the AST of a compare function (compile time or runtime).

Examples

iex> alias MapSorter.SortSpec
iex> sort_specs = [:dob, desc: :likes]
iex> {:ok, comp_fun_ast} = SortSpec.to_quoted(sort_specs)
iex> match?({:&, _meta, _args}, comp_fun_ast) and
iex> match?([_, _], sort_specs)
true

iex> alias MapSorter.SortSpec
iex> sort_specs = quote do: Tuple.to_list({:dob, {:desc, :likes}})
iex> {:ok, comp_fun_ast} = SortSpec.to_quoted(sort_specs)
iex> match?({{:., _, _}, _meta, _args}, comp_fun_ast) and
iex> match?({{:., _, _}, _meta, _args}, sort_specs)
true