Category Archives: Pattern snippets

Data massaging in pipes with anonymous functions

This technique is obsolete since Elixir v.1.12, as it has introduced then/2 The pipe operator is great. So great, in fact, that sometimes you just want to keep that pipe juice flowing and feel bad about breaking it up just … Continue reading

Posted in Elixir, Pattern snippets | Comments Off on Data massaging in pipes with anonymous functions

Finer control in `with` failed matches

Elixir gives us the with construct to combine matching clauses, which is very handy as programs frequently to perform an operation only if/when a set of preconditions is met. Let’s say we want to charge a product to a credit card: … Continue reading

Posted in Elixir, Pattern snippets | Comments Off on Finer control in `with` failed matches

Pattern matching in function heads: don’t go overboard

Elixir’s pattern matching is great, but make sure it’s not reducing your code’s readability. In particular, just because you can match all the variables you’ll need from within the function’s head doesn’t mean you should. Take this code: def handle_call(:checkout, … Continue reading

Posted in Elixir, Pattern snippets | Comments Off on Pattern matching in function heads: don’t go overboard

Keyword list reduction

Keyword lists are often used to provide optional values, which are then processed (for example to initialize the state). One really nice way to do so, is with a reduce pattern. I’ve never seen anyone bringing attention to it in … Continue reading

Posted in Elixir, Pattern snippets | Comments Off on Keyword list reduction