DevL/guardsafe
{ "createdAt": "2015-02-19T16:41:22Z", "defaultBranch": "master", "description": "Macros expanding into code that can be safely used in guard clauses.", "fullName": "DevL/guardsafe", "homepage": null, "language": "Elixir", "name": "guardsafe", "pushedAt": "2025-03-10T12:34:41Z", "stargazersCount": 26, "topics": [], "updatedAt": "2025-03-10T12:34:53Z", "url": "https://github.com/DevL/guardsafe"}Guardsafe
Section titled “Guardsafe”NB: This repository has been moved to https://codeberg.org/DevL/guardsafe.
Macros expanding into code that can be safely used in guard clauses.
Update your mix.exs file and run mix deps.get.
defp deps do [{:guardsafe, "~> 0.5.0"}]endImport all the macros…
defmodule MyModule do import Guardsafe…or just the ones you need.
defmodule MyOtherModule do import Guardsafe, only: [divisible_by?: 2, integer?: 1]Now go forth and make your guard clauses more readable!
def leap_year?(year) when not integer?(year), do: raise "That's not a proper year!"def leap_year?(year) when divisible_by?(year, 400), do: truedef leap_year?(year) when divisible_by?(year, 100), do: falsedef leap_year?(year), do: divisible_by?(year, 4)Documentation for each macro is of course available in iex.
iex(1)> h Guardsafe.divisible_by?
defmacro divisible_by?(number, divisor)
Expands divisible_by?(number, divisor) into rem(number, divisor) == 0Available macros
Section titled “Available macros”NB: If a macro is translated into a function residing in another module
than Kernel you need to require it before use, e.g. require Integer.
Macros for checking types
Section titled “Macros for checking types”atom?/1- translates intoKernel.is_atom/1binary?/1- translates intoKernel.is_binary/1bitstring?/1- translates intoKernel.is_bitstring/1boolean?/1- translates intoKernel.is_boolean/1float?/1- translates intoKernel.is_float/1function?/1- translates intoKernel.is_function/1function?/2- translates intoKernel.is_function/2integer?/1- translates intoKernel.is_integer/1list?/1- translates intoKernel.is_list/1map?/1- translates intoKernel.is_map/1nil?/1- translates intoKernel.is_nil/1number?/1- translates intoKernel.is_number/1pid?/1- translates intoKernel.is_pid/1port?/1- translates intoKernel.is_port/1reference?/1- translates intoKernel.is_reference/1tuple?/1- translates intoKernel.is_tuple/1
Macros for checking values
Section titled “Macros for checking values”divisible_by?/2- checks whether two integers are evenly divisible.even?/1- returns true for even integers.odd?/1- returns true for odd integers.within?/3- checks whether a value is in the range of the last two arguments.
Macros for dates and times
Section titled “Macros for dates and times”date?/1- checks if the term is an Erlang-style date tuple.datetime?/1- checks if the term is an Erlang-style datetime tuple.time?/1- checks if the term is an Erlang-style time tuple.
These can come in handy when working with a library such as GoodTimes.
Why nil? and float? instead of is_nil and is_float
Section titled “Why nil? and float? instead of is_nil and is_float”While the Elixir core team thinks that nil? compared to is_nil is an inconcistency, others, especially Rubyists, might be more comfortable with the nil? notation. Honestly though, this is mostly intended as a display of how Elixir’s metaprogramming capabilities can be used to shape the look and feel of the language itself.
Online documentation
Section titled “Online documentation”For more information, see the full documentation.
Contributing
Section titled “Contributing”- Fork this repository
- Create your feature branch (
git checkout -b quis-custodiet-ipsos-custodes) - Commit your changes (
git commit -am 'Guards! Guards!') - Push to the branch (
git push origin quis-custodiet-ipsos-custodes) - Create a new Pull Request