Pattern Matching in Cyclops for Java 8
One of the most repeated pattern matching examples is the creation of a simple arithmetic parser for example see ( François Sarradin’s Playing With Scala Pattern Matching, Neal Gafter’s Proposal for Pattern Matching and Record Types (C#), Emir, Odersky and Williams Matching Objects with Patterns ). This blog converts those examples to Java 8 using Cyclops Pattern Matching.
Given a case class hierarchy that represents arithmetic expressions we can use pattern matching to construct a arithmetic expression parser. In Java 8 we can use Lombok to create a sealed type hierarchy.
Our parser, written with Cyclops Pattern Matching would then look something like this :-
François Sarradin in his blog takes this a step further to calculate the derivative of an expression, converted to Cyclops, this looks like so
Neal Gafter’s proposal and the Matching Objects with Patterns also show what Expression Simplification would look like with Pattern Matching. This requires the ability to decompose an Object and match against it’s properties, as well as the ability to match using wildcards.