当前位置:网站首页>Scala for derivation: the ability to define a value in the first part of a for expression and use it in subsequent (outer) expressions
Scala for derivation: the ability to define a value in the first part of a for expression and use it in subsequent (outer) expressions
2022-06-22 16:47:00 【ZH519080】
stay scala Of for In circulation , How to... When containers are not applicable , stay for loop ( outside ) Call it later for Properties defined in . Object containing None Attribute in for How to eliminate ???
Both cases are written in the same function :
/**for deduction : In the for The first part of the expression defines the value , Use this value in the following expression
* Such as upcaseBreed stay println Use in */
def roundingFor: Unit ={
val dogBreeds = List("Doberman","Dachshund","Scottish","Portuguese")
for {
breed <- dogBreeds
/** Even though upcaseBreed The value of is immutable ,upcaseBreed There is no need to set it in the front , But you don't need to use val Keywords are qualified */
upcaseBreed = breed.toUpperCase
} println(upcaseBreed) // Equivalent to the for Use after the loop for Internal properties
println(
""" *
| ***
| *****
|*********
| *****
| ***
| *
""".stripMargin)
val catBreeds = List(Some("Dane"),None,Some("Yorkshire"),None,Some("Scottish"))
println("first pass:")
for {
/** Each extracted element is Option object . The following code will use the arrow character to extract Option The value in , The derivation is effectively checked and ignores None, There will be no exceptions */
breedOption <- catBreeds
breed <- breedOption
upcaseBreed = breed.toUpperCase
} println(upcaseBreed)
println("second pass:")
/** At the first for Defined inside the loop breed and upcaseBreed The attribute is in the second... Below for Use in loop */
for {
/** this for The derivation uses pattern matching , Only when breedOption yes Some Type , expression Some(breed) <- catBreeds Will be executed and extracted breed*/
Some(breed) <- catBreeds
upcaseBreed = breed.toUpperCase
} println(upcaseBreed)
}
边栏推荐
猜你喜欢
![[C language] use of library function qsort](/img/b0/6e86e31243164479b0f3d960d039ef.png)
[C language] use of library function qsort

STM32 ADC acquisition via DMA (HAL Library)

机器学习笔记 - HaGRID—手势识别图像数据集简介

jMeter使用案例

大话局部性原理

jsp学习之(一)---------jsp概述
![Web technology sharing | [Gaode map] to realize customized track playback](/img/0b/25fc8967f5cc2cea626e0b3f2b7594.png)
Web technology sharing | [Gaode map] to realize customized track playback

CUMT学习日记——数字图像处理考试速成笔记
![[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]

CUMT study diary - quick notes of digital image processing examination
随机推荐
STM32通过DMA进行ADC采集(HAL库)
Short video source code development, high-quality short video source code need to do what?
高可用性的ResourceManager
linux系统维护篇:mysql8.0.13源码下载及安装之“傻瓜式”操作步骤(linux-centos6.8)亲测可用系列
Summary of spark common operators
VHEDT业务发展框架
Linux system maintenance: mysql8.0.13 source code download and installation "fool" operation steps (Linux centos6.8) test available series
spark常用 算子小总结
双向数据绑定v-model与v-decorator
for.. of vs. for.. In statement
jsp學習之(二)---------jsp脚本元素和指令
Interface idempotent design
长安链使用技巧总结
为什么要买增额终身寿险?增额终身寿险安全可靠吗?
5 modes of IO model
JS method for judging data type of interview questions
Machine learning notes - Hagrid - Introduction to gesture recognition image data set
Parts beyond the text are indicated by ellipsis
NiO service multithreaded version
ALV report in SAP tutorial - ABAP list viewer -012