MapsAsFunctions v0.1.0 MapsAsFunctions View Source

Link to this section Summary

Functions

Defines a mapping function

Link to this section Functions

Link to this macro defmap(map_name, map_pairs) View Source (macro)

Defines a mapping function.

defmodule Foo do
  use MapsAsFunctions
  defmap :bar, [a: 1, b: 2]
end

Examples

Get the map:

iex> Foo.bar
%{a: 1, b: 2}

Get values from the map, with a default of nil (ala Map.get/1):

iex> Foo.bar(:a)
1

iex> Foo.bar(:c)
nil

Get values from the map or raise (ala Map.fetch!/1):

iex> Foo.bar!(:a)
1

iex> Foo.bar!(:c)
** (FunctionClauseError) no function clause matching in MapsAsFunctionsTest.Foo.bar!/1

Test for membership (ala Map.has_key?/1):

iex> Foo.bar?(:a)
true

iex> Foo.bar?(:c)
false

Get keys (ala Map.keys/1), as a compile-time-generated MapSet:

iex> Foo.bar_keys
#MapSet<[:a, :b]>