In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "the definition and use of scala methods and functions". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Scala method and function
Definition method
The basic format for defining a method is:
Def method name (parameter list): return value type = method body. The rule in Scala is that if there is no input parameter, try to define the method only with the method name.
Def add (x _ x+y//): Int = x+y// can also be defined as / / def add (x _ x+y//) = x+y// or def add (x _ (_ Y:Int) (multipier:Int): Int= (xonomy) * multipier println (addThenMultiply (1Power2) (3)) No-parameter method scala > def name:String = System.getProperty ("user.name") name: Stringscala > nameres2: String = applescala > name (): 9: error: not enough arguments for method apply: (index: Int) Char in class StringOps.Unspecified value parameter index. Name () ^ scala > def name (): String = System.getProperty ("user.name") name: () Stringscala > name () res4: String = applescala > nameres5: String = apple method body is the expression of multiline statements def getSquareString (input:Double): String = {val square = input*input square.toString} method with default values
Default parameter, can have more than one. No requirement for location
Def method1 = println ("a =" + a + "b =" + b + "c =" + c) / / call method1 (bread2) / / cannot method1 (2) / / or method1 (1jin2) / method1 (adept 1) / / or method1 (1) / method1 (censor 1) / method1) variable parameters or indefinite length parameters
Note 1: (the variable parameter must be the last parameter in the parameter list)
Note 2: (inside the function, the repeating parameter type is an array that declares the parameter type, but if you are given an array with variable parameters, a compilation error will be reported: type mismatch, you can pass parameters using the array name: _ *)
Def add (aString Int *) {for (I String) =... def bar (f: (Boolean,Double) = > List [String]) =.
The way the function is defined:
Val f1 = ((a+bval IntMagnebGranger Int) = > aplomb) val f2 = > a+bval f3 = (_: Int) + (_: Int) val f4: (Int,Int) = > Int = (_ + _) val f1 = (xRO Intjjjjjnt) = > x+y// calling function F1 (1GRO 2)
How it is defined:
Val f1: (Int,Int) = > Int= (XMague y) = > x+yval f2: (Int,Int) = > Int) = {(xMagne y) = > xrecoy} val f1 = new Function2 [Int,Int,Int] {def apply (XRO Int): Int= if (xx+1)
No-parameter function
Val getTheAnswer = () = > 42println (getTheAnswer ())
The difference between method and function
The syntax of the definition of a method and a function is different.
Methods are generally defined in a class, attribute, or object
Method can share properties within its class
In a functional programming language, a functional "first-class citizen" can call it, pass it, store it in a variable, or pass it to another function as an argument
Case: first define a method, and then pass the function to the method
Scala > def m (f: (Int,Int) = > Int) = f (2pje 6) m: (f: (Int,Int) = > Int) Intscala > val f2 = (XRV Int) = > x-yf2: (Int,Int) = > Int = m (f2) res17: Int =-4scala > def m (f: (Int,Int) = > Int) = f (2pr 6) m: (f: (Int,Int) = > Int) Intscala > def f2 (XRO Int) = x-yf2 Y: Int) Intscala > m (f2) res3: Int =-4scala > method is converted to a function
When a method is passed as an argument to a method or function. Method is converted to a function
Use the magic underscore _
Scala > def M1 (xx+ym1 Intjnt) Intscala > M1 _ res19: (Int, Int) = > Int = scala > M1 (_) res20: (Int, Int) = > Int =
A Function Type is (roughly) a type of the form (T1,..., Tn) = > U, which is a shorthand for the trait FunctionN in the standard library. Anonymous Functions and Method Values have function types, and function types can be used as part of value, variable and function declarations and definitions. In fact, it can be part of a method type.
A Method Type is a non-value type. That means there is no value-no object, no instance-with a method type. As mentioned above, a Method Value actually has a Function Type. A method type is a def declaration-everything about a def except its body.
Example:
Scala > def M1 (x:Int) = x+3m1: (x:Int) Int scala > val F1 = (x:Int) = > x+3f1: Int = > Int =
See, the method definition and function definition are shown on the parser signature of scala. Def M1 (x: Int) = xroom3 is a simple definition of method. M1: (x: Int) Int in signature indicates that method M1 has a parameter int parameter x, and the return value is int type.
Val F1 = (x: Int) = > xroom3 is the definition of function. F1: Int = > Int = in the signature of the parser indicates that the method body of function F1 accepts an int parameter, and the type of the output is int.
A summary can be drawn from the above example:
A method is a logical operation block with a parameter list (which can be absent) that starts with def, just like a member method in object or class.
A function is an anonymous method (with or without a parameter list) that assigns to a variable (or constant) and follows a logical code block with a = > conversion symbol. The logical block of code after the conversion symbol is written in the same way as the body part of method.
This is the end of the content of "definition and use of scala methods and functions". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.