Grizzly.ZWave.ZWEnum (grizzly v9.1.0)

Copy Markdown View Source

A bidirectional map for Z-Wave enumerations.

Summary

Functions

Checks if two enums are equal.

Gets the value represented by the given key.

Gets the value represented by the given key, raising if the key is not found.

Gets the key represented by the given value.

Gets the key represented by the given value, raising if the value is not found.

Gets the value for the given key.

Gets the key for the given value.

Checks if the enum has the given key.

Returns a list of all keys in the enum.

Convenience shortcut for member?/3.

Checks if enum contains {key, value} pair.

Creates a new empty ZWEnum.

Creates a new ZWEnum from an enumerable of {key, value} pairs.

Creates a new ZWEnum from an enumerable, transforming each item with the provided function.

Inserts the given {key, value} pair into the enum.

Returns the number of key-value pairs in the enum.

Converts the enum to a list of {key, value} pairs.

Returns a list of all values in the enum.

Types

k()

@type k() :: atom()

t()

@type t() :: %Grizzly.ZWave.ZWEnum{
  keys: %{optional(atom()) => non_neg_integer()},
  values: %{optional(non_neg_integer()) => atom()}
}

v()

@type v() :: non_neg_integer()

Functions

decode(enum, v)

@spec decode(t(), v()) :: {:ok, k()} | :error

Alias for fetch_key/2.

decode!(enum, v)

@spec decode!(t(), v()) :: k()

Alias for fetch_key!/2.

encode(enum, k)

@spec encode(t(), k()) :: {:ok, v()} | :error

Alias for fetch/2.

encode!(enum, k)

@spec encode!(t(), k()) :: v()

Alias for fetch!/2.

equal?(zw_enum1, zw_enum2)

@spec equal?(t(), t()) :: boolean()

Checks if two enums are equal.

fetch(zw_enum, k)

@spec fetch(t(), k()) :: {:ok, v()} | :error

Gets the value represented by the given key.

Examples

iex> enum = new([a: "foo", b: "bar"])
iex> fetch(enum, :a)
{:ok, "foo"}
iex> fetch(enum, :c)
:error

fetch!(enum, k)

@spec fetch!(t(), k()) :: v()

Gets the value represented by the given key, raising if the key is not found.

Examples

iex> enum = new([a: "foo", b: "bar"])
iex> fetch!(enum, :a)
"foo"

fetch_key(zw_enum, v)

@spec fetch_key(t(), v()) :: {:ok, k()} | :error

Gets the key represented by the given value.

Examples

iex> enum = new([a: "foo", b: "bar"])
iex> fetch_key(enum, "foo")
{:ok, :a}
iex> fetch_key(enum, "baz")
:error

fetch_key!(enum, v)

@spec fetch_key!(t(), v()) :: k()

Gets the key represented by the given value, raising if the value is not found.

Examples

iex> enum = new([a: "foo", b: "bar"])
iex> fetch_key!(enum, "foo")
:a

get(enum, key, default \\ nil)

@spec get(t(), k(), v() | nil) :: v() | nil

Gets the value for the given key.

Examples

iex> enum = new([a: "foo", b: "bar"])
iex> get(enum, :a)
"foo"
iex> get(enum, :c, "default")
"default"

get_key(enum, value, default \\ nil)

@spec get_key(t(), v(), k()) :: k() | nil

Gets the key for the given value.

Examples

iex> enum = new([a: "foo", b: "bar"])
iex> get_key(enum, "foo")
:a
iex> get_key(enum, "baz", :default)
:default

has_key?(zw_enum, k)

@spec has_key?(t(), k()) :: boolean()

Checks if the enum has the given key.

keys(zw_enum)

@spec keys(t()) :: [k()]

Returns a list of all keys in the enum.

Examples

iex> enum = new([a: "foo", b: "bar"])
iex> keys(enum) |> Enum.sort()
[:a, :b]

member?(enum, kv)

@spec member?(
  t(),
  {k(), v()}
) :: boolean()

Convenience shortcut for member?/3.

Examples

iex> enum = new([a: "foo", b: "bar"])
iex> member?(enum, {:a, "foo"})
true
iex> member?(enum, {:a, "bar"})
false

member?(enum, key, value)

@spec member?(t(), k(), v()) :: boolean()

Checks if enum contains {key, value} pair.

Examples

iex> enum = new([a: "foo", b: "bar"])
iex> member?(enum, :a, "foo")
true
iex> member?(enum, :a, "bar")
false

new()

@spec new() :: t()

Creates a new empty ZWEnum.

new(enumerable)

@spec new(Enum.t()) :: t()

Creates a new ZWEnum from an enumerable of {key, value} pairs.

new(enum, transform)

@spec new(Enum.t(), (term() -> {k(), v()})) :: t()

Creates a new ZWEnum from an enumerable, transforming each item with the provided function.

Examples

iex> enum = new([:a, :b, :c], fn item -> {item, Atom.to_string(item)} end)
iex> to_list(enum) |> Enum.sort()
[a: "a", b: "b", c: "c"]

put(enum, k, v)

@spec put(t(), k(), v()) :: t()

Inserts the given {key, value} pair into the enum.

Examples

iex> enum = new()
iex> enum = put(enum, :a, "foo")
iex> enum = put(enum, :b, "bar")
iex> to_list(enum) |> Enum.sort()
[a: "foo", b: "bar"]

size(zw_enum)

@spec size(t()) :: non_neg_integer()

Returns the number of key-value pairs in the enum.

Examples

iex> enum = new([a: "foo", b: "bar"])
iex> size(enum)
2

to_list(zw_enum)

@spec to_list(t()) :: [{k(), v()}]

Converts the enum to a list of {key, value} pairs.

Examples

iex> enum = new([a: "foo", b: "bar"])
iex> to_list(enum) |> Enum.sort()
[a: "foo", b: "bar"]

values(zw_enum)

@spec values(t()) :: [v()]

Returns a list of all values in the enum.

Examples

iex> enum = new([a: "foo", b: "bar"])
iex> values(enum) |> Enum.sort()
["bar", "foo"]