learning scala Option[T]】的更多相关文章

Option(选项)类型用来表示一个值是可选的(有值或无值). Option[T] 是一个类型为 T 的可选值的容器: 如果值存在, Option[T] 就是一个 Some[T] ,如果不存在, Option[T] 就是对象 None val myMap: Map[String, String] = Map("key1" -> "value") val value1: Option[String] = myMap.get("key1") v…
Scala Option Scala Option类型用来表示一个值是可选的(有值或无值) Option[T] 是一个类型为 T 的可选值的容器: 如果值存在, Option[T] 就是一个 Some[T] 如果不存在, Option[T] 就是对象 None Option.orNull 参数:没有(当属性用) 返回:如果Option不为None返回Some的值否则返回null 例一:返回Some的值 scala> val v = Some("Hello Option") v:…
Represents optional values. Instances of Option are either an instance of scala.Some or the object None.Option类代表可选的值.Option的实例要么是Some的实例,要么是None的实例. The most idiomatic way to use an scala.Option instance is to treat it as a collection or monad and u…
转载自: Scala 初学者指南, 这里有一系列很棒的文章 类型 Option 可能你已经见过它在 Map API 中的使用:在实现自己的提取器时,我们也用过它, 然而,它还需要更多的解释. 你可能会想知道它到底解决什么问题,为什么用它来处理缺失值要比其他方法好, 而且可能你还不知道该怎么在你的代码中使用它. 这一章的目的就是消除这些问号,并教授你作为一个新手所应该了解的 Option 知识. Scala 语言中包含一个标准类型 Option[A] 类型, Option[A] 是一个类型为 A…
可以使用scala库,可以从字面上看出是在调用 递归函数: code import scala.util.control.TailCalls._ val arrayDonuts: Array[String] = Array("Vanilla Donut", "Strawberry Donut", "Plain Donut", "Glazed Donut") println("\nStep : How to defin…
package com.aura.scala.day01 import scala.util.Random object extractorObject { def main(args: Array[String]): Unit = { // val customer1ID = CustomerID("Sukyoung") // Sukyoung--23098234908 // customer1ID match { // case CustomerID(name) => pri…
数组:可变的,可索引的,元素具有相同类型的数据集合 一维数组 scala> val intValueArr = new Array[Int](3)intValueArr: Array[Int] = Array(0, 0, 0) scala> val myStrArr = Array("BigData","hadoop","spark")myStrArr: Array[String] = Array(BigData, hadoop, s…
1 .if satement 与其它语言不同的是,scala if statement 返回的是一个值 scala> val a = if ( 6 > 0 ) 1 else -1a: Int = 1 2. while statement 3. do.. while statement 4 for statement for ( 变量 <-  表达式 ) { 语句块}: scala> for ( i <- 1 to 3 ) println(i)123 scala> for…
scala读文件:   example: scala> import scala.io.Sourceimport scala.io.Source scala> var inputFile = Source.fromFile("text.txt")inputFile: scala.io.BufferedSource = non-empty iterator scala> for ( line <- inputFile.getLines()) println(lin…
scala 写文件功能: scala> import java.io.PrintWriterimport java.io.PrintWriter scala> val outputFile = new PrintWriter("text.txt")outputFile: java.io.PrintWriter = java.io.PrintWriter@213c812a scala> outputFile.println("hello panzidong&q…
控制台输出语句: print println example: scala> print("i=");print(i)i=345scala> println("hello ");println("world")hello world…
控制台输入语句: readInt, readDouble, readByte, readShort, readLong, readChar, readBoolean, readLine example:scala> import io.StdIn._import io.StdIn._ scala> var i=readInt()2i: Int = 2 scala> var f=readFloat()2.2f: Float = 2.2 scala> var b=readBoolean…
scala 变量: val : 声明时,必须被初始化,不能再重新赋值. scala> test = "only1"<console>:11: error: not found: value test test = "only1" ^<console>:12: error: not found: value test val $ires0 = test ^    var :可被多次赋值. scala> var test2 = &qu…
scala 操作符: 算术运算符:  +  - *  / % 关系统运算符: > , < ,= ,!= ,>=,<=, 逻辑运算符: && . || , ! 位运算符:&, |, ^,~,<<,>>,>>>(无符号右移) 赋值运算符: = 优先及由上及下…
package com.example import akka.actor._ import akka.util.Timeout object Tutorial_03_Ask_Pattern extends App { val system = ActorSystem("DonutStoreActorySystem") val donutInfoActor = system.actorOf(Props[DonutInfoActor], name="DonutInfoActor…
package com.example import akka.actor.ActorSystem import scala.util.{Failure, Success} import scala.concurrent.ExecutionContext.Implicits.global object Tutorial_01_ActorSystem_Introduction extends App { println("Step1 : Create an actor system")…
object Twice { def apply(x: Int): Int = x * def unapply(z: Int): Option[Int] = == ) Some(z / ) else None } object TwiceTest extends Application { val x = Twice() x match { case Twice(n) => Console.println(n) } // prints 21 } object TwiceTest extends…
The Scala collections library provides specialised implementations for Sets of fewer than 5 values (see the source). The iterators for these implementations return elements in the order in which they were added, rather than the consistent, hash-based…
隐式类可以用来扩展对象的功能非常方便 example: object ImplicitClass_Tutorial extends App { println("Step 1: How to define a case class to represent a Donut object") case class Donut(name: String, price: Double, productCode: Option[Long] = None) println("\nSte…
Partial函数的定义 scala> val isVeryTasty: PartialFunction[String, String] = { case "Glazed Donut" | "Strawberry Donut" => "Very Tasty"}isVeryTasty: PartialFunction[String,String] = <function1> scala> isVeryTasty(&qu…
Scala collection such as List or Sequence or even an Array to variable argument function using the syntax :_ *. code : def printReport(names: String*) { println(s"""Donut Report = ${names.mkString(" - ")}""") } prin…
package com.aura.scala.day01 object genericClasses { def main(args: Array[String]): Unit = { val stack = new Stack[Int] stack.push() stack.push() println(stack.pop()) println(stack.pop()) } } class Stack[A] { private var elements: List[A] = Nil def p…
code: package com.aura.scala.day01 object forComprehensions { def main(args: Array[String]): Unit = { val userBase = List(User(), User(), User(), User()) val twentySomethins = && userYonger.age < ) yield userYonger.name twentySomethins.foreach(…
package com.aura.scala.day01 import scala.util.matching.Regex object regularExpressionPatterns { def main(args: Array[String]): Unit = { // example 1 // .r 方法可便任意字符串变成一个正则表达式 val numberPatter: Regex = "[0-9]".r numberPatter.findFirstMatchIn(&quo…
(1)Scala中创建多行字符串使用Scala的Multiline String. 在Scala中,利用三个双引号包围多行字符串就可以实现. 代码实例如: val foo = """a bc d""" 运行结果为: a bc d (2) 上述方法存在一个缺陷问题,输入的内容,带有空格.\t之类,导致每一行的开始位置不能整洁对齐. 而在实际应用场景下,有时候我们就是确实需要在scala创建多少字符串,但是每一行需要固定对齐. 解决该问题的方法就是应…
package com.aura.scala.day01 object sealedClassed { def findPlaceToSit(piece: Furniture) = piece match { case a: Couch => "Lie on the couch" case b: Chair => "Sit on the chair" } } //sealed定义密封类 sealed abstract class Furniture ca…
code: package com.aura.scala.day01 object patternMatching03 { //当不同类型对象需要调用不同方法时,仅匹配类型的模式非常有用. def goIDLE(device : Device) = device match { case p: Phone => p.screenOff case c:Computer => c.screenSaverOn } } abstract class Device case class Phone(mo…
code package com.aura.scala.day01 object patternMatching02 { def main(args: Array[String]): Unit = { def showNotifacation(notification: Notification):String = { notification match { case Email(sender, title, _) => s"You got an email from $sender w…
code: package com.aura.scala.day01 import scala.util.Random object patternMatching01 { def main(args: Array[String]): Unit = { val x:Int = Random.nextInt() // 一个模式匹配语句包括一个待匹配的值,match关键字,以及至少一个case 语句. x match { => "zero" => "one"…
package com.aura.scala.day01 object caseClasses { def main(args: Array[String]): Unit = { // 注意在实例化案例类时,并没有使用关键字new, 这是因为案例类有一个默认的apply // 方法来负责对象的创建. val frankenstein = Book("978-0486282114") println(frankenstein.isbn) val message1 = Message(&q…