Guardsafe v0.5.1 Guardsafe

Provides readability-enhancing macros that can safely be used in guard clauses.

Examples

defmodule MacrofyAllTheThings do
  import Guardsafe
  require Integer

  def magic(number) when number |> nil? do
    "Staring into the void..."
  end

  def magic(number) when number |> even? do
    "That's not odd."
  end

  def magic(number) when number |> divisible_by?(5) do
    "High five!"
  end
end

iex> MacrofyAllTheThings.magic(8)
"That's not odd."

Summary

Macros

Expands atom?(term) into Kernel.is_atom(term)

Expands binary?(term) into Kernel.is_binary(term)

Expands bitstring?(term) into Kernel.is_bitstring(term)

Expands boolean?(term) into Kernel.is_boolean(term)

Returns true if the term is considered to be a date tuple

Returns true if the term is considered to be a time tuple

Returns true if the integer number is evenly divisible by the divisor

Returns true for even integers

Expands float?(term) into Kernel.is_float(term)

Expands function?(term) into Kernel.is_function(term)

Expands function?(term, arity) into is_function(term, arity)

Expands integer?(term) into Kernel.is_integer(term)

Expands list?(term) into Kernel.is_list(term)

Expands map?(term) into Kernel.is_map(term)

Expands nil?(term) into Kernel.is_nil(term)

Expands number?(term) into Kernel.is_number(term)

Returns true for odd integers

Expands pid?(term) into Kernel.is_pid(term)

Expands port?(term) into Kernel.is_port(term)

Expands reference?(term) into Kernel.is_reference(term)

Returns true if the term is considered to be a time tuple

Expands tuple?(term) into Kernel.is_tuple(term)

Returns true if the term is between the low and high values, inclusively

Macros

atom?(term)

Expands atom?(term) into Kernel.is_atom(term).

Examples

iex> some_atom_variable |> atom?
true
binary?(term)

Expands binary?(term) into Kernel.is_binary(term).

Examples

iex> some_binary_variable |> binary?
true
bitstring?(term)

Expands bitstring?(term) into Kernel.is_bitstring(term).

Examples

iex> some_bitstring_variable |> bitstring?
true
boolean?(term)

Expands boolean?(term) into Kernel.is_boolean(term).

Examples

iex> some_boolean_variable |> boolean?
true
date?(term)

Returns true if the term is considered to be a date tuple.

The date is not checked for validity other than the months being in the 1..12 range and the days being in the 1..31 range.

Examples

iex> 5 |> date?({2015, 3, 20})
true
datetime?(term)

Returns true if the term is considered to be a time tuple.

date? and time? are used for checking and validating the date and time portions of the datetime.

Examples

iex> datetime? {{2015, 3, 20}, {15, 33, 42}}
true
divisible_by?(number, divisor)

Returns true if the integer number is evenly divisible by the divisor.

Examples

iex> 25 |> divisible_by?(5)
true
even?(number)

Returns true for even integers.

Examples

iex> even? 7
false
float?(term)

Expands float?(term) into Kernel.is_float(term).

Examples

iex> some_float_variable |> float?
true
function?(term)

Expands function?(term) into Kernel.is_function(term).

Examples

iex> some_function_variable |> function?
true
function?(term, arity)

Expands function?(term, arity) into is_function(term, arity)

Examples

iex> &String.to_integer/1 |> function?(2)
false
integer?(term)

Expands integer?(term) into Kernel.is_integer(term).

Examples

iex> some_integer_variable |> integer?
true
list?(term)

Expands list?(term) into Kernel.is_list(term).

Examples

iex> some_list_variable |> list?
true
map?(term)

Expands map?(term) into Kernel.is_map(term).

Examples

iex> some_map_variable |> map?
true
nil?(term)

Expands nil?(term) into Kernel.is_nil(term).

Examples

iex> some_nil_variable |> nil?
true
number?(term)

Expands number?(term) into Kernel.is_number(term).

Examples

iex> some_number_variable |> number?
true
odd?(number)

Returns true for odd integers.

Examples

iex> odd? 5
true
pid?(term)

Expands pid?(term) into Kernel.is_pid(term).

Examples

iex> some_pid_variable |> pid?
true
port?(term)

Expands port?(term) into Kernel.is_port(term).

Examples

iex> some_port_variable |> port?
true
reference?(term)

Expands reference?(term) into Kernel.is_reference(term).

Examples

iex> some_reference_variable |> reference?
true
time?(term)

Returns true if the term is considered to be a time tuple.

The time is not checked for validity other than the hours being in the 0..23 range, the minutes being in the 0..59 range, and seconds being in the 0..60 range. The latter due to the existance of leap seconds.

Examples

iex> 5 |> time?({15, 33, 42})
true
tuple?(term)

Expands tuple?(term) into Kernel.is_tuple(term).

Examples

iex> some_tuple_variable |> tuple?
true
within?(term, low, high)

Returns true if the term is between the low and high values, inclusively.

Examples

iex> 5 |> within?(2, 10)
true