update scala sprint2
This commit is contained in:
parent
899b3a272b
commit
8a48cfec38
12 changed files with 336 additions and 2 deletions
|
@ -186,7 +186,7 @@ mulBy(3)(4)
|
|||
>类型构造器原理和函数相似,只不过类型构造器处理的是类型,在函数中,我传入一个a,返回一个b,在类型构造器中,我传入一个类型,返回一个类型
|
||||
```scala
|
||||
List[_]
|
||||
List[String]
|
||||
type listStting = List[String]
|
||||
List[Int]
|
||||
Map[_,_]
|
||||
Map[String,Int]
|
||||
|
@ -232,6 +232,13 @@ Pure.println("Rhyme")
|
|||
```scala
|
||||
val a = ("Rhyme", 20)
|
||||
val (first, second) = a
|
||||
|
||||
obj match {
|
||||
if(obj is not null && obj.isA) => (A)obj.id
|
||||
case A(id) => id
|
||||
case B(name) =>
|
||||
case C(sex) =>
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
|
98
src/main/scala/Chapter14.md
Normal file
98
src/main/scala/Chapter14.md
Normal file
|
@ -0,0 +1,98 @@
|
|||
## 14.1更好的Switch
|
||||
|
||||
1.模式匹配不会发生case穿透
|
||||
|
||||
2.模式匹配可以是任意的类型
|
||||
|
||||
3.使用|来分隔多个选项
|
||||
|
||||
```scala
|
||||
var sign = ""
|
||||
var ch: Char = 'A'
|
||||
sign = ch match {
|
||||
case 'A' => "A"
|
||||
case 'B' => "B"
|
||||
case _ => "Others"
|
||||
}
|
||||
```
|
||||
|
||||
## 14.2 守卫
|
||||
|
||||
> 有了守卫,就可以处理一些通用的情况,例如case 1,case 2 ,case 3,...case 10就可以直接用if(ch <10)表示
|
||||
|
||||
```scala
|
||||
ch match {
|
||||
case _ if Character.isDigit(ch) => digit = Character.digit(ch, 10)
|
||||
}
|
||||
```
|
||||
|
||||
## 14.3 模式中的变量
|
||||
|
||||
> 可以在模式匹配中将变量赋值给变量
|
||||
|
||||
```scala
|
||||
var digit = 1
|
||||
ch = '2'
|
||||
ch match {
|
||||
case _ if Character.isDigit(ch) => digit = Character.digit(ch, 10)
|
||||
}
|
||||
digit
|
||||
```
|
||||
|
||||
## 14.4类型模式
|
||||
|
||||
> 代替isIntanceOf操作,更加推荐使用模式匹配来进行类型的判断与转化
|
||||
|
||||
```scala
|
||||
def typeMatch[A](x: A) = {
|
||||
x match {
|
||||
case x: Int => x
|
||||
case s: String => Integer.parseInt(s)
|
||||
case _: BigInt => Int.MaxValue
|
||||
case _ => 0
|
||||
}
|
||||
}
|
||||
|
||||
typeMatch("234")
|
||||
```
|
||||
|
||||
> 匹配类型的时候必须给出变量名,否则将拿对象本身来进行匹配
|
||||
|
||||
> 模式匹配发生在运行期,Java虚拟机中泛型的信息会被擦除,所以以下匹配特定类型的Map是错误的
|
||||
|
||||
```scala
|
||||
obj match {
|
||||
case m: Map[Int, String]=> ...
|
||||
}
|
||||
```
|
||||
|
||||
## 14.5 匹配数组、列表、元组
|
||||
|
||||
数组、列表、元组(析构匹配)
|
||||
|
||||
```scala
|
||||
val a1 = Array(0, 1, 2, 3)
|
||||
val a2 = Array(5, 6, 7, 8)
|
||||
a1 match {
|
||||
case Array(0) => "0"
|
||||
case Array(x, y) => s"$x $y"
|
||||
// case Array(0, _*) => "0..."
|
||||
case Array(0, rest@_*) => rest.max
|
||||
case _ => "something else"
|
||||
}
|
||||
```
|
||||
|
||||
> 模式中使用的变量名都用小写,大写的变量名会被当做常量来看待
|
||||
|
||||
> 如果模式中有不同的分支,除了下划线,别的都不能用
|
||||
|
||||
```scala
|
||||
var t = (1, 2)
|
||||
t match {
|
||||
case (0, _) | (_, 0) => "0 _ _ 0"
|
||||
case (0, x) | (x, 0) => "错误"// 不能这么用
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
|
@ -3,6 +3,6 @@ object Test {
|
|||
println("pppp")
|
||||
|
||||
val list = Seq(1,4,3,6)
|
||||
list.sortWith()
|
||||
// list.sortWith()
|
||||
}
|
||||
}
|
||||
|
|
32
src/main/scala/Tree2.scala
Normal file
32
src/main/scala/Tree2.scala
Normal file
|
@ -0,0 +1,32 @@
|
|||
/**
|
||||
* Created by dripower on 2018/7/18.
|
||||
*/
|
||||
trait Tree2[+A] {
|
||||
def scanLeft[R](init: R)(f: (R, A) => R): R ={
|
||||
this match {
|
||||
case TNil2 => init
|
||||
case Node2(data,left,right) => left.scanLeft(f(init, data))(f);right.scanLeft(f(init, data))(f)
|
||||
case Node2(data,left,_) => left.scanLeft(f(init, data))(f)
|
||||
case Node2(data,_,right) => right.scanLeft(f(init, data))(f)
|
||||
}
|
||||
}
|
||||
// def printDot(): String ={
|
||||
//
|
||||
// }
|
||||
// def reverse: Tree2[A] ={
|
||||
// this match {
|
||||
// case TNil2 => return null
|
||||
// case _ =>
|
||||
// }
|
||||
// }
|
||||
}
|
||||
case class Node2[A](data: A, left: Tree2[A], right: Tree2[A]) extends Tree2[A]
|
||||
case object TNil2 extends Tree2[Nothing]
|
||||
|
||||
object testTree2 {
|
||||
def main(args: Array[String]): Unit = {
|
||||
val Tree2: Node2[Int] = Node2(1,Node2(2,TNil2,TNil2),Node2(3,TNil2,TNil2))
|
||||
val fun = (init: Int,value: Int) => init + value
|
||||
println(Tree2.scanLeft(0)(fun))
|
||||
}
|
||||
}
|
53
src/main/scala/chapter14/Chapter14.sc
Normal file
53
src/main/scala/chapter14/Chapter14.sc
Normal file
|
@ -0,0 +1,53 @@
|
|||
var sign = ""
|
||||
var ch: Char = 'A'
|
||||
sign = ch match {
|
||||
case 'A' => "A"
|
||||
case 'B' => "B"
|
||||
case _ => "Others"
|
||||
}
|
||||
|
||||
//守卫
|
||||
var digit = 1
|
||||
ch = '2'
|
||||
ch match {
|
||||
case _ if Character.isDigit(ch) => digit = Character.digit(ch, 10)
|
||||
}
|
||||
digit
|
||||
|
||||
//类型模式
|
||||
var i = 2
|
||||
i match {
|
||||
case s: Int => s
|
||||
}
|
||||
|
||||
def typeMatch[A](x: A) = {
|
||||
x match {
|
||||
case x: Int => x
|
||||
case s: String => Integer.parseInt(s)
|
||||
case BigInt => -1
|
||||
case _: BigInt => Int.MaxValue
|
||||
case _ => 0
|
||||
}
|
||||
}
|
||||
|
||||
var b: BigInt = BigInt(234)
|
||||
|
||||
typeMatch(b)
|
||||
|
||||
val a1 = Array(0, 1, 2, 3)
|
||||
val a2 = Array(5, 6, 7, 8)
|
||||
a1 match {
|
||||
case Array(0) => "0"
|
||||
case Array(x, y) => s"$x $y"
|
||||
// case Array(0, _*) => "0..."
|
||||
case Array(0, rest@_*) => rest.max
|
||||
case _ => "something else"
|
||||
}
|
||||
|
||||
val list = List(1, 2, 3, 4)
|
||||
|
||||
var t = (1, 2)
|
||||
t match {
|
||||
case (0, _) | (_, 0) => "0 _ _ 0"
|
||||
case (0, x) | (x, 0) => "错误"
|
||||
}
|
68
src/main/scala/chapter2/C2.sc
Normal file
68
src/main/scala/chapter2/C2.sc
Normal file
|
@ -0,0 +1,68 @@
|
|||
import scala.io.StdIn
|
||||
import scala.util.Try
|
||||
|
||||
val name = "Rhyme"
|
||||
val age = 3
|
||||
println(f" Hello $name ,I'm ${age + 3} years old")
|
||||
|
||||
//scala.io.StdIn.readLine("your name")
|
||||
//scala.io.StdIn.readInt()
|
||||
//scala.io.StdIn.readDouble()
|
||||
|
||||
val n = 10
|
||||
for (n <- 0 to 10) {
|
||||
print(n)
|
||||
}
|
||||
|
||||
for (i <- 0 to 10; j <- 0 to i if i != j) {
|
||||
print(i * j)
|
||||
}
|
||||
|
||||
for (i <- 0 to 10; from = 4 - i; j <- from to 3) {
|
||||
print(i * j)
|
||||
}
|
||||
|
||||
for (i <- 0 to 10; j <- "Rhyme") yield i * j
|
||||
|
||||
for {
|
||||
i <- 0 to 10
|
||||
from = 4 - i
|
||||
j <- from to 4
|
||||
} yield i * j
|
||||
|
||||
// 默认参数
|
||||
def decorate(value: String, left: String = "<<", right: String = ">>") = {
|
||||
left + value + right
|
||||
}
|
||||
|
||||
decorate("Rhyme")
|
||||
|
||||
decorate(value = "Rhyme", left = "{", right = "}")
|
||||
|
||||
def sum(args: Int*): Int = {
|
||||
var sum = 0
|
||||
for (arg <- args) {
|
||||
sum = sum + arg
|
||||
}
|
||||
sum
|
||||
}
|
||||
|
||||
sum(1, 2, 3, 4)
|
||||
|
||||
def recursiveSum(args: Int*): Int = {
|
||||
if (args.isEmpty) 0 else {
|
||||
args.head + recursiveSum(args.tail: _*)
|
||||
}
|
||||
}
|
||||
|
||||
recursiveSum(1, 2, 3, 4)
|
||||
|
||||
def process(): Unit = {
|
||||
print("Hello")
|
||||
}
|
||||
|
||||
for (a <- Try {
|
||||
StdIn.readInt()
|
||||
}; b <- Try {
|
||||
StdIn.readInt()
|
||||
}) yield a / b
|
0
src/main/scala/chapter2/sum.md
Normal file
0
src/main/scala/chapter2/sum.md
Normal file
3
src/main/scala/course/Node.scala
Normal file
3
src/main/scala/course/Node.scala
Normal file
|
@ -0,0 +1,3 @@
|
|||
package course
|
||||
|
||||
case class Node[T](ele: T, left: Tree[T], right: Tree[T]) extends Tree[T]
|
3
src/main/scala/course/TNil.scala
Normal file
3
src/main/scala/course/TNil.scala
Normal file
|
@ -0,0 +1,3 @@
|
|||
package course
|
||||
|
||||
case object TNil extends Tree[Nothing]
|
16
src/main/scala/course/TestTree.scala
Normal file
16
src/main/scala/course/TestTree.scala
Normal file
|
@ -0,0 +1,16 @@
|
|||
package course
|
||||
|
||||
object TestTree {
|
||||
def main(args: Array[String]): Unit = {
|
||||
val leftNode = Node(2, TNil, TNil)
|
||||
val rightNode = Node(3, TNil, TNil)
|
||||
val tree = Node(1, leftNode, rightNode)
|
||||
val fun = (init: Long, value: Int) => init + value
|
||||
println(tree.scanLeft(0L)(fun))
|
||||
print("-----printDot-----")
|
||||
println(tree.printDot())
|
||||
print("-----reverse-----")
|
||||
val reverse = tree.reverse()
|
||||
println(reverse)
|
||||
}
|
||||
}
|
40
src/main/scala/course/Tree.scala
Normal file
40
src/main/scala/course/Tree.scala
Normal file
|
@ -0,0 +1,40 @@
|
|||
package course
|
||||
|
||||
import java.io.PrintWriter
|
||||
|
||||
trait Tree[+T] {
|
||||
def scanLeft[R](init: R)(f: (R, T) => R): R = {
|
||||
this match {
|
||||
// 空树的情况
|
||||
case TNil => init
|
||||
// 树非空
|
||||
case Node(value, left, right) =>
|
||||
val leftRes = left.scanLeft(f(init, value))(f)
|
||||
right.scanLeft(leftRes)(f)
|
||||
}
|
||||
}
|
||||
|
||||
def printDot(): String = {
|
||||
val out = new PrintWriter("tree.dot")
|
||||
this match {
|
||||
case TNil => out.write("TNil\n"); "TNil\n"
|
||||
case Node(value, left, right) =>
|
||||
val leftRes = left.printDot()
|
||||
val rightRes = right.printDot()
|
||||
out.write(value.toString + "\n" + leftRes.toString + rightRes.toString)
|
||||
out.flush()
|
||||
out.close()
|
||||
value.toString + "\n" + leftRes.toString + rightRes.toString
|
||||
}
|
||||
}
|
||||
|
||||
def reverse(): Tree[T] = {
|
||||
this match {
|
||||
case TNil => TNil
|
||||
case Node(value, left, right) =>
|
||||
left.reverse()
|
||||
right.reverse()
|
||||
Node(value, right, left)
|
||||
}
|
||||
}
|
||||
}
|
14
src/test/scala/com/rhyme/TreeTest.scala
Normal file
14
src/test/scala/com/rhyme/TreeTest.scala
Normal file
|
@ -0,0 +1,14 @@
|
|||
package com.rhyme
|
||||
|
||||
import org.scalatest.{FlatSpec, Matchers}
|
||||
import course.Node
|
||||
import course.TNil
|
||||
|
||||
class TreeTest extends FlatSpec with Matchers {
|
||||
"a course.Tree" should "be same after reverse and reverse" in {
|
||||
val leftNode = Node(2, TNil, TNil)
|
||||
val rightNode = Node(3, TNil, TNil)
|
||||
val tree = Node(1, leftNode, rightNode)
|
||||
tree.reverse().reverse() should be(tree)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue