Does anyone have any recommendations for better understanding pattern matching? I don't understand why it's valuable but see smart people talking about it.

@pyrmont IMO is not about smart people thinking is good or not. In my experience is more about why is worthy at some use case.

Perhaps a view from other languages as Rust give more ideas...

doc.rust-lang.org/book/second-

In rust the most used is for a powerful case dispatcher. But is not only that, can go beyond that. A clear advantage goes on a code with less if-else-elseif...

... continues

Follow

@pyrmont Now in Elixir:

elixir-lang.org/getting-starte

Is also used for many cases, as head / tail (who needs recursion?) to a functions-acting-as-overloading, but powerful and with a lot of sense:

def zero?(0) do : true
def zero?(x) when is_integer(x): false

In the above code the invocation will be called with the code required, no `if` statements involved because we are using pattern matching.

>zero(0) # will call 1st def
>zero(10) # will call the 2nd

More about:

quickleft.com/blog/pattern-mat

@pyrmont so, how this is good or bad? it depends. But most of the cases is a more clear design, and in some a way better way to solve problems involving flow of 'instant' dispatchers with a defined pattern.

A promised no-if-statments-needed coding style? maybe.

What do we have on ? Since is not bundle on the language we have been developing modules and classes trying to implement this ideas.

dry-rb.org/gems/dry-matcher/
github.com/pzol/deterministic

Basic idea: Create a Result object, then match.

@pyrmont also keep an eye on what @baweaver has with Qo:

github.com/baweaver/qo

He has been doing a lot of talks on this, you can check almost everywhere :)

@esparta @pyrmont See the articles in the README of that gem, I have a few that explain some of how it works in there.

If you'd like me to explain anything else in detail on that note, let me know :)

@esparta I generally like pattern matching, but don't most examples violate the Tell Don't Ask principle? The complex matching logic should be done within the object, instead of outside.

Sign in to participate in the conversation
Ruby.social

A Mastodon instance for Rubyists & friends