qlc v1.0.8 Qlc View Source

Link to this section Summary

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

Specs

abstract_expr() :: :erl_parse.abstract_expr()

Specs

binding_struct() :: :erl_eval.binding_struct()

Specs

bindings() :: Keyword.t()

Specs

error_info() :: :erl_parse.error_info()

Specs

Specs

key_pos() :: non_neg_integer() | [non_neg_integer()]

key position for sorting tuples.

indexed by 0 (elixir's tuple element indexing).

Specs

object_list() :: traverse_fun0() | objects()

Specs

objects() :: [] | [term() | object_list()]

Specs

order_fun() :: (smaller :: term(), bigger :: term() -> boolean())

order function

detect order if smaller =< bigger then return true else false.

Specs

order_option() :: :ascending | :descending | order_fun()

order option

order by elixir native ascending, descending or order_fun(). default is ascending.

Specs

qlc_lc() :: any()

Specs

qlc_opt() :: tuple()

Specs

qlc_opt_list() :: [{atom(), any()} | atom()]

Specs

query_cursor() :: Qlc.Cursor.t()

Specs

query_handle() :: :qlc.query_handle()

Specs

sort_option() :: {:order, order_option()} | unique_option() | any()

sorting option.

see :qlc.sort/2

Specs

sort_options() :: [sort_option()] | sort_option()

Specs

table_options() :: Keyword.t() | tuple()

Specs

traverse_fun() :: traverse_fun0() | traverse_fun1()

Specs

traverse_fun0() :: (() -> traverse_result())

Specs

traverse_fun1() :: (:ets.match_spec() -> traverse_result())

Specs

traverse_result() :: term() | objects()

Specs

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

Specs

bind(Keyword.t()) :: binding_struct()

Specs

variable binding list to erlang_binding list

Specs

cursor(query_handle()) :: query_cursor()

create qlc cursor from qlc_handle

(create processes)

Specs

delete_cursor(Qlc.Cursor.t()) :: :ok

delete qlc cursor

(kill processes)

Specs

e(query_handle()) :: list()

eval qlc_handle

Link to this function

expr_to_handle(expr, bind, opt)

View Source

Specs

expr_to_handle(expr(), binding_struct(), qlc_opt_list()) ::
  query_handle() | {:qlc_handle, tuple()}

erlang ast with binding variables to qlc_handle

Specs

exprs(String.t()) :: expr() | no_return()

string to erlang ast

Link to this function

fold(qh, a, f, option \\ [])

View Source

Specs

fold(query_handle(), any(), (any(), any() -> any()), [any()]) :: any()

fold qlc_handle with accumulator

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}]}
Link to this function

keysort(qh, keypos, opt \\ [])

View Source

Specs

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

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]
Link to this function

next_answers(qc, number_of_answers \\ 10)

View Source

Specs

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
Link to this function

options(opt, tagname, field_defs)

View Source

Specs

options(list(), atom(), Keyword.t()) :: qlc_opt()

optoin list to record(:qlc_opt)

Link to this macro

q(string, bindings, opt \\ [])

View Source (macro)

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

[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

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}]
Link to this macro

qlc_handle(args \\ [])

View Source (macro)
Link to this macro

qlc_handle(record, args)

View Source (macro)
Link to this macro

qlc_lc(args \\ [])

View Source (macro)
Link to this macro

qlc_lc(record, args)

View Source (macro)
Link to this macro

qlc_opt(args \\ [])

View Source (macro)
Link to this macro

qlc_opt(record, args)

View Source (macro)

Specs

Returns a query handle.

When evaluating returned query handle, the answers to query handle argument are sorted by the query_handle() options.

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]
Link to this function

string_to_handle(str, bindings, opt \\ [])

View Source

Specs

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

Link to this function

table(traverse_fun, option \\ [])

View Source

Specs

create qlc handle for any. see :qlc.table/2

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]