Map Sorter v0.1.2 MapSorter.Support View Source

Generates a sort function from sort specs allowing to sort maps.

Link to this section Summary

Functions

Takes a list of sort specs (ascending/descending map keys)

Takes a list of sort specs (ascending/descending map keys)

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() :: Map.key() | {sort_dir(), Map.key()}

Link to this section Functions

Link to this function eval_sort_fun(sort_specs) View Source
eval_sort_fun([sort_spec()]) :: sort_fun()

Takes a list of sort specs (ascending/descending map keys).

Returns a sort function based on the given sort specs comparing two maps and returning true if the first map precedes the second one.

Examples

iex> alias MapSorter.Support
iex> Logger.configure(level: :info) # :debug ⇒ debug messages
iex> sort_fun = Support.eval_sort_fun([:bmi, desc: :likes])
iex> Logger.configure(level: :info) # :info ⇒ no debug messages
iex> here_doc =
...>   """
...>   & cond do
...>   &1[:bmi] < &2[:bmi] -> true
...>   &1[:bmi] > &2[:bmi] -> false
...>   &1[:likes] > &2[:likes] -> true
...>   &1[:likes] < &2[:likes] -> false
...>   true -> true
...>   end
...>   """
iex> {here_fun, []} = Code.eval_string(here_doc)
iex> sort_fun == here_fun and
...> is_function(sort_fun, 2)
true
Link to this function sort_fun_ast(sort_specs) View Source
sort_fun_ast({any(), any(), any()}) :: {{:., any(), any()}, any(), any()}
sort_fun_ast([sort_spec()]) :: {:&, any(), any()}

Takes a list of sort specs (ascending/descending map keys).

Returns the AST of a sort function based on the given sort specs allowing to sort a list of maps (compile time expansion).

The sort function must compare two maps and return true if the first map precedes the second one.

Or:

Takes the AST of an expression that should evaluate at runtime to a list of sort specs (ascending/descending map keys).

Returns the AST of a function call to evaluate the sort function at runtime (compile time injection).

Examples

iex> alias MapSorter.Support
iex> Logger.configure(level: :info) # :debug ⇒ debug messages
iex> sort_fun_ast = Support.sort_fun_ast([:height, desc: :likes])
iex> Logger.configure(level: :info) # :info ⇒ no debug messages
iex> here_doc = """
...>   & cond do
...>   &1[:height] < &2[:height] -> true
...>   &1[:height] > &2[:height] -> false
...>   &1[:likes] > &2[:likes] -> true
...>   &1[:likes] < &2[:likes] -> false
...>   true -> true
...>   end
...>   """
iex> {:ok, here_ast} = Code.string_to_quoted(here_doc)
iex> sort_fun_ast == here_ast and
...> match?({:&, _meta, _args}, sort_fun_ast)
true

iex> alias MapSorter.Support
iex> Logger.configure(level: :info) # :debug ⇒ debug messages
iex> {sort_fun, []} =
iex>   [:weight, desc: :likes]
...>   |> Support.sort_fun_ast()
...>   |> Code.eval_quoted()
iex> Logger.configure(level: :info) # :info ⇒ no debug messages
iex> here_doc = """
...>   & cond do
...>   &1[:weight] < &2[:weight] -> true
...>   &1[:weight] > &2[:weight] -> false
...>   &1[:likes] > &2[:likes] -> true
...>   &1[:likes] < &2[:likes] -> false
...>   true -> true
...>   end
...>   """
iex> {here_fun, []} = Code.eval_string(here_doc)
iex> sort_fun == here_fun and
...> is_function(sort_fun, 2)
true