I've implemented a Ruby Gem that provides the ultra super pattern-matching to Ruby. We can directly express non-linear pattern-matching against lists, multisets, and sets using this gem. Today, I'd like introduce the power of this gem.
This is open source software. You can access the source code on GitHub.
$ gem install egison
$ gem install bundler (if you need) $ echo "gem 'egison'" > Gemfile $ bundle install
The library provides
object, the argument of the
match expression is pattern-matched with a
pattern, the argument of
If a pattern matches, a block passed to
with is called and returns its result.
In our pattern-matching system, there are cases that pattern-matching has multiple results.
match_all calls the block passed to
with for each pattern-matching result and returns all results as an array.
match_all takes one single match-clause.
On the other hand,
match takes multiple match-clauses.
It pattern-matches from the first match-clause.
If a pattern matches, it calls the block passed to the matched match-clause and returns a result for the first pattern-matching result.
An element pattern matches the element of the target array.
A subcollection pattern matches the subcollection of the target array.
A subcollection pattern has
A literal that contain
_ ahead is a pattern-variable.
We can refer the result of pattern-matching through them.
We can write pattern-matching against lists, multisets, and sets. When we regard an array as a multiset, the order of elements is ignored. When we regard an array as a set, both of the duplicates and order of elements are ignored.
_ is a wildcard.
It matches with any object.
___ are also interpreted as a wildcard.
This is because
__ are system variables and sometimes have its own meaning in IRB.
_ is provided as syntactic sugar for
Non-linear pattern is the most important feature of our pattern-matching system.
Our pattern-matching system allows users multiple occurrences of same variables in a pattern.
A Pattern whose form is
__("...") is a value pattern.
In the place of
..., we can write any ruby expression we like.
It matches the target when the target is equal with the value that
... evaluated to.
When, the expression in the place of
... is a single variable, we can omit
") as follow.
We can do pattern-matching against streams with the
This feature is implemented by antimon2.
We can enumerates all combinations of the elements of a collection with pattern-matching.
We can write patterns for all poker-hands in one single pattern. It is as follow. Isn't it exciting?
The following code enumerates all twin primes with pattern-matching! I believe it is also a really exciting demonstration.
The system that realizes pattern-matching above is based on the theory of the Egison programming language. Egison is the pattern-matching-oriented, pure functional programming language. Actually, the expressive power of the original pattern-matching of Egison is more powerful. For example, we can do following things in the original Egison.
There is a new programming world! Please try it, too!