Skip to content

Instantly share code, notes, and snippets.

@mrroot5
Last active January 28, 2022 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrroot5/72e5568efc3c26261e2472729aa29582 to your computer and use it in GitHub Desktop.
Save mrroot5/72e5568efc3c26261e2472729aa29582 to your computer and use it in GitHub Desktop.
Elixir util / helper functions. Keywords: elixir, helpers, utils, elixir utils, elixir helpers.

Intro

Functions to help with common tasks on Elixir.

Utilities

What is Credo?

Credo is a tools to improve your cod, refactor, consistency, etc.

Why typeof show a CyclomaticComplexity warning?

When a function has >= 9 conditions (by default is 9) to accomplish a warning was shown.

In this function we have 9 conditions on our cond that is the reason. If you remove, for example, true -> nil the warning is removed.

This warning is showed because on Elixir this type of practice are...disgunting.

Source https://hexdocs.pm/credo/Credo.Check.Refactor.CyclomaticComplexity.html

Sources

defmodule Utils do
@moduledoc """
Utility functions for Elixir.
"""
@doc """
Returns a string with variable type name or nil if not found
"""
@spec typeof(any()) :: String.t()
def typeof(myterm) do
cond do
is_float(myterm) -> :float
is_number(myterm) -> :number
is_atom(myterm) -> :atom
is_boolean(myterm) -> :boolean
is_binary(myterm) -> :binary
is_function(myterm) -> :function
is_list(myterm) -> :list
is_tuple(myterm) -> :tuple
true -> nil
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment