Java Reference
In-Depth Information
() -> {return defaultcode ;});
will determine whether e is a BinOp (and if so run binopcode, which has access to the fields of
the BinOp via identifiers op, l, r), or whether it is a Number (and if so run numcode, which has
access to the value n). The method even makes provision for defaultcode, which would be
executed if someone later created a tree node that was neither a BinOp nor a Number.
The following listing shows how to start using patternMatchExpr by simplifying addition and
multiplication expressions.
Listing 14.1. Implementing pattern matching to simplify an expression
You can now call the simplify method as follows:
You've seen a lot of information so far: higher-order functions, currying, persistent data
structures, lazy lists, and pattern matching! We now look at certain subtleties, which we've
deferred until the end to avoid overcomplicating the text.
 
Search WWH ::




Custom Search