qlc v1.0.6 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

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

Link to this type

binding_struct() View Source
binding_struct() :: :erl_eval.binding_struct()

key position for sorting tuples.

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

Link to this type

object_list() View Source
object_list() :: traverse_fun0() | objects()

Link to this type

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

Link to this type

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

order function

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

Link to this type

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

order option

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

Link to this type

qlc_opt() View Source
qlc_opt() :: tuple()

Link to this type

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

Link to this type

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

Link to this type

query_handle() View Source
query_handle() :: :qlc.query_handle()

Link to this type

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

sorting option.

see :qlc.sort/2

Link to this type

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

Link to this type

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

Link to this type

traverse_fun() View Source
traverse_fun() :: traverse_fun0() | traverse_fun1()

Link to this type

traverse_fun0() View Source
traverse_fun0() :: (() -> traverse_result())

Link to this type

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

Link to this type

traverse_result() View Source
traverse_result() :: term() | objects()

Link to this type

unique_option() View Source
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

variable binding list to erlang_binding list

create qlc cursor from qlc_handle

(create processes)

Link to this function

delete_cursor(qc) View Source
delete_cursor(Qlc.Cursor.t()) :: :ok

delete qlc cursor

(kill processes)

eval qlc_handle

Link to this function

expr_to_handle(expr, bind, opt) View Source
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

Link to this function

fold(qh, a, f, option \\ []) View Source
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}]
Link to this function

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

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

options(opt, tagname, field_defs) View Source
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)

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
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

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]