Scala Reading Notes ch3-ch4
- One of the main characteristics of a functional language is that functions are first class construct, and that's very true in Scala.
- Iterate with foreach and for : args.foreach(arg => println(arg)).
- In this code, you call the foreach method on args, and pass in a function.
- args.foreach((arg : String) => println(arg))
- args.foreach(println)
- for (arg <- arg="" args="" li="" println="">->
- val greetStrings = new Array[String](3)
- greetStrings(0) = "Hello" ...
- for (i <- 0="" 2="" greetstrings="" i="" li="" print="" to="">
->
- val greetStrings : Array[String] = new Array[String](3)
- greetString(0) = "Hello" will be transformed into greetStrings.update(0, "Hello")
- val numNames = Array("zero", "one", "two")
- val oneTwo = List(1, 2); val threeFour = List(3, 4); val oneTwoThreeFour = oneTwo ::: ThreeFour;
- val pair = (99, "Luftballons"); println(pair._1); println(pair._2)
- var jetSet = Set("Boeing", "Airbus"); jetSet += "Lear"; jetSet.contains"Cessna";
- val hashSet = HashSet("Tomatoes", "Chilies")
- val treasureMap = Map[Int, String](); treasureMap += (1 -> "Go to island.");
- (x: Int, y:Int) => x + y : 这是一个匿名函数的定义,可以把它赋值给一个不变量;
- def add(x: Int, y: Int): Int = {x + y} : 这是一个正常函数的定义;
- Remeber : Concise is Nice !
- thrill.mkString(", ") equals ",".join(thrill) in python
No comments:
Post a Comment