Map Sorter v0.1.16 MapSorter.Impl View Source

Generates a sort 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

Returns a sort function based on the given sort specs

Returns the AST of a sort function based on the given sort specs (compile time or runtime)

Link to this section Types

Link to this type sort_dir() View Source
sort_dir() :: :asc | :desc
Link to this type sort_fun() View Source
sort_fun() :: (map(), map() -> boolean())
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.Impl
iex> doc =
...>   """
...>   &1[:dob] < ...
...>   &2[:likes] -> ...
...>   """
iex> Impl.adapt_string(doc, true)
"""
Elixir.MapSorter.Impl.comparable(&1[:dob]) < ...
Elixir.MapSorter.Impl.comparable(&2[:likes]) -> ...
"""

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

iex> alias MapSorter.Impl
iex> Impl.comparable(3.1416)
3.1416
Link to this function eval_sort_fun(sort_specs) View Source
eval_sort_fun([sort_spec()]) :: sort_fun()

Returns a sort function based on the given sort specs.

Examples

iex> alias MapSorter.Impl
iex> Logger.configure(level: :info) # :debug → debug messages
iex> sort_fun = Impl.eval_sort_fun([:dob, desc: :likes])
iex> Logger.configure(level: :info)
iex> is_function(sort_fun, 2)
true
Link to this function sort_fun(sort_specs) View Source
sort_fun([sort_spec()] | Macro.expr()) :: Macro.expr()

Returns the AST of a sort function based on the given sort specs (compile time or runtime).

Examples

iex> alias MapSorter.Impl
iex> sort_specs = [:dob, desc: :likes]
iex> Logger.configure(level: :info) # :debug → debug messages
iex> sort_fun = Impl.sort_fun(sort_specs)
iex> Logger.configure(level: :info)
iex> match?({:&, _meta, _args}, sort_fun) and
iex> match?([_, _], sort_specs)
true

iex> alias MapSorter.Impl
iex> sort_specs = quote do: Tuple.to_list({:dob, {:desc, :likes}})
iex> Logger.configure(level: :info) # :debug → debug messages
iex> sort_fun = Impl.sort_fun(sort_specs)
iex> Logger.configure(level: :info)
iex> match?({{:., _, _}, _meta, _args}, sort_fun) and
iex> match?({{:., _, _}, _meta, _args}, sort_specs)
true