+ All Categories
Home > Documents > f Ë ó Ë Ë © f Ë óimages.china-pub.com/ebook3680001-3685000/3684420/ch01.pdf · 2012. 11....

f Ë ó Ë Ë © f Ë óimages.china-pub.com/ebook3680001-3685000/3684420/ch01.pdf · 2012. 11....

Date post: 22-Jan-2021
Category:
Upload: others
View: 2 times
Download: 0 times
Share this document with a friend
13
A1 1.1 Scala 1 1.2 3 1.3 4 1.4 5 1.5 7 1.6 apply 8 1.7 Scaladoc 9 11
Transcript
Page 1: f Ë ó Ë Ë © f Ë óimages.china-pub.com/ebook3680001-3685000/3684420/ch01.pdf · 2012. 11. 1. · val greeting: String = null val greeting: Any = "Hello" Ú I 4DBMB × J + Ú

A1

1.1 Scala 1

1.2 3

1.3 4

1.4 5

1.5 7

1.6 apply 8

1.7 Scaladoc 9 11

Page 2: f Ë ó Ë Ë © f Ë óimages.china-pub.com/ebook3680001-3685000/3684420/ch01.pdf · 2012. 11. 1. · val greeting: String = null val greeting: Any = "Hello" Ú I 4DBMB × J + Ú

Scala Scala

Scala

Scaladoc

Scala

var val

Scaladoc

Scala

Scala

scala/bin PATH

scala Enter

1Chapter

1

Page 3: f Ë ó Ë Ë © f Ë óimages.china-pub.com/ebook3680001-3685000/3684420/ch01.pdf · 2012. 11. 1. · val greeting: String = null val greeting: Any = "Hello" Ú I 4DBMB × J + Ú

2

Enter

8 * 5 + 2 42

scala> 8 * 5 + 2 res0: Int = 42

res0

scala> 0.5 * res0 res1: Double = 21.0

scala> "Hello," + res0 res2: java.lang.String = Hello, 42

Int

Double java.lang.String

res2.to Tab

toCharArray toLowerCase toString toUpperCase

Tab U Tab

res2.toUpperCase

Enter Tab

↑ ↓

← → Del

res2.toLowerCase

Scala

REPL

Page 4: f Ë ó Ë Ë © f Ë óimages.china-pub.com/ebook3680001-3685000/3684420/ch01.pdf · 2012. 11. 1. · val greeting: String = null val greeting: Any = "Hello" Ú I 4DBMB × J + Ú

3

scala

Java Scala

REPL

res0 res1

scala> val answer = 8 * 5 + 2 answer: Int = 42

scala> 0.5 * answer

res3: Double = 21.0

val

scala> answer = 0 <console>:6: error: reassignment to val

var

var counter = 0

counter = 1 // OK var

Scala val Java C++

var

2

Page 5: f Ë ó Ë Ë © f Ë óimages.china-pub.com/ebook3680001-3685000/3684420/ch01.pdf · 2012. 11. 1. · val greeting: String = null val greeting: Any = "Hello" Ú I 4DBMB × J + Ú

4

val greeting: String = null val greeting: Any = "Hello"

val xmax, ymax = 100 // xmax ymax 100

var greeting, message: String = null

// greeting message null

Scala Int Double Java

Scala 7 Byte Char Short Int Long Float Double 1

Boolean Java Scala

1.toString() // "1"

1.to(10) // Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

13 Range

3

Page 6: f Ë ó Ë Ë © f Ë óimages.china-pub.com/ebook3680001-3685000/3684420/ch01.pdf · 2012. 11. 1. · val greeting: String = null val greeting: Any = "Hello" Ú I 4DBMB × J + Ú

5

Scala Scala

Int int[]

1.1 Scala java.lang.String

StringOps

intersect

"Hello".intersect("World") // "lo"

java.lang.String "Hello" StringOps

StringOps intersect

Scala 1.7 StringOps

Scala RichInt RichDouble RichChar

Int Double Char to

RichInt

1.to(10)

Int 1 RichInt to

BigInt BigDecimal

java.math.BigInteger java.math.BigDecimal 1.4

Scala Java C++

4

Page 7: f Ë ó Ë Ë © f Ë óimages.china-pub.com/ebook3680001-3685000/3684420/ch01.pdf · 2012. 11. 1. · val greeting: String = null val greeting: Any = "Hello" Ú I 4DBMB × J + Ú

6

val answer = 8 * 5 + 2

+ − * / % & | ^ >> <<

a + b

a.+(b)

+ Scala

BigInt /%

a b

a. (b)

1.to(10)

1 to 10

Scala

Java Java a + b

a.+(b)

Java C++ Scala Scala ++ −−

+= 1 −= 1

counter += 1 // counter Scala ++

Scala ++

++ Int

Scala

5

Page 8: f Ë ó Ë Ë © f Ë óimages.china-pub.com/ebook3680001-3685000/3684420/ch01.pdf · 2012. 11. 1. · val greeting: String = null val greeting: Any = "Hello" Ú I 4DBMB × J + Ú

7

BigInt BigDecimal

val x: BigInt = 1234567890

x * x * x // 1881676371789154860897069000

Java Java x.multiply(x).multiply(x)

Scala Java Scala min

pow

sqrt(2) // 1.4142135623730951

pow(2, 4) // 16.0

min(3, Pi) // 3.0

scala.math

import scala.math._ // Scala _ Java *

7 import import ._

Scala singleton object6 companion objectJava BigInt BigInt

probablePrime

6

Page 9: f Ë ó Ë Ë © f Ë óimages.china-pub.com/ebook3680001-3685000/3684420/ch01.pdf · 2012. 11. 1. · val greeting: String = null val greeting: Any = "Hello" Ú I 4DBMB × J + Ú

8

BigInt.probablePrime(100, scala.util.Random)

REPL 1039447980491200275486540240713

BigInt.probablePrime Java

Scala StringOps API

distinct ()

"Hello".distinct

5

Scala ss(i) i C++ s[i] Java

s.charAt(i) REPL

"Hello"(4) // 'o'

() applyStringOps

def apply(n: Int): Char

"Hello"(4)

"Hello".apply(4)

BigInt BigIntapply

BigInt("1234567890")

7

Page 10: f Ë ó Ë Ë © f Ë óimages.china-pub.com/ebook3680001-3685000/3684420/ch01.pdf · 2012. 11. 1. · val greeting: String = null val greeting: Any = "Hello" Ú I 4DBMB × J + Ú

9

BigInt.apply("1234567890")

BigInt new

BigInt("1234567890") * BigInt("112358111321")

apply Scala Array(1, 4, 9,

16) Array apply

Java Javadoc Java API Scala Scaladoc

1-1

Javadoc Scaladoc Scala Java

Scala Scaladoc

8

Page 11: f Ë ó Ë Ë © f Ë óimages.china-pub.com/ebook3680001-3685000/3684420/ch01.pdf · 2012. 11. 1. · val greeting: String = null val greeting: Any = "Hello" Ú I 4DBMB × J + Ú

10

www.scala-lang.org/api Scaladoc www.scala-lang.org/

downloads#api

Javadoc Scaladoc

1-2

×

O C C O

Scaladoc

RichInt RichDouble

SpringOps

scala.math

BigInt unary_-

11 -x

implicit BigInt

int long BigInt

21

StringOps count

Char true false

def count(p: (Char) => Boolean) : Int

s.count(_.isUpper) 12

Range Seq[Char]

Scala

9

Page 12: f Ë ó Ë Ë © f Ë óimages.china-pub.com/ebook3680001-3685000/3684420/ch01.pdf · 2012. 11. 1. · val greeting: String = null val greeting: Any = "Hello" Ú I 4DBMB × J + Ú

11

Scala

StringOps

def patch [B >: Char, That](from: Int, patch: GenSeq[B], replaced: Int)

(implicit bf: CanBuildFrom[String, B, That]): That

patch

def patch(from: Int, that: GenSeq[Char], replaced: Int): StringOps[A]

GenSeq[Char] StringOps[A] String

REPL

"Harry".patch(1, "ung", 2) // "Hungry"

1. Scala REPL 3 Tab2. Scala REPL 3 3

res3. res val var4. Scala REPL "crazy" * 3

Scaladoc5. 10 max 2 max6. BigInt 2 10247. probablePrime(100, Random) probablePrime

Radom8. BigInt

"qsnvbevtomcj38o06kul" ScaladocScala

9. Scala10. take drop takeRight dropRight substring

10

11

Page 13: f Ë ó Ë Ë © f Ë óimages.china-pub.com/ebook3680001-3685000/3684420/ch01.pdf · 2012. 11. 1. · val greeting: String = null val greeting: Any = "Hello" Ú I 4DBMB × J + Ú

A1

2.1 14

2.2 16

2.3 16

2.4 17

2.5 18

2.6 for for 19

2.7 21

2.8 L1 22

2.9 L1 22

2.10 23

2.11 L1 24

2.12 25

27 12


Recommended