Colonel.Experimental (Colonel v0.4.0)
View SourceA proving ground for functions before they make it into Colonel
proper.
Summary
Functions
Translates to left + right
.
Translates to left && right
.
Translates to left <> right
.
Delete from structure if condition is met.
Translates to left / right
.
Translates to left == right
.
Translates to left > right
.
Translates to left >= right
.
Translates to left != right
.
Does the same thing as inspect/2
but returns IO data instead of a string.
Translates to left < right
.
Translates to left <= right
.
Translates to left ++ right
.
Translates to left -- right
.
Calls fun
with acc
until halted.
Translates to left * right
.
Translates to -value
.
Translates to !value
.
Translates to left || right
.
Translates to +value
.
Translates to base ** exponent
.
Put value into structure if condition is met.
Build a recursive anonymous function.
Handles the sigil ~i
for iodata.
Translates to left === right
.
Translates to left !== right
.
Translates to left !== right
.
Translates to left - right
.
Create tagged tuple, wrapping value
in a tuple with tag
.
Apply fun
to term
if condition
is met.
Alternate, pipe-friendly syntax for single-clause with
expressions.
Translates to left != right
.
Unwrap tagged tuple, asserting first element is tag
and returning the second.
Update value in structure if condition is met.
Functions
@spec add(integer(), integer()) :: integer()
@spec add(float(), float()) :: float()
@spec add(integer(), float()) :: float()
@spec add(float(), integer()) :: float()
Translates to left + right
.
Examples
iex> add(1, 2)
3
@spec and?(as_boolean(left), as_boolean(right)) :: as_boolean(left | right) when left: term(), right: term()
Translates to left && right
.
Examples
iex> and?(nil, false)
nil
iex> and?(1, nil)
nil
iex> and?(false, 2)
false
iex> and?(true, 2)
2
Translates to left <> right
.
Examples
iex> binary_concat("foo", "bar")
"foobar"
@spec delete_if( data, as_boolean(term()), [Access.get_and_update_fun(data, term()) | term(), ...] ) :: data when data: Access.container()
Delete from structure if condition is met.
Uses the Access
behaviour to perform the delete, so keys are a list as with
Kernel.get_and_update_in/3
and its companions.
Examples
iex> delete_if(%{items: [:a, :b, :c]}, true, [:items, Access.at(1)])
%{items: [:a, :c]}
iex> delete_if(%{key: "value"}, false, [:key])
%{key: "value"}
Translates to left / right
.
Examples
iex> divide(1, 2)
0.5
Translates to left == right
.
Examples
iex> equal?(1, 2)
false
iex> equal?(1, 1.0)
true
Translates to left > right
.
Examples
iex> greater_than?(1, 2)
false
Translates to left >= right
.
Examples
iex> greater_than_or_equal?(1, 2)
false
Translates to left != right
.
Examples
iex> inequal?(1, 2)
true
iex> inequal?(1, 1.0)
false
Does the same thing as inspect/2
but returns IO data instead of a string.
Examples
iex> iodata_inspect(:foo)
[":foo"]
iex> iodata_inspect([1, 2, 3, 4, 5], limit: 3)
["[", "1", ",", " ", "2", ",", " ", "3", ",", " ", "...", "]"]
iex> iodata_inspect([1, 2, 3], pretty: true, width: 0)
["[", "1", ",", "\n ", "2", ",", "\n ", "3", "]"]
iex> iodata_inspect("olá" <> <<0>>)
["<<", "111", ",", " ", "108", ",", " ", "195", ",", " ", "161", ",", " ", "0", ">>"]
iex> iodata_inspect("olá" <> <<0>>, binaries: :as_strings)
[~S("olá\0")]
iex> iodata_inspect("olá", binaries: :as_binaries)
["<<", "111", ",", " ", "108", ",", " ", "195", ",", " ", "161", ">>"]
iex> iodata_inspect([0 | ~c"bar"])
["[", "0", ",", " ", "98", ",", " ", "97", ",", " ", "114", "]"]
iex> iodata_inspect(100, base: :octal)
["0o144"]
iex> iodata_inspect(100, base: :hex)
["0x64"]
Translates to left < right
.
Examples
iex> less_than?(1, 2)
true
Translates to left <= right
.
Examples
iex> less_than_or_equal?(1, 2)
true
@spec list_concat(list(), term()) :: maybe_improper_list()
Translates to left ++ right
.
Examples
iex> list_concat([1], [2, 3])
[1, 2, 3]
Translates to left -- right
.
Examples
iex> list_subtract([1, 2, 3], [1, 2])
[3]
@spec loop(acc, (acc -> {:cont, acc} | {:halt, final_acc})) :: final_acc when acc: term(), final_acc: term()
Calls fun
with acc
until halted.
Function takes an acc and can return two things:
{:cont, new_acc}
results infun
being called withnew_acc
{:halt, final_acc}
results inloop/2
returningfinal_acc
Examples
iex> loop({5, 1}, fn
...> {1, total} -> {:halt, total}
...> {n, total} -> {:cont, {n - 1, n * total}}
...> end)
120
@spec multiply(integer(), integer()) :: integer()
@spec multiply(float(), float()) :: float()
@spec multiply(integer(), float()) :: float()
@spec multiply(float(), integer()) :: float()
Translates to left * right
.
Examples
iex> multiply(1, 2)
2
@spec negative(0) :: 0
@spec negative(pos_integer()) :: neg_integer()
@spec negative(neg_integer()) :: pos_integer()
@spec negative(float()) :: float()
Translates to -value
.
Examples
iex> negative(2)
-2
@spec not?(as_boolean(term())) :: boolean()
Translates to !value
.
Examples
iex> not?(nil)
true
iex> not?(1)
false
@spec or?(as_boolean(left), as_boolean(right)) :: as_boolean(left | right) when left: term(), right: term()
Translates to left || right
.
Examples
iex> or?(nil, false)
false
iex> or?(1, nil)
1
iex> or?(false, 2)
2
iex> or?(true, 2)
true
Translates to +value
.
Examples
iex> positive(1)
1
@spec pow(integer(), non_neg_integer()) :: integer()
@spec pow(integer(), neg_integer()) :: float()
@spec pow(float(), float()) :: float()
@spec pow(integer(), float()) :: float()
@spec pow(float(), integer()) :: float()
Translates to base ** exponent
.
Examples
iex> pow(2, 5)
32
iex> pow(36, 0.5)
6.0
iex> pow(4, -1)
0.25
@spec put_if( data, as_boolean(term()), [Access.get_and_update_fun(data, term()) | term(), ...], term() ) :: data when data: Access.container()
Put value into structure if condition is met.
Uses the Access
behaviour to perform the put, so keys are a list as with
Kernel.get_and_update_in/3
and its companions.
Examples
iex> put_if(%{data: %{}}, true, [:data, :my_key], "my value")
%{data: %{my_key: "my value"}}
iex> put_if([], false, [:only], [:a, :b, :c])
[]
Build a recursive anonymous function.
This macro must be passed the function in the fn
syntax.
Generated functions can call themselves using super
.
Examples
iex> reverse = recursive fn
...> [], acc -> acc
...> [head | tail], acc -> super(tail, [head | acc])
...> end
iex> reverse.([1, 2, 3], [])
[3, 2, 1]
Handles the sigil ~i
for iodata.
Modifiers
s
: interpolated values should be transformed byto_string/1
(default)i
: interpolated values should be transformed byiodata_inspect/1
d
: interpolated values are already iodata/chardata and do not need transformed
Using the ~i
sigil with the s
or i
modifiers will always return iodata. The d
modifier
allows for chardata that is not iodata by passing through interpolated chardata.
The d
modifier
The d
modifier does not transform interpolated values, so they will appear in the generated
list as-is. This makes the operation cheaper when interpolated values are already
iodata/chardata but allows construction of invalid iodata/chardata. Care should be taken when
using the d
modifier that all interpolated values are valid iodata/chardata.
Examples
iex> ~i"some #{["list", ["of", "nested"], "values"]} and #{:such}"
["some ", "listofnestedvalues", " and ", "such"]
iex> ~i"some #{["list", ["of", "nested"], "values"]} and #{:such}"s
["some ", "listofnestedvalues", " and ", "such"]
iex> ~i"some #{["list", ["of", "nested"], "values"]} and #{:such}"i
[
"some ",
[
"[",
"",
~S("list"),
",",
" ",
"[",
~S("of"),
",",
" ",
~S("nested"),
"]",
",",
" ",
~S("values"),
"",
"]"
],
" and ",
[":such"]
]
iex> ~i"some #{["list", ["of", "nested"], "values"]} and #{to_string(:such)}"d
["some ", ["list", ["of", "nested"], "values"], " and ", "such"]
Translates to left === right
.
Examples
iex> strictly_equal?(1, 2)
false
iex> strictly_equal?(1, 1.0)
false
Translates to left !== right
.
Examples
iex> strictly_inequal?(1, 2)
true
iex> strictly_inequal?(1, 1.0)
true
Translates to left !== right
.
Examples
iex> strictly_unequal?(1, 2)
true
iex> strictly_unequal?(1, 1.0)
true
@spec subtract(integer(), integer()) :: integer()
@spec subtract(float(), float()) :: float()
@spec subtract(integer(), float()) :: float()
@spec subtract(float(), integer()) :: float()
Translates to left - right
.
Examples
iex> subtract(1, 2)
-1
Create tagged tuple, wrapping value
in a tuple with tag
.
Examples
iex> tag(:cool, :ok)
{:ok, :cool}
@spec then_if(input, as_boolean(term()), (input -> output)) :: input | output when input: term(), output: term()
Apply fun
to term
if condition
is met.
Otherwise term
is passed through as-is.
Examples
iex> then_if(%{}, true, &Map.put(&1, :key, "value"))
%{key: "value"}
iex> then_if(7, false, fn x -> x + 1 end)
7
Alternate, pipe-friendly syntax for single-clause with
expressions.
Separates pattern from the expression it matches.
Examples
iex> then_with({:ok, 23}, {:ok, value}, do: {:ok, to_string(value)})
{:ok, "23"}
iex> then_with %{}, %{value: value} = result when is_binary(value) do
...> %{result | size: byte_size(value)}
...> end
%{}
Translates to left != right
.
Examples
iex> unequal?(1, 2)
true
iex> unequal?(1, 1.0)
false
Unwrap tagged tuple, asserting first element is tag
and returning the second.
Examples
iex> untag({:ok, :cool}, :ok)
:cool
iex> untag({:error, :oops}, :ok)
** (MatchError) no match of right hand side value: {:error, :oops}
@spec update_if( data, as_boolean(term()), [Access.get_and_update_fun(data, current_value) | term(), ...], (current_value -> term()) ) :: data when current_value: term(), data: Access.container()
Update value in structure if condition is met.
Uses the Access
behaviour to perform the update, so keys are a list as with
Kernel.get_and_update_in/3
and its companions.
Examples
iex> update_if([bounds: 1..10], true, [:bounds, Access.key!(:first)], fn x -> x + 3 end)
[bounds: 4..10]
iex> update_if(%{limit: 7}, false, [:limit], fn limit -> limit * 2 end)
%{limit: 7}