Follow

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

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

@pyrmont It’s regex for data structures. I found this article that seems to do a good job diving in. zverok.github.io/blog/2015-07-

@pyrmont There are two distinct types of pattern matching, right and left hand.

Left hand is like `a, b = [1,2]`. It's often called destructuring. In JS it's really powerful: `let { a, b } = { a: 1, b: 2 }` where a becomes 1 and b becomes 2.

Ruby has some of this with splat (`*`) and kwsplat (`**`) in conjunction with the implied `to_ary` and explicit `to_a` type methods. That's a rabbit hole though.

@pyrmont Right hand is a lot of what Qo does, which was mentioned earlier.

It's goal is to say the thing on the left matches the thing on the right.

`Qo[a: Integer, b: Integer] === { a: 1, b: 2 }`

This can be made into a case statement or more complicated expression.

The reason Qo is so handy is it destructures objects methods to do it as well.

@pyrmont I may do a more conclusive writeup on what pattern matching means and why you might like it. @zverok on Twitter has done some good work in this area too

Sign in to participate in the conversation
Ruby.social

A Mastodon instance for Rubyists & friends