神奇的Scala Pattern Matching

原文地址:http://kerflyn.wordpress.com/2011/02/14/playing-with-scalas-pattern-matching/

推荐阅读:http://ofps.oreilly.com/titles/9780596155957/RoundingOutTheEssentials.html  ==>   Pattern Matching

How many times have you been stuck in your frustration because you were unable to use strings as entries in switch-case statements. Such an ability would be really useful for example to analyze the arguments of your application or to parse a file, or any content of a string. Meanwhile, you have to write a series of if-else-if statements (and this is annoying). Another solution is to use a hash map, where the keys are those strings and values are the associated reified processes, for example a Runnable or a Callable in Java (but this is not really natural, long to develop, and boring too).

If a switch-case statement that accepts strings as entries would be a revolution for you, the Scala’s pattern matchingsays that this is not enough! Indeed, there are other cases where a series of if-else-if statements would be generously transformed into a look-alike switch-case statement. For example, it would be really nice to simplify a series ofinstanceof and cast included in if-else-if to execute the good process according to the type of a parameter.

In this post, we see the power of the Scala’s pattern matching in different use cases.

Notice that the pattern matching is not something new. It is something that appears already in some other languages like OCaml, Haskell. A kind of pattern matching also exits in Prolog, but it uses a far different mechanism (unification in this case).

Traditional approach

There is an approach to the Scala’s pattern matching that looks similar to the switch-case structure in C and Java: each case entry use an integer or any scalar type. Here is an example:

1 def toYesOrNo(choice: Int): String = choice match {
2     case 1 ="yes"
3     case 0 ="no"
4     case _ ="error"
5   }

So if  you enter toYesOrNo(1), Scala says ”yes”. And if you enter toYesOrNo(2), Scala says “error”. Notice that the symbol _ is used to formulate the default case.You also have to remark that there is no need to use the break statement.

But if you want that Scala says “yes” when you enter toYesOrNo(1), toYesOrNo(2), or toYesOrNo(3), you will write the function like this:

1 def toYesOrNo(choice: Int): String = choice match {
2     case 1 2 3 ="yes"
3     case 0 ="no"
4     case _ ="error"
5   }

Now, you can use a string for each case entry. Using strings is interesting when you want to parse options of your applications:

1 def parseArgument(arg: String) = arg match {
2     case "-h" "--help" => displayHelp
3     case "-v" "--version" => displayVerion
4     case whatever => unknownArgument(whatever)
5   }

So if you enter parseArgument(“-h”) or parseArgument(“–help”), Scala calls the displayHelp function. And if you enter parseArgument(“huh?”), Scala calls unknownArgument(“huh?”).

Notice that I have not used _ for the default case. Instead, I have put an identifier as its value is used in the associated instruction.

Typed pattern

Sometimes in Java, you have to deal with an instance of something that appears like an Object or any high level classes or interfaces. After checking for nullity, you have to use series of if-else statements and instanceof-cast to check the class or the interface that the instance extends or implements, before using it and processing the instance correctly. This the case when you have to override the equals() method in Java. Here is the way Scala sees the thing:

1 def f(x: Any): String = match {
2     case i:Int ="integer: " + i
3     case _:Double ="a double"
4     case s:String ="I want to say " + s
5   }
  • f(1) → “integer: 1″
  • f(1.0) → “a double”
  • f(“hello”) → “I want to say hello”

Is not it better than a succession of if+instanceof+cast?

This kind of pattern matching is useful to walk through a structure using the composite design pattern. For example, you can use it to explore the DOM of an XML document or the object model of a JSON message.

Functional approach to pattern matching

A side effect of such a matching is that it provides an alternative way to design functions. For example, consider the factorial function. If you choose the recursive version, usually, you would define it like this:

1 def fact(n: Int): Int =
2     if (n == 01
3     else n * fact(n - 1)

But you can use Scala’s pattern matching in this case:

1 def fact(n: Int): Int = match {
2     case 0 =1
3     case => n * fact(n - 1)
4   }

Notice the use of the variable n in this case. It is matched with any value that does not appears in the preceding cases. You have to understand that the n in the last case is not the same as the one used in the function signature. If you wanted to use the parameter n and not a new variable with the same name, you have to delimit the variable name with back-quote in the case definition, like this: `n`

Here is a simple way to build you factorial function in Scala:

1 def fact(n) = (1 to n).foldLeft(1) { _ _ }

Pattern matching and collection: the look-alike approach

Pattern matching may be applied to the collections. Below is a function that computes the length of a list without pattern matching:

1 def length[A](list : List[A]) : Int = {
2     if (list.isEmpty) 0
3     else 1 + length(list.tail)
4   }

Here is the same function with pattern matching:

1 def length[A](list : List[A]) : Int = list match {
2     case _ :: tail =1 + length(tail)
3     case Nil =0
4   }

In this function, there are two cases. The last one checks if the list is empty with the Nil value. The first one checks if there is at least one element is the list. The notation _ :: tail should be understood “a list with whatever head followed by a tail”. Here the tail can be Nil (ie. empty list) or a non-empty list.

To go further, we will use this approach with tuples in order to improve our method parserArgument() above:

1 def parseArgument(arg : String, value: Any) = (arg, value) match {
2   case ("-l", lang) => setLanguageTo(lang)
3   case ("-o" "--optim", n : Int) if ((0 < n) && (n <= 5)) => setOptimizationLevelTo(n)
4   case ("-o" "--optim", badLevel) => badOptimizationLevel(badLevel)
5   case ("-h" "--help"null=> displayHelp()
6   case bad => badArgument(bad)
7 }

Notice first the use of the operator | that allows to match alternative forms of arg inside the tuple. Notice also the use of two patterns for options -o and --optim. These patterns are distinguished by the use of a guard condition. The guard conditions help you to get fine tuned pattern matching when pattern matching alone is not enough.

Advanced pattern matching: case class

Case classes are classes with part of their behavior predefined in order to make easier their construction and their use in a pattern. Case classes enables you to manipulate parameterized symbols for example, parsed by a compiler or used by your internal Domain Specific Language (DSL).

The example below shows how to use case classes and pattern matching in order to build simple mathematical expressions, evaluate them, and compute their derivative. First, lets define the symbol to represent expressions: the variable X, constants, addition, multiplication, and the negative operator (for the fun!). Here sealed means that there is no other children of the class Expression outside of the namespace.

1 sealed abstract class Expression
2 case class X() extends Expression
3 case class Const(value : Int) extends Expression
4 case class Add(left : Expression, right : Expression) extends Expression
5 case class Mult(left : Expression, right : Expression) extends Expression
6 case class Neg(expr : Expression) extends Expression

Now, lets define a function to evaluate expressions with a given value for the variable by using pattern matching.

1 def eval(expression : Expression, xValue : Int) : Int = expression match {
2   case X() => xValue
3   case Const(cst) => cst
4   case Add(left, right) => eval(left, xValue) + eval(right, xValue)
5   case Mult(left, right) => eval(left, xValue) * eval(right, xValue)
6   case Neg(expr) => - eval(expr, xValue)
7 }

Lets try the eval() function:

1 val expr = Add(Const(1), Mult(Const(2), Mult(X(), X())))  // 1 + 2 * X*X
2 assert(eval(expr, 3== 19)

Now, we define a function that compute the (unreduced) derivative of an expression:

1 def deriv(expression : Expression) : Expression = expression match {
2   case X() => Const(1)
3   case Const(_=> Const(0)
4   case Add(left, right) => Add(deriv(left), deriv(right))
5   case Mult(left, right) => Add(Mult(deriv(left), right), Mult(left, deriv(right)))
6   case Neg(expr) => Neg(deriv(expr))
7 }

Lets try the deriv() function:

1 val df = deriv(expr)

Here is what you get in df:

1 Add(Const(0),Add(Mult(Const(0),Mult(X(),X())),Mult(Const(2),Add(Mult(Const(1),X()),Mult(X(),Const(1))))))
2 // = 0 + (0 * X*X + 2 * (1*X + X*1)) = 4 * X
1 assert(eval(df, 3), 12)

Other advanced pattern matching features

There are some special notations used in the Scala’s pattern matching. Scala allows to put aliases on patterns or on parts of a pattern. The alias is put before the part of the pattern, separated by @. For example, in the expressionaddress @ Address(_, _, "Paris", "France"), we want any addresses in Paris/France, but we do not care about its content after the match is done. So after, we just use the alias address.

There is also a specific notation to match sequences with _*. It matches zero, one or more elements in a sequence to its end.

In Scala, pattern matching does not appears only after a the function match. You can put pattern matching in any closure and also in a catch block.

You can provide your own behavior for pattern matching. This is called extractor. To do so, you have to provide your own implementation of the unapply() method (and/or unapplySeq() for a sequence).

Conclusion

In this post, we have seen different ways to use the pattern matching with Scala. The main advantage of such a feature is to provide an easy way to build alternative structures based the matching of scalar values, strings, collections, but also types and parameterized symbols. For me, pattern matching is one of the sexiest alternatives to if statements! A good use of pattern matching can make your program really readable and helps you to build an internal DSL.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值