maybe v1.0.0 Maybe

Access nested maps and structs, protected from nil.

See maybe/1 or maybe/2 for more details.

Example

import Maybe

map = %{city: %{name: "Portland"}}
maybe(map, [:city, :name]) # => "Portland"
maybe(map.city.name) # => "Portland"
maybe(map.city.location) # => nil

Link to this section Summary

Functions

Get a value out of a nested map or struct, or return nil

Get a value out of a nested map or struct, or return nil

Link to this section Functions

Link to this macro maybe(ast) (macro)

Get a value out of a nested map or struct, or return nil.

Compiles down to maybe/2. In other words, this:

maybe(map.city.name)

Is compiled down to a simple function call to maybe/2:

maybe(map, [:city, :name])

Examples

iex> map = %{city: %{name: "Portland"}}
...> maybe(map.city.name)
"Portland"

iex> map = %{city: nil}
...> maybe(map.city.name)
nil
Link to this function maybe(map, keys)
maybe(map(), keys :: [atom()]) :: any() | nil

Get a value out of a nested map or struct, or return nil.

For prettier syntax, see the maybe/1 macro.

Examples

iex> map = %{city: %{name: "Portland"}}
...> maybe(map, [:city, :name])
"Portland"

iex> maybe(%{}, [:city, :name])
nil