ExtraEnum.Mailbox (ExtraEnum v0.1.1) View Source

Enumerates against the mailbox by matching.

Can match a single pattern or multiple patterns. Arbitrary code can be executed when using the multiple pattern syntax, giving feature parity with Kernel.SpecialForms.receive/1.

Stops enumerating when the end of the mailbox is reached. Can be asked to delay momentarily to allow late messages to filter in.

Examples

Single Pattern Match

iex> require ExtraEnum.Mailbox, as: Mailbox
iex> send(self(), {:message, :test1})
{:message, :test1}
iex> send(self(), {:message, :test2})
{:message, :test2}
iex> Mailbox.match({:message, _}) |> Enum.to_list()
[{:message, :test1}, {:message, :test2}]

Multiple Pattern Match

iex> require ExtraEnum.Mailbox, as: Mailbox
iex> send(self(), {:message, :test1})
{:message, :test1}
iex> send(self(), {:message, :test2})
{:message, :test2}
iex> Mailbox.match do
...>   {:message, :test1} -> 1
...>   {:message, :test2} -> 2
...> end |> Enum.to_list()
[1, 2]

Link to this section Summary

Functions

Create an enumerable that will match a given pattern in the process mailbox.

Like match/2, but only accepts the multiple match syntax.

Like match/2, but only accepts the single match syntax.

Link to this section Functions

Link to this macro

match(arg1, arg2 \\ nil)

View Source (macro)

Create an enumerable that will match a given pattern in the process mailbox.

Accepts as parameters:

  • a single pattern as the first argument
  • multiple patterns and result values as a do block (like Kernel.SpecialForms.case/2)
  • a keyword list of options

It's not allowed to use the single and multiple pattern syntax at the same time.

Options:

  • delay - waits a given number of milliseconds before ending enumeration to allow late messages to trickle in.
Link to this macro

match_many(opts \\ [], list)

View Source (macro)

Like match/2, but only accepts the multiple match syntax.

Link to this macro

match_single(single_pattern, opts \\ [])

View Source (macro)

Like match/2, but only accepts the single match syntax.