In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to write Scala scripts to achieve loops and enumerations". In daily operations, I believe many people have doubts about how to write Scala scripts to achieve loops and enumerations. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "how to write Scala scripts to achieve loops and enumerations". Next, please follow the editor to study!
Step 4: write some Scala scripts
Although Scala is designed to help programmers build very large-scale systems, it can also be reduced to the scale of scripting. A script is a sequence of sentences that are often executed and placed in a file. Put the following code in the hello.scala file:
Println ("Hello, world, from a script!")
Then run:
$scala hello.scala
So you'll get another message:
Hello, world, from a script!
The command-line arguments passed to the Scala script can be obtained through Scala's array named args. In Scala, the array starts with zero and accesses an element by specifying an index in parentheses. So the * * element of the array steps in Scala is steps (0), not like steps [0] in Java. As a test, enter the following into the new file helloarg.scala:
/ / say hello to * parameters
Println ("Hello," + args (0) + "!")
Then run:
Println ("Hello," + args (0) + "!")
In this command, "planet" is passed as a command-line argument and accessed as args (0) in the script. So, you will see:
Hello, planet!
Note that this script includes a comment. The Scala compiler ignores characters from / / to the end of the line and between / * and * /. This example also demonstrates the connection of String using the + operator. This is exactly what you expected. Expression "Hello,"+" world! " The string "Hello, world!" will be generated.
Step 5: use while loop; use if to judge
To try while, enter the following code in the printargs.scala file:
Var I = 0 while (I
< args.length) { println(args(i)) i += 1 } 注意 虽然本节的例子有助于解释while循环,但它们并未演示***的Scala风格。在下一段中,你会看到避免用索引枚举数组的更好的手段。 这个脚本开始于变量定义,var i = 0。类型推断认定i的类型是scala.Int,因为这是它的初始值的类型,0。下一行里的while结构使得代码块(大括号之间的代码)重复执行直到布尔表达式i < args.length为假。args.length给出了args数组的长度。代码块包含两句话,每个都缩进两个空格,这是Scala的推荐缩进风格。***句话,println(args(i)),输出第i个命令行参数。第二句话,i += 1,让i自增一。注意Java的++i和i++在Scala里不起作用,要在Scala里自增,必须写成要么i = i + 1,或者i += 1。用下列命令运行这个脚本: $ scala printargs.scala Scala is fun 你将看到: Scala is fun 想要更好玩儿一些,就把下列代码输入到新文件echoargs.scala: var i = 0 while (i < args.length) { if (i != 0) print(" ") print(args(i)) i += 1 } println() 在这个版本里,用print调用替代了println调用,这样所有参数将被输出在同一行里。为了更好的可阅读性,你应该用if(i != 0)检查,除了***个之外的每个参数前插入一个空格。由于***次做while循环时i != 0会失败,因此在头一个参数之前不会输出空格。***,你应该在末尾多加一个println,这样在输出所有参数之后会有一个换行。这样你的输出就非常漂亮了。如果用下面的命令运行脚本: $ scala echoargs.scala Scala is even more fun 就能得到: Scala is even more fun 注意Scala和Java一样,必须把while或if的布尔表达式放在括号里。(换句话说,就是不能像在Ruby里面那样在Scala里这么写:if i < 10。在Scala里必须写成if (i < 10)。)另外一点与Java类似的,是如果代码块仅有一个句子,大括号就是可选的,就像echoargs.scala里面if句子演示的。并且尽管你没有看到,Scala也和Java一样使用分号分隔句子的,只是Scala里的分号经常是可选的,从而可以释放你的右小手指。如果你有点儿罗嗦的脾气,那么就把echoargs.scala脚本写成下面的样子好了: var i = 0; while (i < args.length) { if (i != 0) { print(" "); } print(args(i)); i += 1; } println(); 第六步:用foreach和for枚举 尽管或许你没意识到,在前一步里写while循环的时候,你正在用指令式:imperative风格编程。指令式风格,是你常常使用像Java,C++和C这些语言里用的风格,一次性发出一个指令式的命令,用循环去枚举,并经常改变共享在不同函数之间的状态。Scala允许你指令式地编程,但随着你对Scala的深入了解,你可能常会发现你自己在用一种更函数式:functional的风格编程。实际上,本书的一个主要目的就是帮助你变得对函数式风格感觉像和指令式风格一样舒适。 函数式语言的一个主要特征是,函数是***类结构,这在Scala里千真万确。举例来说,另一种(简洁得多)打印每一个命令行参数的方法是: args.foreach(arg =>Println (arg))
In this line of code, you call the foreach method on args and pass it into the function. In this case, you pass in the function text with a parameter called arg: function literal. The function body is println (arg). If you enter the above code into the new file pa.scala, and use the command to execute:
$scala pa.scala Concise is nice
You will see:
Concise is nice
In the previous example, the Scala interpreter infers that the type of arg is String because String is the element type of the array you called foreach. If you prefer something more explicit, you can add the type name, but in that case you should wrap the parameter part in parentheses (in short, this is the common form of syntax):
Args.foreach ((arg: String) = > println (arg))
The result of running this script is the same as the previous one.
If you prefer simplicity to explicit style, you can fully appreciate the advantages of Scala's particular simplicity. If the function text consists of a sentence with one parameter, you don't need to explicitly name and specify parameters. In this way, the following code is equally valid:
Args.foreach (println)
All in all, the syntax of the function text is the list of named arguments in parentheses, the right arrow, and then the function body.
Now, at this point, you may wonder where the for loops that you trust in imperative languages such as Java or C # have gone. In an effort to steer you in the functional direction, there is only one functional approximation of the imperative for (called for expression: expression) in Scala. You don't see all their power and expression at the moment, until you read (or take a glance at) Section 7.3, and we'll just take you through it here. Create a new file, forargs.scala, and enter the following code:
For (arg
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.