- Hands-On Data Analysis with Scala
- Rajesh Gupta
- 606字
- 2021-06-24 14:51:06
Scala case classes
Scala case classes provide a convenient mechanism to work with objects that hold values. Let's look at an example in Scala REPL. The case class defined in the following code will be used in other example codes in this chapter:
scala> case class Person(fname: String, lname: String, age: Int)
defined class Person
scala> val jon = Person("Jon", "Doe", 21)
jon: Person = Person(Jon,Doe,21)
In the preceding example, we have defined a Scala case class called Person with three attributes, namely fname, lname, and age. We created an instance, jon, of the Person class without using the new keyword. Also, note that the jon object's attributes are printed out in a easy-to-use form. There are several such convenient features associated with Scala case classes that are extremely beneficial for programmers in general, particularly someone who deals with data.
Let's look at another convenient feature of Scala case classes, namely the copy object. We'll copy a Scala case class object by updating only the fname attribute using the following code:
scala> case class Person(fname: String, lname: String, age: Int)
defined class Person
scala> val jon = Person("Jon", "Doe", 21)
jon: Person = Person(Jon,Doe,21)
scala> val jonNew = jon.copy(fname="John")
jonNew: Person = Person(John,Doe,21)
This feature comes in really handy during data processing when we work with a template representation and generate specific instances from a template by updating a subset of attributes.
Another great feature of case classes is pattern matching, which helps in writing flexible code that is easier to work with. Let's look at an example of pattern matching in Scala REPL, as shown in the following code:
scala> def isJon(p: Person) = {
| p match {
| case Person("Jon", _, _) => {println("I am Jon"); true}
| case Person(n,_,_) => {println(s"I am not Jon but I am ${n}"); false}
| case _ => {println("I am not Jon but I am something other than Person"); false}
| }
| }
isJon: (p: Person)Boolean
scala> val jon = Person("Jon", "Doe", 25)
jon: Person = Person(Jon,Doe,25)
scala> isJon(jon)
I am Jon
res13: Boolean = true
scala> val bob = Person("Bob", "Crew", 27)
bob: Person = Person(Bob,Crew,27)
scala> isJon(bob)
I am not Jon but I am Bob
res14: Boolean = false
scala> isJon(null)
I am not Jon but I am something other than Person
res16: Boolean = false
We can explore the same example in the IDE, as shown in the following screenshot:

Using the IDE, we can clearly see the properties of the case class. Another great option is to use the Scala worksheet feature in IDE to explore this example, as shown in the following screenshot:

The preceding screenshot shows a fairly simple example of pattern matching using Scala case classes that illustrates the simplicity and power of this feature. In the data analysis world, pattern matching has been found to be extremely useful in solving certain categories of problems. Scala provides an intuitive, elegant, and simple way to take advantage of pattern matching.
Let's look at the preceding example in a bit more detail, looking at the following lines:
- Line #4: case Person("Jon", _, _) means any person whose first name is Jon
- Line #7: case Person(n, _, _) means any person with the first name is extracted into variable n
- Line #10: case _ means anything that does not match line #4 and line #7
With classic pattern matching, it is generally necessary for you to write a significant amount of boilerplate code with if-then-else types of constructs. Scala and its case classes provide a concise and expressive way to solve this problem.
- ABB工業機器人編程全集
- Getting Started with MariaDB
- Google App Inventor
- 電腦上網直通車
- STM32G4入門與電機控制實戰:基于X-CUBE-MCSDK的無刷直流電機與永磁同步電機控制實現
- 21天學通ASP.NET
- 數據挖掘方法及天體光譜挖掘技術
- 網絡布線與小型局域網搭建
- Google SketchUp for Game Design:Beginner's Guide
- Docker on Amazon Web Services
- HTML5 Canvas Cookbook
- Mastering Exploratory Analysis with pandas
- 典型Hadoop云計算
- Mastering DynamoDB
- 淘寶網店頁面設計、布局、配色、裝修一本通