DevHeads.net

Qo 0.5.0 API

*What's Qo?*

Qo is a pattern matching implementation in pure Ruby.

It lets you do things like this:

Qo.case(['Foo', 42]) { |m|
m.when(/^F/, Any) { 'Foo!' }
m.else { 'Not foo...' }
}
=> 'Foo!'

[['Foo', 42], ['Bar', 24]].map(&Qo.match { |m|
m.when(/^F/, Any) { 'Foo!' }
m.else { 'Not foo...' }
})
=> ['Foo!', 'Not foo...']

The syntax purposely mimics case statements.

*RFC*

Working on refining the API for Qo, mostly modeled after the 2.6+ potential
Hash#=== and Array#=== features. If these two are merged, Qo will likely
get substantially faster. The only thing these don't do are Hash vs Object
matches.

I'd be curious to get any opinions on the API so far, as I want to drive it
a bit more towards a stable set going into the next few versions before I
pull the 1.0.0 switch.

<a href="https://github.com/baweaver/qo/pull/20" title="https://github.com/baweaver/qo/pull/20">https://github.com/baweaver/qo/pull/20</a>

So if you have any opinions on pattern matching in general, I'd be
interested to hear them

- Brandon (baweaver)

Comments

Re: Qo 0.5.0 API

By Don Wilde at 08/07/2018 - 10:08

I like the when... else syntax. I'd add a negation 'when not'. Do you
want to have multi-level matching or not? ('else if') Personally, I
think not. The simplicity of what you are starting with is compelling.

On 8/7/18, Brandon Weaver < ... at gmail dot com> wrote:

Re: Qo 0.5.0 API

By Brandon Weaver at 08/07/2018 - 13:19

That's the fun thing, Qo has inverse matchers and or matchers as well:

Qo.case(['Foo', 42]) { |m|
m.when(Qo.not(/^F/, Any)) { 'Not foo...' }
m.else { 'Foo!' }
}
=> 'Foo!'

I may make that more succinct later, but I'd need to think it through