`
sillycat
  • 浏览: 2550727 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

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

分享到:
评论

相关推荐

    Reactive.Programming.with.Scala.and.Akka.1783984341.epub

    Chapter 1. Introducing Reactive Programming Chapter 2. Functional Reactive Programming in Scala Chapter 3. Asynchronous Programming in Scala Chapter 4. Akka for Reactive Programming Chapter 5. ...

    Learning.Scala.Practical.Functional.Programming.for.the.JVM

    Chapter 1. Getting Started With The Scalable Language Chapter 2. Working With Data: Literals, Values, Variables, and Types Chapter 3. Expressions and Conditionals Chapter 4. Functions Chapter 5. First...

    Building.applications.with.Scala.epub

    Chapter 1. Introduction to FP, Reactive, and Scala Chapter 2. Creating Your App Architecture and Bootstrapping with SBT Chapter 3. Developing the UI with Play Framework Chapter 4. Developing Reactive ...

    Object-Orientation, Abstraction, and Data Structures Using Scala, 2nd Edition

    The book is filled with end-of-chapter projects and exercises, and the authors have also posted a number of different supplements on the book website. Video lectures for each chapter in the book are ...

    Fast.Data.Processing.Systems.with.SMACK.Stack

    Chapter 2: The Model - Scala and Akka Chapter 3: The Engine - Apache Spark Chapter 4: The Storage - Apache Cassandra Chapter 5: The Broker - Apache Kafka Chapter 6: The Manager - Apache Mesos Chapter ...

    Gradle.Essentials.1783982365

    Chapter 1. Running Your First Gradle Task Chapter 2. Building Java Projects Chapter 3. Building a Web Application Chapter 4. Demystifying Build Scripts Chapter 5. Multiprojects Build Chapter 6. The ...

    Mastering.Apache.Spark.178397146

    Chapter 3: Apache Spark Streaming Chapter 4: Apache Spark Sql Chapter 5: Apache Spark Graphx Chapter 6: Graph-Based Storage Chapter 7: Extending Spark With H2O Chapter 8: Spark Databricks Chapter 9: ...

    Java - The Well-Grounded Java Developer

    - **Overview**: This chapter delves into dependency injection (DI) frameworks like Guice and Spring, explaining how they can improve the modularity and testability of Java applications. - **Topics ...

    akka官方文档

    akka学习文档,英文、java、scale; 中文版请参考 http://udn.yyuap.com/doc/akka-doc-cn/2.3.6/scala/book/chapter1/introduction.html

    Learning.Node.js.for..NET.Developers.epub

    Chapter 1. Why Node.js? Chapter 2. Getting Started with Node.js Chapter 3. A JavaScript Primer Chapter 4. Introducing Node.js Modules Chapter 5. Creating Dynamic Websites Chapter 6. Testing Node.js ...

    Apache Spark 2 for Beginners [2016]

    This book offers an easy introduction to the Spark framework published on the latest version of Apache Spark 2 Perform efficient data processing, machine learning and graph processing using various ...

    Complete Guide to Open Source Big Data Stack-Apress(2018).pdf

    In the first chapter, I will provide a fuller introduction to the book architecture and chapter contents. I will describe the big data stack structure as well as extended topics such as scaling and ...

Global site tag (gtag.js) - Google Analytics