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...

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


@pyrmont Now in Elixir:

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:

@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.

Basic idea: Create a Result object, then match.

@pyrmont also keep an eye on what @baweaver has with 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

A Mastodon instance for Rubyists & friends