In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use the Case class in Scala. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Scala supports the concept of case classes. The case class is also a regular class, exposing its constructor parameters and providing a recursive deconstruction mechanism through pattern matching. The following is an example of a class hierarchy consisting of an abstract superclass Term and three concrete case classes Var,Fun, and App.
Abstract class Term case class Var (name: String) extends Term case class Fun (arg: String, body: Term) extends Term case class App (f: Term, v: Term) extends Term
This class hierarchy can be used to represent untyped lambda operators. In order to facilitate the construction of case class instances, Scala does not need to use the new primitive. Simply use the class name as a function. Examples are as follows:
Fun ("x", Fun ("y", App (Var ("x"), Var ("y")
The constructor parameters of the case class are treated as public values and can be accessed directly.
Val x = Var ("x") Console.println (x.name)
For each case class, the Scala compiler produces an equals method and a toString method that implements a structural equality check. Examples are as follows:
Val x1 = Var ("x") val x2 = Var ("x") val y1 = Var ("y") println ("+ x1 +" = = "+ x2 +" = > "+ (x1 = = x2)) println ("+ x1 +" = "+ y1 +" = > "+ (x1 = = y1)
Will print
Var (x) = = Var (x) = > true Var (x) = = Var (y) = > false
Defining a case class makes sense only if pattern matching is used when decomposing data structures. The following objects define a beautiful print function for the presentation of our lambda operator:
Object TermTest extends Application {def printTerm (term: Term) {term match {case Var (n) = > print (n) case Fun (x, b) = > print ("^" + x + ".") PrintTerm (b) case App (f, v) = > Console.print ("(") printTerm (f) print ("") printTerm (v) print (")}} def isIdentityFun (term: Term): Boolean = term match {case Fun (x, Var (y) if x = y = > true case _ = > false} val id = Fun (" x ") Var ("x")) val t = Fun ("x", Fun ("y", App (Var ("x"), Var ("y") printTerm (t) println println (isIdentityFun (id)) println (isIdentityFun (t))}
In our example, the function print is expressed as a pattern matching statement that starts with the match keyword and consists of a series of case Pattern = > Body clauses.
The above program also defines the function isIdentityFun, which is used to check whether a given term corresponds to a simple identity function. This example uses depth mode and escort statements. After matching the pattern with the given value, the guard statement (defined after the keyword if) is evaluated. If true is returned, the match succeeds; otherwise, the match fails and the next pattern is tried.
This is the end of this article on "how to use Case classes in Scala". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.