Scala总结(三)

1、偏应用函数

2、闭包

3、高阶函数:函数作为参数传递给另外一个函数,就是高阶函数。

4、SAM

5、Curring柯里化:柯里化是指把原来接受两个函数的参数变成新的接受一个参数的函数的过程。新的函数返回一个以原有的第二个参数为参数的函数。

6、模式匹配

val data = 2
data match {
  case 1 => println("First")
  case 2 => println("Second")
  case _ => println("Not Known Number")
}

val result = data match {
  case i if i == 1 => "The First"
  case number if number == 2 => "The Second" + number
  case _ => "Not Known Number"
}
println(result)

"Scala".foreach(c => println(
  c match {
    case ' ' => "space"
    case ch => "Char: " + ch
  }
))

7、类型模式匹配、Tupe、Array、List模式匹配

def match_type(t : Any) = t match {
  case v : Int => println("Int")
  case v : String => println("String")
  case v : Map[_ , _] => v.foreach(println)
  case _ => println("Unknown type !!")
}

def match_array(arr : Any) = arr match {
  case Array(1) => println("arr:1")
  case Array(x, y) => println("arr:" + x + " " + y)
  case Array(1, _*) => println("arr:" + "1 ...")
  case _ => println("others")
}

def match_list(ls : Any) = ls match {
  case 1 :: Nil => println("list:" + 1)
  case x :: y :: Nil => println("list:" + x + " " + y)
  case 0 :: tail => println("list:" + "0...")
  case _ => println("something else")
}

def match_tuple(tp : Any) = tp match {
  case (5, _) => println("tuple:" + "5")
  case (x, 0) => println("tuple:x" + x)
  case _ => println("something else")
}

8、apply是传进参数构建对象和unapply是从已经创建的对象中提取出内容。

9、case class和case object

def match_Person(person : Person) = person match {
    case Student(age) => println("Person:" + age)
    case Worker(_ , salary) => println("salary: " + salary)
    case Shared => println("No property")
  }

}

abstract class Person
case class Student(age : Int) extends Person
case class Worker(age : Int, salary : Double) extends Person
case object Shared extends Person

10、函数的参数不是函数就是一阶函数,函数的参数是一个函数的话就称作高阶函数。

11、List一阶函数操作。

val ls1 = List("hello", "world", "nice")
println(ls1.last)
println(ls1.init)
println(ls1.reverse)
println(ls1 take 2)
println(ls1 drop 1)
println(ls1 splitAt 2)
println(ls1 apply(2))

12、zip和unzip操作。zip是拉链操作,把两个List转化为一一对应的关系。unzip是拉链操作的逆操作。

val zipped = "abcde".toList zip List(1, 2, 3, 4, 5)
println(zipped)
println(zipped.unzip)

13、map、flatMap、foreach、filter。map:对每个元素进行处理,返回list。flatMap:对每个元素进行处理,并且压到一个List中。foreach:对每个元素进行处理,返回Unit。filter:对每个元素进行处理,筛选出符合条件的元素,返回List。

println(ls1.map(_ + 1))
println(lv map(_.length))
println(lv map(_.toList.reverse.mkString))
println(lv map(_.toList))
println(lv flatMap(_.toList))
println(List.range(1, 10).flatMap(i => List.range(1, i) map(j => (i, j))))

var sum = 0
List(1, 2, 3, 4, 5) foreach(sum += _)
println("sum:" + sum)

println(List(1, 2, 3, 4, 5, 6).filter(_ % 2 == 0))

14、Option、Some、None的相互关系。

15、partition、find、takeWhile、dropWhile、span

val ls2 = List(1, 2, 3, 4, 5, 6)
println(ls2.partition(_ % 2 == 0))
println(ls2.filter(_ % 2 == 0))
println(ls2.find(_ % 2 == 0))
println(ls2.find(_ <= 4))
println(ls2 takeWhile(_ < 4))
println(ls2 dropWhile(_ < 4))
println(ls2 span(_ < 4))
val mx = List(List(1, 3, 4), List(0, 0 ,0), List(2, 1, 3))
println(matrixOps(mx))

16、foldLeft、foldRight、Sort

17、List伴生对象object List的一些常用操作。

println(List.apply(1, 2, 3))
println(List.range(1 ,3))
println(List.range(9, 0, -2))
println(List(List('h', 'e'), List('l', 'l'), List('0')).flatten)
println(List.concat(List('h', 'e'), List('l', 'l'), List('0')))

18、ListBuffer、ArrayBuffer、Queue、Stack

def stackOps(): Unit = {
  val stack01 = new mutable.Stack[Int]
  stack01.push(1)
  stack01 push(2)
  println(stack01.top)
  println(stack01)
  println(stack01.pop)
  println(stack01)
}

def queueOps(): Unit = {
  val queue0 = Queue[Int]()
  val queue1 = queue0.enqueue(1)
  val queue2 = queue0.enqueue(List(2, 3, 4, 5))
  println(queue2)
  val (element, left) = queue2.dequeue
  println(element + ":" + left)
}

def listBufferOps(): Unit = {
  val listBuffer = new ListBuffer[Int]()
  listBuffer ++= List(2, 3)
  println(listBuffer)
}

def arrbufOps(): Unit = {
  val arrBuffer = new ArrayBuffer[Int]()
  arrBuffer ++= Array(1, 2)
  arrBuffer.foreach(println)
  println(arrBuffer)
}

19、Set、Map、TreeSet、TreeMap

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值