scala(14)CN Book - Chapter 1, 2, 3

scala(14)CN Book - Chapter 1, 2, 3

1. Some Tips about the Language Itself
1.1 Map
scala> var capital = Map("1"->"one","2"->"two")
capital: scala.collection.immutable.Map[String,String] = Map(1 -> one, 2 -> two)

scala> println(capital)
Map(1 -> one, 2 -> two)

scala> capital +=("3"->"three")

scala> println(capital("3"))
three

1.2 New Type
BigInt extends java.math.BigInteger

1.3 New Control Type
recipient ! msg

receive {
     case Msg1 => …
     case Msg2 => ...
}

1.4 Compare to Java
class MyClass{
     private int index:
     private String name:
     …getter and setter and constructor….
}

class MyClass(index: Int, name: String)

Check the functions on API http://www.scala-lang.org/api/current/index.html#scala.Char 
scala> val aString = "abCde"
aString: String = abCde

scala> val upperFound = aString.exists(_.isUpperCase)
<console>:8: error: value isUpperCase is not a member of Char
       val upperFound = aString.exists(_.isUpperCase)
                                         ^
scala> val upperFound = aString.exists(_.isUpper)
upperFound: Boolean = true

2. First step of Scala
2.1 Types
scala.Int ---> Java int
scala.Boolean ---> Java boolean

All the private types, we have the same thing in scala.
scala> 1+2
res0: Int = 3

scala> res0 * 3
res1: Int = 9

scala> res1 + 1
res2: Int = 10

Type Inference
val msg1: String = "Hello some String."
val msg2 = "Hello some String"

The character |
scala> val multiple =
     | "This is multple lines"
multiple: String = This is multple lines

Define Function
Sometimes, we can ignore the return type of the function
scala> def max2(x: Int, y: Int) = if(x>y) x else y

2. Chapter 3 Next Step of Scala
List  list1:::list2     1::list2

Nil is the empty element.
val list5 = 1::Nil

Some functions on List
count with some condition
scala> list1
res8: List[String] = List(will, fill, until)

scala> list1.count(s => s.length == 4)
res9: Int = 2


drop and dropRight will drop the number of elements
scala> list1.drop(2)
res12: List[String] = List(until)

scala> list1.dropRight(2)
res13: List[String] = List(will)

exists will check the existence
scala> list1.exists(s => s == "until")
res14: Boolean = true

filter will filter the element with 4 characters
scala> list1.filter(s => s.length == 4)
res15: List[String] = List(will, fill)

forall check all the elements in the List
scala> list1.forall(s=> s.endsWith("1"))
res16: Boolean = false

foreach, just the operation on every element
scala> list1.foreach(s=>print(s))
willfilluntil

head return the first element of the List
scala> list1.head
res18: String = will

init will return all the elements beside the last one
scala> list1.init
res21: List[String] = List(will, fill)

isEmpty
scala> list1.isEmpty
res23: Boolean = false

last is the last element
scala> list1.last
res24: String = until

scala> list1.length
res25: Int = 3

map is operation on every element, generate a new List
scala> list1.map(s=>s+"y")
res26: List[String] = List(willy, filly, untily)

mkString generate a new String
scala> list1.mkString(", ")
res27: String = will, fill, until

remove is not working after the version 2.9, we need to use filterNot instead
scala> list1.filterNot(s=>s.length==4)
res29: List[String] = List(until)

scala> list1.filter(s=>s.length==4)
res30: List[String] = List(will, fill)

reverse return the same List in another order, just reverse it
scala> list1.reverse
res31: List[String] = List(until, fill, will)

sort was deprecated in 2.8 and cut out in later version. We need to use sortWith instead

scala> list1.sortWith((s,t) =>
     | s.charAt(0).toLower <
     | t.charAt(0).toLower)
res35: List[String] = List(fill, until, will)

3. Use Tuple
The thing different between List and Tuple, tuple can hold different type of data, but List[Int], List[String]

In Java world, we usually use Java Bean to hold all the result values, but here, we can easily use tuple without defining a java bean.

scala> val t2 = ('r',"the",1,4)
t2: (Char, String, Int, Int) = (r,the,1,4)

Actually, tuple's constructor is the all the types. t2: (Char, String, Int, Int)

4. Use Set and Map
…snip…

5. Functional Programming
Keep not using var.
def printArgs(args: Array[String]): Unit = {
     args.foreach(println)
}

6. Deal with Files
scala.io.Source



References:
scala(13)
http://sillycat.iteye.com/blog/1849918

Scala编程.pdf

你可能感兴趣的:(scala)