Map Sorter v0.1.7 MapSorter.Support View Source
Generates a sort function from a list of sort specs
.
Link to this section Summary
Functions
Takes a list of sort specs
(ascending/descending keys)
Takes a list of sort specs
(ascending/descending keys)
Link to this section Types
Link to this section Functions
Takes a list of sort specs
(ascending/descending keys).
Returns a sort function based on the given sort specs
which compares
two maps
¹ and returns true if the first map
precedes the second one.
¹Or keywords or structures implementing the Access behaviour.
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
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 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 will compare two maps
¹ and return true
if the first map
precedes the second one.
—Or—
Takes the AST of an expression that will evaluate at runtime
to a list of sort specs
(ascending/descending keys).
Returns the AST of a function call to evaluate the sort function at runtime (compile time injection).
¹Or keywords or structures implementing the Access behaviour.
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