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
- 浏览: 2542477 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
发表评论
-
NodeJS12 and Zlib
2020-04-01 07:44 468NodeJS12 and Zlib It works as ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 330Traefik 2020(1)Introduction and ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 428Private Registry 2020(1)No auth ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 377Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 468NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 414Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 332Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 243GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 445GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 321GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 307Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 310Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 286Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 303Serverless with NodeJS and Tenc ... -
NodeJS MySQL Library and npmjs
2020-02-07 06:21 281NodeJS MySQL Library and npmjs ... -
Python Library 2019(1)requests and aiohttp
2019-12-18 01:12 257Python Library 2019(1)requests ... -
NodeJS Installation 2019
2019-10-20 02:57 565NodeJS Installation 2019 Insta ... -
Monitor Tool 2019(2)Monit on Multiple Instances and Email Alerts
2019-10-18 10:57 257Monitor Tool 2019(2)Monit on Mu ... -
Sqlite Database 2019(1)Sqlite3 Installation and Docker phpsqliteadmin
2019-09-05 11:24 362Sqlite Database 2019(1)Sqlite3 ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 366Supervisor 2019(2)Ubuntu and Mu ...
相关推荐
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. ...
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...
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 ...
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 ...
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 ...
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 ...
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: ...
- **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学习文档,英文、java、scale; 中文版请参考 http://udn.yyuap.com/doc/akka-doc-cn/2.3.6/scala/book/chapter1/introduction.html
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 ...
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 ...
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 ...