View Source Aja (Aja v0.6.5)
Convenience macros to work with Aja's data structures.
Use import Aja
to import everything, or import only the macros you need.
Summary
Functions
Convenience operator to concatenate an enumerable right
to a vector left
.
Convenience macro to create or pattern match on Aja.OrdMap
s.
Returns the size of an ord_map
.
Convenience macro to create or pattern match on Aja.Vector
s.
Returns the size of a vector
.
Functions
Convenience operator to concatenate an enumerable right
to a vector left
.
left
has to be an Aja.Vector
, right
can be any Enumerable
.
It is just an alias for Aja.Vector.concat/2
.
Only available on Elixir versions >= 1.11.
Examples
iex> import Aja
iex> vec(5..1) +++ vec([:boom, nil])
vec([5, 4, 3, 2, 1, :boom, nil])
iex> vec(5..1) +++ 0..3
vec([5, 4, 3, 2, 1, 0, 1, 2, 3])
Convenience macro to create or pattern match on Aja.OrdMap
s.
Use import Aja
to use it, or import Aja, only: [ord: 1]
.
Creation examples
iex> ord(%{"一" => 1, "二" => 2, "三" => 3})
ord(%{"一" => 1, "二" => 2, "三" => 3})
iex> ord(%{a: "Ant", b: "Bat", c: "Cat"})
ord(%{a: "Ant", b: "Bat", c: "Cat"})
Pattern matching examples
iex> ord(%{b: bat}) = ord(%{a: "Ant", b: "Bat", c: "Cat"}); bat
"Bat"
Replace existing keys examples
iex> ordered = ord(%{a: "Ant", b: "Bat", c: "Cat"})
iex> ord(%{ordered | b: "Buffalo"})
ord(%{a: "Ant", b: "Buffalo", c: "Cat"})
iex> ord(%{ordered | z: "Zebra"})
** (KeyError) key :z not found in: ord(%{a: "Ant", b: "Bat", c: "Cat"})
Returns the size of an ord_map
.
It is implemented as a macro so that it can be used in guards.
When used outside of a guard, it will just be replaced by a call to Aja.OrdMap.size/1
.
When used in guards, it will fail if called on something else than an Aja.OrdMap
.
It is recommended to verify the type first.
Runs in constant time.
Examples
iex> import Aja
iex> ord_map = Aja.OrdMap.new(a: 1, b: 2, c: 3)
iex> match?(v when ord_size(v) > 5, ord_map)
false
iex> match?(v when ord_size(v) < 5, ord_map)
true
iex> ord_size(ord_map)
3
Convenience macro to create or pattern match on Aja.Vector
s.
Examples
iex> import Aja
iex> vec([1, 2, 3])
vec([1, 2, 3])
iex> vec(first ||| last) = Aja.Vector.new(0..99_999); {first, last}
{0, 99999}
iex> vec([1, 2, var, _, _, _]) = Aja.Vector.new(1..6); var
3
iex> vec([_, _, _]) = Aja.Vector.new(1..6)
** (MatchError) no match of right hand side value: vec([1, 2, 3, 4, 5, 6])
It also supports ranges with constant values:
iex> vec(0..4) = Aja.Vector.new(0..4)
vec([0, 1, 2, 3, 4])
Variable lists or dynamic ranges cannot be passed:
vec(my_list) # invalid
vec(1..n) # invalid
Explanation
The vec/1
macro generates the AST at compile time instead of building the vector
at runtime. This can speedup the instanciation of vectors of known size.
Returns the size of a vector
.
It is implemented as a macro so that it can be used in guards.
When used outside of a guard, it will just be replaced by a call to Aja.Vector.size/1
.
When used in guards, it will fail if called on something else than an Aja.Vector
.
It is recommended to verify the type first.
Runs in constant time.
Examples
iex> import Aja
iex> match?(v when vec_size(v) > 20, Aja.Vector.new(1..10))
false
iex> match?(v when vec_size(v) < 5, Aja.Vector.new([1, 2, 3]))
true
iex> vec_size(Aja.Vector.new([1, 2, 3]))
3