View Source Qlc (qlc v1.0.10)
Link to this section Summary
Types
key position for sorting tuples.
order function
order option
sorting option.
unique option for sorting.
Functions
variable binding list to erlang_binding list
create qlc cursor from qlc_handle
delete qlc cursor
eval qlc_handle
erlang ast with binding variables to qlc_handle
string to erlang ast
fold qlc_handle with accumulator
sorts tuples on query_handle.
Returns some or all of the remaining answers to a Qlc.Cursor. Only the owner of Qlc.Cursor can retrieve answers.
optoin list to record(:qlc_opt)
string to qlc_handle with variable bindings.
Returns a query handle.
string to qlc_handle with variable bindings
create qlc handle for any. see :qlc.table/2
Link to this section Types
@type abstract_expr() :: :erl_parse.abstract_expr()
@type binding_struct() :: :erl_eval.binding_struct()
@type bindings() :: Keyword.t()
@type error_info() :: :erl_parse.error_info()
@type expr() :: :erl_parse.abstract_expr()
@type key_pos() :: non_neg_integer() | [non_neg_integer()]
key position for sorting tuples.
indexed by 0 (elixir's tuple element indexing).
@type object_list() :: traverse_fun0() | objects()
@type objects() :: [] | [term() | object_list()]
order function
detect order if smaller =< bigger then return true else false.
@type order_option() :: :ascending | :descending | order_fun()
order option
order by elixir native ascending, descending or order_fun(). default is ascending.
@type qlc_lc() :: any()
@type qlc_opt() :: tuple()
@type query_cursor() :: Qlc.Cursor.t()
@type query_handle() :: :qlc.query_handle()
@type sort_option() :: {:order, order_option()} | unique_option() | any()
sorting option.
see :qlc.sort/2
@type sort_options() :: [sort_option()] | sort_option()
@type traverse_fun0() :: (() -> traverse_result())
@type traverse_fun1() :: (:ets.match_spec() -> traverse_result())
@type traverse_fun() :: traverse_fun0() | traverse_fun1()
@type unique_option() :: {:unique, boolean()}
unique option for sorting.
only the first of a sequence of terms that compare equal(==) is output if this option is set to true. defaults to false.
Link to this section Functions
@spec bind(Keyword.t()) :: binding_struct()
@spec bind(Keyword.t(), binding_struct()) :: binding_struct()
variable binding list to erlang_binding list
@spec cursor(query_handle()) :: query_cursor()
create qlc cursor from qlc_handle
(create processes)
@spec delete_cursor(Qlc.Cursor.t()) :: :ok
delete qlc cursor
(kill processes)
@spec e(query_handle()) :: list() | {:error, module(), any()}
eval qlc_handle
@spec expr_to_handle(expr(), binding_struct(), qlc_opt_list()) :: query_handle() | {:qlc_handle, tuple()}
erlang ast with binding variables to qlc_handle
string to erlang ast
fold qlc_handle with accumulator
example
example
iex> require Qlc
iex> list = [a: 1,b: 2,c: 3]
iex> qlc_handle = Qlc.q("[X || X = {K,V} <- L, K =/= Item]",
...> [L: list, Item: :b])
...> Qlc.fold(qlc_handle, [], fn({k,v}, acc) ->
...> [{v, k}|acc]
...> end)
[{3, :c}, {1, :a}]
iex> :mnesia.create_table(:t1, [{:attributes, [:k, :v]}])
{:atomic, :ok}
iex> :mnesia.transaction(fn() ->
...> Qlc.fold(qlc_handle, [], fn({k, v}, acc) ->
...> :mnesia.write({:t1, k, v})
...> [{:t1, k, v}|acc]
...> end)
...> end)
{:atomic, [{:t1, :c, 3}, {:t1, :a, 1}]}
iex> qh = Qlc.q("[X || X = {T, K, V} <- L, K =/= Item]",
...> [L: :mnesia.table(:t1), Item: :a])
iex> :mnesia.transaction(fn() ->
...> Qlc.fold(qh, [], fn({t, k, v}, acc) ->
...> IO.inspect({t, k, v})
...> :ok = :mnesia.write({:t1, k, v+1})
...> [{:t1, k, v+1}|acc]
...> end)
...> end)
{:atomic, [{:t1, :c, 4}]}
@spec keysort(query_handle(), key_pos(), sort_options()) :: query_handle()
sorts tuples on query_handle.
The sort is performed on the element(s) mentioned in key_pos. If two tuples compare equal (==) on one element, the next element according to key_pos is compared. The sort is stable. key_pos is indexing by 0.
example
example
iex> list = [a: 3, b: 2, c: 1]
iex> Qlc.q("[ X || X <- L]", [L: list]) |>
...> Qlc.keysort(1, order: :ascending) |>
...> Qlc.e()
[c: 1, b: 2, a: 3]
iex> list = [a: 1, b: 2, c: 3, d: 2]
...> Qlc.q("[X || X <- L]", [L: list]) |>
...> Qlc.keysort(1, order: :descending, unique: true) |>
...> Qlc.e()
[c: 3, b: 2, a: 1]
@spec next_answers(Qlc.Cursor.t(), non_neg_integer()) :: [any()]
Returns some or all of the remaining answers to a Qlc.Cursor. Only the owner of Qlc.Cursor can retrieve answers.
Optional argument number_of_answers determines the maximum number of answers returned. Defaults to 10. If less than the requested number of answers is returned, subsequent calls to next_answers return [].
Examples:
iex> require Qlc
iex> list = [a: 1, b: 2,c: 3, d: 4, e: 5, f: 6]
iex> qlc_handle = Qlc.q("[X || X = {K,V} <- L]",
...> [L: list])
...> qc = Qlc.cursor(qlc_handle)
...> Qlc.next_answers(qc, 2)
[{:a, 1}, {:b, 2}]
...> Qlc.next_answers(qc, 2)
[{:c, 3}, {:d, 4}]
...> Qlc.delete_cursor(qc)
:ok
optoin list to record(:qlc_opt)
string to qlc_handle with variable bindings.
string may be literal or variable. If string is variable or function call, then expanding to string_to_handle/3 automatically. elixir expression using bang macro are available to interpolation, but expanding to erlang expression string. see examples.
syntax
syntax
[Expression || Qualifier1, Qualifier2, ...]
Expression :: arbitary Erlang term (the template)
Qualifier :: Filter or Generators
Fiilter :: Erlang expressions returning bool()
Generator :: Pattern <- ListExpression
ListExpression :: Qlc_handle or list()
Qlc_handle :: returned from Qlc.table/2, Qlc.sort/2, Qlc.keysort/3
Qlc.q/2, Qlc.string_to_handle/2
example
example
iex> require Qlc
iex> list = [a: 1,b: 2,c: 3]
iex> qlc_handle = Qlc.q("[X || X = {K,V} <- L, K =/= Item]",
...> [L: list, Item: :b])
...> Qlc.e(qlc_handle)
[a: 1, c: 3]
...> Qlc.q("[X || X = {K, V} <- L, K =:= Item]",
...> [L: qlc_handle, Item: :c]) |>
...> Qlc.e
[c: 3]
...> query_string = "[X || X = {K, V} <- L, K =:= Item]"
...> bindings = [L: list, Item: :b]
...> Qlc.q(query_string, bindings) |> Qlc.e()
[b: 2]
iex> ## Qlc.Record.defrecord(:user, [id: nil, name: nil, age: nil])
iex> list = [user(id: 1, name: :foo, age: 10),
...> user(id: 2, name: :bar, age: 20),
...> user(id: 3, name: :baz, age: 30)]
...> query_string = "[X || X <- L, #{user!(X, :age)} < Age]"
...> bindings = [L: list, Age: 20]
...> Qlc.q(query_string, bindings) |> Qlc.e()
[{:user, 1, :foo, 10}]
@spec sort(query_handle(), sort_options()) :: query_handle()
Returns a query handle.
When evaluating returned query handle, the answers to query handle argument are sorted by the query_handle() options.
example
example
iex> list = [a: 3, b: 2, c: 1]
iex> Qlc.q("[ X || X <- L]", [L: list]) |>
...> Qlc.sort(order: :descending) |>
...> Qlc.e()
[c: 1, b: 2, a: 3]
@spec string_to_handle(String.t(), binding_struct(), list()) :: query_handle() | {:error, :qlc, {non_neg_integer() | {non_neg_integer(), pos_integer()}, atom(), any()}}
string to qlc_handle with variable bindings
@spec table(traverse_fun(), table_options()) :: query_handle()
create qlc handle for any. see :qlc.table/2
example
example
iex> q = Qlc.table(fn() -> [a: 1, b: 2, c: 3] end, [])
...> Qlc.q("[X || X = {K, V} <- L, K =:= Y]", [L: q, Y: :a]) |>
...> Qlc.e()
[a: 1]
iex> tf = fn(r, f) ->
...> [r.first |
...> fn() ->
...> last = r.last
...> case r.first do
...> x when x < last ->
...> f.(Range.new(r.first+1, last), f)
...> _x -> []
...> end
...> end]
...> end
...> trf = fn(r) -> tf.(r, tf) end
...> q = Qlc.table(fn() -> trf.(1..3) end, [])
...> Qlc.q("[X || X <- Q, X > 2]", [Q: q]) |> Qlc.e()
[3]