The goal of this series is to redirect your awareness of functional thinking, to help you look at http://www.aliyun.com/zixun/aggregation/17253.html in a new way and to improve your daily coding capabilities. This series explores the concept of functional programming, the framework that allows functional programming in the Java language, the functional programming language that runs on the JVM, and the future direction of language design. This series is intended for developers who understand Java and how it works, but don't know much about using functional languages.
In the previous issue, I explored how to use Java generics to simulate pattern matching in Scala, allowing you to write concise, readable conditional statements. Scala pattern matching is an example of an alternative scheduling mechanism, and we use the "dispatch mechanism" as a broad term to describe the way in which various languages dynamically select behavior. This installment expands this discussion to show you how scheduling mechanisms in various functions JVM languages can achieve greater simplicity and flexibility than the Java language.
Improve scheduling with Groovy
In Java, conditional execution ends with an if statement, but in rare cases the switch statement is used. Because a long string of if statements makes readability very poor, Java developers begin to rely on the Gang of Loko (GoF) Factory (or Abstract Factory) pattern. If you use a language that contains more flexible decision expressions, you can further simplify your extensive code.
Groovy has a powerful switch statement that simulates the syntax of a Java switch statement, not the behavior, as shown in Listing 1:
Listing 1. Groovy's vastly improved switch statement
Class Lettergrade {def gradefromscore (score) {switch (score) {case 90..100:return ' A ' case 80..<90:return ' B ' case 70..& Lt;80:return "C" Case 60..<70:return "D" Case 0..<60:return "F" Case ~ "[ABCDFABCDF]": Return Score.touppercase () Default:throw new IllegalArgumentException ("Invalid Score: ${score}")}}
Groovy switch Statements accept a wide variety of dynamic types. In Listing 1, the score parameter should be either a number between 0 and 100, or a letter rating. In Java, you must terminate each case by using return or break, which follows the same fall-through semantics. But in Groovy, unlike Java, I can specify a range (90..100), not a range (80..<90), a regular expression (~ "[ABCDFABCDF]"), and a default condition.
Groovy's dynamic type allows me to send different types of arguments and respond in an appropriate way, as shown in the unit test in Listing 2:
Listing 2. Testing Groovy Letter Ratings
@Testpublic void Test_letter_grades () {def LG = new Lettergrade () assertequals ("A", Lg.gradefromscore ()) Assertequals ( "B", Lg.gradefromscore ()) assertequals ("D", Lg.gradefromscore ()) assertequals ("F", Lg.gradefromscore ("F"))}
A more powerful switch gives you a useful tradeoff between continuous if and Factory design patterns. Groovy's switch allows you to match ranges and other complex types, which are similar in intent to the patterns in Scala.