All,

I wrote a macro which allows for using a pattern to essentially extract a 
value from a data structure:


@doc """
Filter a value out of data using a pattern with one variable. Returns a 
default value
if no match. Raises if pattern contains no variables or more than one 
variable.

## Examples

iex> "$3.45" |> PatternHelpers.pattern_filter("$" <> money) |> 
String.to_float()
3.45

iex> {1,2,3} |> PatternHelpers.pattern_filter({1,_,a})
3

iex> %{a: 1, b: 2} |> PatternHelpers.pattern_filter(%{a: 9, b: b})
nil

iex> %{a: 1, b: 2} |> PatternHelpers.pattern_filter(%{a: 9, b: b}, "???")
"???"
"""

And an unsafe version:

@doc """
See `pattern_filter/3`. Raises if no match.

## Examples

iex> {1,2,3} |> PatternHelpers.pattern_filter!({9,_,b})
** (MatchError) no match of right hand side value: {1, 2, 3}
"""

This is my first proposal. Please let me know if this idea is worth some 
attention, and how I might better do my part to present it. I have code 
obviously but I'm not sure this is the place for it.

Thanks,
Matt F

-- 
You received this message because you are subscribed to the Google Groups 
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elixir-lang-core+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/4b65083e-6c36-4519-a0ed-fa2da8b61a9bn%40googlegroups.com.

Reply via email to