MapsAsFunctions

A compile-time macro to build fast mapper and predicate functions from a map.

Usage

Define a map in your module using the macro MapsAsFunctions.defmap/2:

use MapsAsFunctions

defmap :bar, [
  a: 1,
  b: 2
]

MapsAsFunctions.defmap/2 will define, given a map :bar, a module-attribute @bar, which you can use as normal in further function definitions:

def bar_values do
  Map.values(@bar)
end

MapsAsFunctions.defmap/2 will also define, given a map :bar, the public functions bar/0, bar/1, and bar?/1:

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

iex> Foo.bar(:a)
1

iex> Foo.bar(:c)
nil

iex> Foo.bar?(:a)
true

iex> Foo.bar?(:c)
false

There will also be a strict version of bar/1, called bar!/1:

iex> Foo.bar!(:a)
1

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

…and a function bar_keys/0 that returns the keys of the map as a MapSet:

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

None of the above functions have any runtime logic; they are all expanded into a series of function-clauses at compile time.

Installation

Add maps_as_functions to your list of dependencies in mix.exs:

def deps do
  [
    {:maps_as_functions, "~> 0.1.0"}
  ]
end

Docs can be found at https://hexdocs.pm/maps_as_functions.