Colonel.Experimental (Colonel v0.4.0)

View Source

A 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

add(left, right)

(since 0.1.0)
@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

and?(left, right)

(since 0.1.0)
@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

binary_concat(left, right)

(since 0.1.0)
@spec binary_concat(binary(), binary()) :: binary()

Translates to left <> right.

Examples

iex> binary_concat("foo", "bar")
"foobar"

delete_if(data, condition, keys)

(since 0.4.0)
@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"}

divide(left, right)

(since 0.1.0)
@spec divide(number(), number()) :: float()

Translates to left / right.

Examples

iex> divide(1, 2)
0.5

equal?(left, right)

(since 0.1.0)
@spec equal?(term(), term()) :: boolean()

Translates to left == right.

Examples

iex> equal?(1, 2)
false

iex> equal?(1, 1.0)
true

greater_than?(left, right)

(since 0.1.0)
@spec greater_than?(term(), term()) :: boolean()

Translates to left > right.

Examples

iex> greater_than?(1, 2)
false

greater_than_or_equal?(left, right)

(since 0.1.0)
@spec greater_than_or_equal?(term(), term()) :: boolean()

Translates to left >= right.

Examples

iex> greater_than_or_equal?(1, 2)
false

inequal?(left, right)

(since 0.1.0)
This function is deprecated. Prefer `unequal?/2`.
@spec inequal?(term(), term()) :: boolean()

Translates to left != right.

Examples

iex> inequal?(1, 2)
true

iex> inequal?(1, 1.0)
false

iodata_inspect(term, opts \\ [])

(since 0.2.0)
@spec iodata_inspect(
  Inspect.t(),
  keyword()
) :: iodata()

Does the same thing as inspect/2 but returns IO data instead of a string.

See "iodata and chardata".

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

less_than?(left, right)

(since 0.1.0)
@spec less_than?(term(), term()) :: boolean()

Translates to left < right.

Examples

iex> less_than?(1, 2)
true

less_than_or_equal?(left, right)

(since 0.1.0)
@spec less_than_or_equal?(term(), term()) :: boolean()

Translates to left <= right.

Examples

iex> less_than_or_equal?(1, 2)
true

list_concat(left, right)

(since 0.1.0)
@spec list_concat(list(), term()) :: maybe_improper_list()

Translates to left ++ right.

Examples

iex> list_concat([1], [2, 3])
[1, 2, 3]

list_subtract(left, right)

(since 0.1.0)
@spec list_subtract(list(), list()) :: list()

Translates to left -- right.

Examples

iex> list_subtract([1, 2, 3], [1, 2])
[3]

loop(acc, fun)

(since 0.3.0)
@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 in fun being called with new_acc
  • {:halt, final_acc} results in loop/2 returning final_acc

Examples

iex> loop({5, 1}, fn
...>   {1, total} -> {:halt, total}
...>   {n, total} -> {:cont, {n - 1, n * total}}
...> end)
120

multiply(left, right)

(since 0.1.0)
@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

negative(value)

(since 0.1.0)
@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

not?(value)

(since 0.1.0)
@spec not?(as_boolean(term())) :: boolean()

Translates to !value.

Examples

iex> not?(nil)
true

iex> not?(1)
false

or?(left, right)

(since 0.1.0)
@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

positive(value)

(since 0.1.0)
@spec positive(integer()) :: integer()
@spec positive(float()) :: float()

Translates to +value.

Examples

iex> positive(1)
1

pow(base, exponent)

(since 0.4.0)
@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

put_if(data, condition, keys, value)

(since 0.4.0)
@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])
[]

recursive(fun)

(since 0.1.0) (macro)

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]

sigil_i(term, modifiers)

(since 0.2.0) (macro)

Handles the sigil ~i for iodata.

See "iodata and chardata".

Modifiers

  • s: interpolated values should be transformed by to_string/1 (default)
  • i: interpolated values should be transformed by iodata_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"]

strictly_equal?(left, right)

(since 0.1.0)
@spec strictly_equal?(term(), term()) :: boolean()

Translates to left === right.

Examples

iex> strictly_equal?(1, 2)
false

iex> strictly_equal?(1, 1.0)
false

strictly_inequal?(left, right)

(since 0.1.0)
This function is deprecated. Prefer `strictly_unequal?/2`.
@spec strictly_inequal?(term(), term()) :: boolean()

Translates to left !== right.

Examples

iex> strictly_inequal?(1, 2)
true

iex> strictly_inequal?(1, 1.0)
true

strictly_unequal?(left, right)

(since 0.3.0)
@spec strictly_unequal?(term(), term()) :: boolean()

Translates to left !== right.

Examples

iex> strictly_unequal?(1, 2)
true

iex> strictly_unequal?(1, 1.0)
true

subtract(left, right)

(since 0.1.0)
@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

tag(value, tag)

(since 0.3.0)
@spec tag(value, tag) :: {tag, value} when tag: term(), value: term()

Create tagged tuple, wrapping value in a tuple with tag.

Examples

iex> tag(:cool, :ok)
{:ok, :cool}

then_if(term, condition, fun)

(since 0.4.0)
@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

then_with(expression, pattern, block)

(since 0.4.0) (macro)

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
%{}

unequal?(left, right)

(since 0.3.0)
@spec unequal?(term(), term()) :: boolean()

Translates to left != right.

Examples

iex> unequal?(1, 2)
true

iex> unequal?(1, 1.0)
false

untag(tuple, tag)

(since 0.3.0)
@spec untag(
  {tag, value},
  tag
) :: value
when tag: term(), value: term()

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}

update_if(data, condition, keys, fun)

(since 0.4.0)
@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}