当前位置:网站首页>Basic application of scala for
Basic application of scala for
2022-06-22 16:47:00 【ZH519080】
scala-for Application :
Go straight to the code .
def testfor01: Unit ={
val nieces = List("emily", "hananh", "mercedes", "porsche")
/*
keyword yield:for In the loop yield The current element will be recorded ,
Save to collection , After the loop ends, the set is returned .
scala in for A loop has a return value , If what is being cycled is Map, Then it returns Map,
If what is being cycled is List, Is returned List, And so on
scala There are many ways to traverse a collection :for loop 、foreach loop 、while loop 、map mapping 、flatmap etc.
Use to or until Conduct for loop ,to And until difference :
to: Including upper and lower limits , It's a closed interval
until: Excluding upper and lower limits , It's open range
*/
for (n <- nieces) yield println(n.capitalize)
/*
for (n <- nieces) such for In the derivation , Failed matches are quietly ignored
*/
nieces.foreach(nie => println(nieces.mkString))
//2、for A loop can be used as a timer
println("------for as a timer------")
val a = Array("apple", "banana", "orange")
a.foreach(println)
for (i <- 0 until a.length if i != 1) println(s"$i is ${a(i)}")
//3、 Defensive sentences (for In circulation if Judge ), here to And until Same function
// Guard statements are usually used as filter sets , According to the specific requirements , You can use... In the set filter、take、drop And so on for loop
println("------ stay for There are in the loop if Statement nesting ------")
for (x <- 0 to 5 if x != 3) println(x)
// stay for Nested in a loop if sentence , Multiple can be nested at the same time if sentence
for (wei_x <- 0 to 5 if wei_x % 2 == 0 if wei_x == 2) println(wei_x)
System.out.println("------ Traverse Map aggregate ------")
// Traverse Map aggregate
val names = Map("fname" -> "Robert", "lname" -> "Goren")
for ((k, v) <- names) println(s"key: $k, value: $v")
}
def testfor02: Unit ={
val nieces = List("emily", "hananh", "mercedes", "porsche")
//for deduction : adopt for-yield Application for A loop has a return value .
val str01: List[String] = for (n <- nieces if !n.contains("emily")) yield n+" ... dd"
str01.foreach(println)
//for deduction : stay yield When there are multiple lines of code in , Use in yield Using code blocks
val str02 = for (n <- nieces if ! n.contains("emily")) yield {
val str = n+" ... dd"
str
}
str02.foreach(println)
}scala-for Attention during application for Is the inner loop an open interval or a closed interval , If you use for-yield The guards , it is to be noted that yield The return object of .
边栏推荐
- Parts beyond the text are indicated by ellipsis
- Test for API
- Task scheduling design of collection system
- 交互电子白板有哪些特点?电子白板功能介绍
- 5 modes of IO model
- [MYSQL]数据同步提示:Specified key was too long;max key length is 767 bytes
- 大话局部性原理
- Spark's NaiveBayes Chinese text classification
- 毕业季·本科毕业感想——机械er的自救之路
- Summary of Changan chain usage skills
猜你喜欢
![[pop up box 2 at the bottom of wechat applet package]](/img/31/266e6a1f4200347c9324ea37b78562.png)
[pop up box 2 at the bottom of wechat applet package]

对ABAP程序调优的学习(四)LOOP WHERE KEY

洞见科技牵头的全球「首个」IEEE隐私计算「互联互通」国际标准正式启动

jsp学习之(二)---------jsp脚本元素和指令
![Prometheus监控之Consul监控 [consul-exporter]](/img/9e/8547b2c38143ab0e051c1cf0b04986.png)
Prometheus监控之Consul监控 [consul-exporter]

【C语言】库函数qsort的使用
![[C language] use of library function qsort](/img/b0/6e86e31243164479b0f3d960d039ef.png)
[C language] use of library function qsort

【微信小程序封装底部弹出框】一

ABAP query tutorial in sap: sq01, sq02, sq03-017

What is restful and what rules should be followed when designing rest APIs?
随机推荐
Interface (optimization type annotation)
oracle分库分表
Short video source code development, high-quality short video source code need to do what?
Call CMD process communication
jsp学习之(一)---------jsp概述
Test for API
数据库mysql 主从方案
变量
【微信小程序获取自定义tabbar的高度】绝对可用!!!
ERROR 1364 (HY000): Field ssl_cipher doesnt have a default value
spark关于数据倾斜问题
接口幂等性设计
Smart forms-014 in SAP ABAP
JS method for judging data type of interview questions
购买指南丨如何购买一台高质量会议平板,这几个方面一定要对比
How to add a "security lock" to the mobile office of government and enterprises?
迭代器与生成器
[C language] deeply analyze the storage of integer and floating-point types in memory
JSP learning (2) -- JSP script elements and instructions
SAP ABAP dialog programming tutorial: module pool in -09