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 > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "definitions of interpreters, variables and functions in Scala". In the actual operation process of cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
Step 1: Learn to use the Scala interpreter
The easiest way to get started with Scala is with the Scala interpreter, an interactive "shell" for writing Scala expressions and programs. Simply enter an expression into the interpreter, which will evaluate the expression and print the resulting value. Scala's interactive shell is called scala. You can use it by typing scala at the command prompt:
$ scala Welcome to Scala version 2.7.2. Type in expressions to have them evaluated. Type :help for more information. scala>
After you type an expression, such as 1 + 2, and type Enter:
scala> 1 + 2
The interpreter will print:
res0: Int = 3
This row includes:
An automatically generated or user-defined name describing the calculated value (res0, for result 0),
A colon (:) followed by the type of expression (Int),
An equal sign (=),
Compute the result of the expression (3).
The Int type refers to the class Int of the scala package. Scala packages are similar to Java packages: they partition the global namespace and provide mechanisms for information hiding. The value of class Int corresponds to the int value of Java. More broadly, all Java primitive types have corresponding classes in the scala package. For example, scala.Boolean corresponds to boolean in Java. scala.Float corresponds to Java float. When you compile your Scala code into Java bytecode, the Scala compiler will use Java's primitive types to reap the performance benefits.
The resX identifier is also used in subsequent lines of code.
For example, since res0 was previously set to 3, res0 * 3 is 9:
scala> res0 * 3 res1: Int = 9
Hello, world! Message, input:
scala> println("Hello, world! ") Hello, world!
println prints the string passed to it on standard output, just like System.out.println in Java.
Step 2: Define some variables
Scala has two variables, val and var. val is similar to the final variable in Java. Once initialized, val cannot be assigned. var is like a non-final variable in Java. var can be assigned multiple times during its lifetime. Here is the definition of val:
scala> val msg = "Hello, world! " msg: java.lang.String = Hello, world!
This statement introduces msg as the string "Hello, world! "The name. The type is java.lang.String, because Scala strings are implemented by Java's String class.
If you've defined Java variables before, you'll notice a striking difference: neither java.lang.String nor String appear in val's definition. This example demonstrates type inference, Scala's ability to automatically understand types you omit. In this case, because you initialized msg with a string literal, Scala infers that msg is of type String. If the Scala interpreter (or compiler) can infer types, let it do so instead of writing unnecessary explicit type annotations, which are often *** choices. However, you can define types explicitly if you want to, and maybe you should sometimes. Explicit type annotations not only ensure that the Scala compiler infers your preferred type, but also serve as useful documentation for future code readers. Unlike Java, where the type of a variable is specified before its name, Scala has the type of a variable separated by a colon after its name. For example:
scala> val msg2: java.lang.String = "Hello again, world! " msg2: java.lang.String = Hello again, world!
Or, since the simplified name of the java.lang type is also visible in Scala, it can be simplified to:
scala> val msg3: String = "Hello yet again, world! " msg3: String = Hello yet again, world!
Go back to the original msg, now that it's defined, you can use it as you want, such as:
scala> println(msg) Hello, world!
What you can't do with msg, because it's val and not var, is assign it a value again. For example, look at what the compiler says when you try:
scala> msg = "Goodbye cruel world! " :5: error: reassignment to val msg = "Goodbye cruel world! " ˆ
If reassignment is what you need, you should use var, as follows:
scala> var greeting = "Hello, world! " greeting: java.lang.String = Hello, world!
Since greeting is var and not val, you can reassign it later. For example, if you are upset later, you can modify your greeting to:
scala> greeting = "Leave me alone, world! " greeting: java.lang.String = Leave me alone, world!
To type something that spans multiple lines, just type in one line at a time. If the input does not end at the end of the line, the interpreter responds with a vertical bar on the next line.
scala> val multiLine = | "This is the next line. " multiLine: java.lang.String = This is the next line.
If you realize you typed something wrong and the interpreter is still waiting for more input, you can cancel it by pressing Enter twice:
scala> val oops = | | You typed two blank lines. Starting a new command. scala>
Later in the book, we'll omit vertical bars to make the code easier to read (and easier to copy and paste from PDF ebooks into the interpreter).
Step 3: Define some functions
Now that you've used Scala's variables, you might want to write some functions. Here's how it works in Scala:
scala> def max(x: Int, y: Int): Int = { if (x > y) x else y } max: (Int,Int)Int
The definition of a function begins with def. The function name, max in this case, is followed by a colon-delimited list of arguments in parentheses. Each function argument must be followed by a type annotation prefixed with a colon, because the Scala compiler (and interpreter, but we'll just say compiler later) can't infer function argument types. In this example, the function named max takes two arguments, x and y, both of type Int. After the parentheses in the max parameter list you will see another ": Int" type annotation. This defines the result type of the max function: result type. Following the result type of the function is an equal sign and a pair of curly braces enclosing the body of the function. In this case, there is only one if expression in the body of the function, and choosing x or y, whichever is greater, is treated as the result of the max function. As demonstrated here, Scala's if expression can produce a value just like Java's ternary operator. For example, the Scala expression "if (x > y) x else y" is the same as the Java expression "(x > y) ?" x : y"behaves like that. In the world view of functional programming that the equal sign before the function body suggests, a function defines an expression that produces a value. The basic structure of the function is illustrated in Figure 2.1.
Sometimes the Scala compiler will require you to define the result type of a function. For example, if the function is recursive, you must explicitly define the result type of the function. In the max case, however, you don't have to write the result type; the compiler can infer it. Similarly, if the function consists of only one sentence, you can optionally leave out the braces. So you can write the max function as follows:
scala> def max2(x: Int, y: Int) = if (x > y) x else y max2: (Int,Int)Int
Once you define a function, you can call it by its name, such as:
scala> max(3, 5) res6: Int = 5
There are also function definitions that take neither arguments nor return useful results:
scala> def greet() = println("Hello, world! ") greet: ()Unit
When you define the greet() function, the interpreter responds with a greet: ()Unit. "Greet" is, of course, the name of the function. Blank parentheses indicate that the function takes no arguments. Unit is the result type of greets. The result type of Unit means that the function does not return a useful value. Scala's Unit type is closer to Java's void type, and virtually every Java method that returns void is mapped to a Scala method that returns Unit. Therefore methods with a result type of Unit are run only for their side effects. In the greet() example, the side effect is to print a polite auxiliary on standard output.
Next, you'll put Scala code in a file and execute it as a script. If you want to leave the interpreter, type:quit or:q.
Scala> :quit $"Interpreters, variables and functions in Scala" is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
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.