This is an interesting idea, but maybe `then/2` (or case/2 if you're fine piping in it) could already cover these cases quite well (equivalent to your pattern_filter! function):
"$3.45" |> then(fn "$" <> money -> money end) |> String.to_float() "$3.45" |> case do "$" <> money -> money end |> String.to_float() The non-raising alternative might be slightly more verbose because you need a second clause. But most of the time (like your money example), the next step of the pipe might fail on nil anyway. Or maybe a with/1 pipeline might work better than the pipe operator if you want a succession of happy-path matches? If we move forward, it might be better to explicitly declare the returned expression, not just infer it from dangling variables: it isn't obvious what should `{a, b}` or `{a, b, a}` return. Something like: {1, 2, 1} |> pattern_filter(b <- {a, b, a}) Le mer. 14 déc. 2022 à 05:50, 'Matt Farabaugh' via elixir-lang-core < elixir-lang-core@googlegroups.com> a écrit : > 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 > <https://groups.google.com/d/msgid/elixir-lang-core/4b65083e-6c36-4519-a0ed-fa2da8b61a9bn%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- 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/CANnyohYy8hRvu2WD%2BMUEzWS2SxoA8eYk9PETOe8jGxr2tiuW%2BA%40mail.gmail.com.