scala 函数式编程

变换算子Transform

scala> List(1,2,3,4,5).map(_*2)
res0: List[Int] = List(2, 4, 6, 8, 10)

scala> List(1,2,3,4,5)reduceLeft{*}
res3: Int = 120

  1. 函数Closures (闭包)
    scala> var factor = 3
    factor: Int = 3

scala> val multipilier = (i:Int) => i* factor
multipilier: Int => Int =

scala> multipilier(20)
res4: Int = 60

scala> val v1 = List(1,2,3,4,5) map multipilier
v1: List[Int] = List(3, 6, 9, 12, 15)

scala> factor = 5
factor: Int = 5

scala> val v1 = List(1,2,3,4,5) map multipilier
v1: List[Int] = List(5, 10, 15, 20, 25)

  1. 递归以及尾递归优化

def factorial(i: BigInt): BigInt = {

def fact(i: BigInt, accumulator: BigInt) : BigInt = i match {
    case _ if i== 1 => accumulator
    case _ => fact(i-1, i* accumulator)
}
fact(i, 1)

}

for(i<- 1 to 10) { println(i, factorial(i))}

  1. 函数式数据结构

1〉 列表
scala> val list2= “people” :: “should” :: “learn” :: list1
list2: List[String] = List(people, should, learn, Programming, Scala)

2〉 映射, 集合, 过滤, Fold,归约

依次遍历
List(1,2,3,4,5).foreach { i => println(“Int: " + i) }

val map = Map( “one” -> 1, “two” -> 2, “three” ->3)
map foreach{ kv => println(kv.1 + “:” + kv.2) }

val map2 = map filter(kv => kv._2 >= 2 )
map2: scala.collection.immutable.Map[String,Int] = Map(two -> 2, three -> 3)

scala> List(1,2,3,4,5).reduceLeft(+)
res15: Int = 15

scala> List(1,2,3,4,5).foldLeft(10)(*)
res16: Int = 1200

scala> List(1,2,3,4,5).reduceLeft(*)
res17: Int = 120

  1. 模式匹配

val num0 = Some(5)
val num1 = None

for (option <- List(num0, num1)) {

option.map(n => println(n * 5))

}
25

  1. 偏函数

def concatUpper(s1: String, s2: String): String = (s1 + " " + s2).toUpperCase
val c = concatUpper
println(c(“short”, “pants”))
val c2 = concatUpper(“short”,
: String)
println(c2(“pants”))

val pantsTest: PartialFunction[String, String] = {
case “pants” => “yes, we have pants!”
}

println(pantsTest.isDefinedAt(“pants”))
println(pantsTest.isDefinedAt(“skort”))

  1. 柯里化

def cat(s1: String, s2: String) = s1 + s2

val curryCat = cat _
curryCat: (String, String) => String =

scala> curryCat(“hello”, " world”)
res24: String = hello world

scala> val test4 = cat
:8: error: missing arguments for method cat;
follow this method with `_' if you want to treat it as a partially applied function

   val test4 = cat
               ^

// 是把接受多个参数的函数变换成接受一个单一参数(最初函数的第一个参数)
scala> def multiplier(i: Int)(factor: Int) = i * factor
multiplier: (i: Int)(factor: Int)Int

scala> val byFive = multiplier(5) _
byFive: Int => Int =

scala> val byTen = multiplier(10) _
byTen: Int => Int =

8.隐式类型转换

import scala.runtime.RichString

class FancyString(val str: String)

object FancyString2RichString {
implicit def fancyString2RichString(fs: FancyString) =
new RichString(fs.str)

}

val fs = new FancyString(“scala”)
println(fs.capitalize.reverse)

def multiplier(i: Int)(implicit factor: Int) {

println(i * factor)

}
implicit val factor = 2
multiplier(2)
multiplier(2)(3)

  1. 惰性求值

scala> lazy val num = 10
num: Int =

scala> num
res23: Int = 10

你可能感兴趣的:(scala 函数式编程)