Modules and Named Functions

In Elixir, modules group functions together, much like a namespace. Usually, functions that reside in the same module are related to one another. You create a module using the defmodule construct:

iex> defmodule StringHelper do...>   def palindrome?(term) do...>     String.reverse(term) == term...>   end...> end{:module, StringHelper, <<70, 79, 82, 49, 0, 0, 4, 0, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 119, 0,   0, 0, 11, 19, 69, 108, 105, 120, 105, 114, 46, 83, 116, 114, 105, 110, 103,   72, 101, 108, 112, 101, 114, 8, 95, 95, ...>>, {:palindrome?, 1}}iex> StringHelper.palindrome?("abcd")falseiex> StringHelper.palindrome?("abba")true

In the preceding example we're also creating a function inside the StringHelper module, ...

Get Mastering Elixir now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.