# scala-notes **Repository Path**: aidysun/scala-notes ## Basic Information - **Project Name**: scala-notes - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-11-05 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Scala ## operators val - final/const var - variable ``` val l = List(1, 4, 9) // l is immutable but list is mutable, l(0)=5 can work ``` * for ``` for (i <- 1 to 3) { println(a) } 1 2 3 ``` ## List * creation ``` var a = Array("a", "bcd") // (10) won't work, because it is mutable, // vm would give it any memory since there is no data var l = new List[10]() ``` * map ``` > var list1 = List(1, 2, 3) > list1.map { println("hi"); _*10 } // block expression, println executes once, map only exe _*10 hi ListBuffer(10 ,20. 30) > list1.map {x=>println("hi"); x*10 } // a lambda function with a loop for each item hi hi hi ListBuffer(10 ,20. 30) ``` * reduceLeft, foldLeft ``` list1.reduceLeft(_-_) // (l(0) - l(1)) - l(2)... list2.foldLeft(33)(_+_) // (33 + l(0)) + l(1)... ``` ## Tuple * zip ``` val a1 = Array("a", "b", "c") val a2 = Array(1, 2, 3, 4) val a3 = a1.zip(a2) val defaultx = "Z" val defaulty = -1 val a4 = a1.zipAll(a2, defaultx, defaulty) ``` ## Class / Object ``` class A { // properties val var private private[this] // constructure // default this() class AA(var1: Int) {} // var1 is assigned to member property class AA(var var1: Int) {} // var1 is treated as a new member property // private default class AA private {} // assistent constructure def this(var2: Int) { this() } // access singoten A.log("hello") } ``` * there is no static class/properties in Scala like Java ``` // singleton object A { def log(s:String) { } } ``` * type ``` obj.isInstanceOf[Class] obj.asInstanceOf[Class] classOf[Class] ``` * `sealed` class cannot define subclasses outside of its class file ## trait ``` var obj1 = new Account with ConsoleLog with TimeLog with ShorterLog // multiple traits executes from right to left ``` ## f s raw ``` val name = "Tom" println(s"name is ${name}") // evalue ${expression} println(f"name is ${name} age ${age}%2.0f") val raw2 = raw"a\nb" ``` ## pattern * const variable *MUST* be capitalized ``` val site="www.ggg.com" val CONST_SITE="ooo.com" // const variable MUST be capitalized site match { case CONST_SITE => println("matched") case _ => println("not match") } ``` ## PartialFunction ``` val pf1 = new PartialFunction[Int, String] { override def isDefinedAt(d:Int):Boolean = d != 0 override def apply(d:Int):String = { "Yes" + d } } # orElse # andThen ``` ## File ``` val src = Source.fromFile("f.txt") val lines = src.getLines() for (l <- lines) ... src.close() val out = PrintWriter("fo.txt"); for (i <- 1 to 10) out.println(i) out.close() ``` ## RegEx ``` val pattern = """\s[0-9]\s""" ``` ## Actor * paiallel - multi core * concurrency - multi thread * react is more efficient, reusable threads like thread pool ``` class FirstActor extends Actor { override def act(): Unit = { loop { // while(true) { react { // receive { case "start" => { println("strating.") } case "stop" => exit() case WordCount(f) => { val lines = Source.fromFile(f).getLines().toList val words = lines.flatMap(_.split(" ")) val result = words.map((_,1)) .groupBy(_._1) .mapValues(_.size) sender ! result } } } } } object Test { def main(args: Array[String]): Unit = { val a = new FirstActor a.start() a ! "start" a ! "stop" } } ``` * send messages to actors * ! - async with no return * !? - sync with return * ?? - async with return `Future[Any]`