【Spark学习笔记】Scala语法

学习列表:

  1. scala官方文档
  2. scala cheat
  3. twitter Scala 课堂
  4. 有趣的 Scala 语言: 使用递归的方式去思考
  5. 《scala 编程》
  • 几乎一切乎都是表达式
scala> 1 + 1
res0: Int = 2

  • 复合表达式——{}

花括号用于创建复合表达式,复合表达式的返回值是最后一个表达式

scala> {
     | println("hello world")
     | "hi"
     | }
hello world
res0: String = hi

  • 常量

使用val,不能重复给常量赋值

scala> val value = 100 + 1
value: Int = 101
scala> value = 200// 给常量重复赋值将报错
:11: error: reassignment to val
       value = 200
             ^

  • 变量

使用var,可以修改变量的值

scala> var value = 100
value: Int = 100
scala> value = 200
value: Int = 200

  • 类型推断

scala支持类型推断,类型声明置后,编译器会推断其类型,会比java更加简洁

scala> val m: Int = 100// 不建议
m: Int = 100
scala> var n: String = "hi"// 不建议
n: String = hi
scala> val m = 100// 推荐
m: Int = 100
scala> var n = "hi"// 推荐
n: String = hi

  • 函数

使用def创建函数(与python一样),需要为函数参数指定类型签名,函数返回值类型可以不写(递归函数必须声明返回值类型),让编译器进行类型推断

scala> def sum(a: Int, b: Int): Int = a + b// 中规中矩的写法
sum: (a: Int, b: Int)Int
scala> sum(2, 3)
res8: Int = 5
scala> def fun(x: Int) = x*x// 不声明返回值类型
fun: (x: Int)Int
scala> fun(5)
res9: Int = 25
// 使用复合表达式
scala> def mul(x: Int, y: Int) = {
     | println(x.toString)
     | println(y.toString)
     | x * y
     | }
mul: (x: Int, y: Int)Int
scala> mul(2, 3)
2
3
res12: Int = 6
// 无返回值的函数可以不用=,代表返回值为Unit类似java中void
scala> def hi() {
     | println("hi world!")
     | }
hi: ()Unit
scala> hi// 无参函数可以不使用括号调用
hi world!

  • 匿名函数

使用 =>

scala> (x: Int) => x*x
res14: Int => Int = 
scala> res14(5)
res15: Int = 25

可以将匿名函数进行赋值

scala> val fun = (x: Int) => x*x
fun: Int => Int = 
scala> fun(3)
res27: Int = 9

同样可以使用{}定义复杂的匿名函数

scala> var m = {x: Int =>
     | println("hi")
     | val temp = x*x
     | temp + 1
     | }
m: Int => Int = 
scala> m(4)
hi
res28: Int = 17

  • 部分应用(partially applied function)

你可以使用下划线“_”(通配符)部分应用一个函数,结果将得到另一个函数。

scala> def add(x: Int, y: Int) = x+y
add: (x: Int, y: Int)Int
scala> add(4, _:Int)
res1: Int => Int = 
scala> res1(3)
res2: Int = 7

  • 柯里化函数(curried)

有时会有这样的需求:允许别人一会在你的函数上应用一些参数,然后又应用另外的一些参数。

scala> def mul(x: Int)(y:Int) = x*y
mul: (x: Int)(y: Int)Int
scala> mul(2)_
res3: Int => Int = 
scala> res3(5)
res4: Int = 10

多参函数柯里化

scala> def mul3(x: Int, y: Int, z: Int) = x*y*z
mul3: (x: Int, y: Int, z: Int)Int
scala> (mul3 _).curried
res8: Int => (Int => (Int => Int)) = 
scala> res8(2)
res9: Int => (Int => Int) = 
scala> res9(3)
res10: Int => Int = 
scala> res10(4)
res11: Int = 24

  • 变长参数

向方法传入任意多个同类型的参数,比如任意多个整数求和

scala> def sum(nums: Int*) = (nums reduceLeft ((x: Int, y: Int) => x+y))
sum: (nums: Int*)Int
scala> sum(1,2,3,4,5,6,7,8,9,10)
res19: Int = 55

  • 参数限定

scala可以使用require进行函数参数限制,类似java assert

scala> def sq(x: Int) = {
     | require(x >= 0, "input must > 0!")
     | math.sqrt(x)
     | }
sq: (x: Int)Double
scala> sq(2)
res0: Double = 1.4142135623730951
scala> sq(-1)
java.lang.IllegalArgumentException: requirement failed: input must > 0!
  at scala.Predef$.require(Predef.scala:219)
  at .sq(:11)
  ... 33 elided

  • 隐式类型转换(implicit)

在同一作用域下,自动进行类型的转换,与该函数名无关,仅与其输入输出类型有关

scala> implicit def fun(x: String) = x.toInt
fun: (x: String)Int
scala> math.max("132", 13)
res0: Int = 132

  • 闭包(closure)

函数及其执行所需的上下文环境(“An object is data with functions. A closure is a function with data.” — John D. Cook)

scala> def fun() = {
     | var i = 0
     | val lam = () => {i+=1;i}
     | lam
     | }
fun: ()() => Int
scala> val clo = fun()
clo: () => Int = 
scala> clo()
res0: Int = 1
scala> clo()
res1: Int = 2
scala> clo()
res2: Int = 3

  • 传名参数(call by name,传值、传引用之外的另外一种参数传递方式)

传名的参数传递使用替换规则。

scala> def callByNameFun(x: => Int) = List(x, x)// =>开头为传名参数
callByNameFun: (x: => Int)List[Int]
// 定义高阶函数createNum产生一个函数,使用闭包,每调用一次fun(),i自增加1
scala> def createNum() = {
     | var i = 0
     | val fun = {
     | () =>
     | i += 1
     | i
     | }
     | fun
     | }
createNum: ()() => Int
scala> val f = createNum()
f: () => Int = 
scala> callByNameFun(f())
res0: List[Int] = List(1, 2)
scala> callByNameFun(f())
res1: List[Int] = List(3, 4)

  • 尾递归

尾递归函数返回值为本身或者结果,同一般的递归相比,可被直接转化为循环,栈的开销为O(1),scala中可以用 @tailrec 检测定义的函数是否为尾递归

scala> import scala.annotation.tailrec
scala> @tailrec def fun(re: Int, index: Int): Int = {
     | if (index == 0) {
     | re
     | } else {
     | fun(re+index, index - 1)
     | }
     | }
fun: (re: Int, index: Int)Int
scala> fun(0, 100)
res0: Int = 5050
scala> fun(0, 100)
res1: Int = 5050

使用class关键字

scala> class User {
     | var name = "bernie"
     | var age = 23
     | def show() = println(name + ":" + age.toString)
     | }
defined class User
scala> var user = new User()
user: User = User@41112c13

构造函数,构造函数不是特殊的方法,他们是除了类的方法定义之外的代码。

scala> class Person(pName: String, pAge: Int) {
     | var name = pName
     | var age = pAge
     | def show() = println(name + ":" + age.toString)
     | }
defined class Person
// 也可重载构造函数
scala> class Person(pName: String, pAge: Int) {
     | def this(pName: String) = this(pName, 18)
     | var name = pName
     | var age = pAge
     | }
defined class Person

  • 抽象类

和java差不多,继承也使用extends

scala> abstract class AbstractFather {
     | def fun(x: Int): Int
     | }
defined class AbstractFather

  • 特质(trait)

是一组属性与行为的组合,特质可以多扩展,在抽象类与特质之间优先选择特质

scala> trait Fly {
     | val wing: Int
     | def fly() = println("I can fly")
     | }
defined trait Fly
scala> trait Run {
     | val leg: Int
     | def run() = println("I can run")
     | }
defined trait Run
scala> class Bird extends Fly with Run {
     | val wing = 2
     | val leg = 2
     | def power() = {
     | fly()
     | run()
     | }
     | }
defined class Bird
scala> new Bird().power()
I can fly
I can run

  • 单例的语言级别实现——object

Util类就非常适合使用object定义

object Util {
    def echo() = println("Hello World!")
}

  • 伴生对象与伴生类

名字相同的class与object,放在同一个文件下定义,能够访问彼此的私有成员

  • 泛型

使用中括号,类和方法都可以是泛型的

scala> class Gen[T] {
     | def show(x: T) = x.toString + "$"
     | }
defined class Gen
scala> val gen = new Gen[Int]
gen: Gen[Int] = Gen@3a44ebcb
scala> gen.show(100)
res11: String = 100$
scala> def genFun[T](x: T) = x.toString + "&"
genFun: [T](x: T)String
scala> genFun(100)
res13: String = 100&

  • 语法糖 apply update

为了方便你使用类,scala提供了apply 与 update语法糖

scala> object Arr {
     | val arr = Array(0,1,2,3,4,5)
     | def apply(i: Int) = arr(i)
     | def update(i: Int, value: Int) = {arr(i) = value}
     | }
defined object Arr
scala> Arr(1)
res38: Int = 1
scala> Arr(1) = 100
scala> Arr.arr
res40: Array[Int] = Array(0, 100, 2, 3, 4, 5)

其实 Array 取值与更新就是实现了这两个方法

image
  • 函数与对象de边界

函数可以看作一堆特性的集合(Funtion0 -> Funtion22),apply语法糖有助于统一对象和函数式编程的二重性,你可以传递类,并把它们当做函数使用,而函数本质上是类的实例

scala> object Fun extends Function1[Int, Int] {
     | def apply(x: Int) = x*x
     | }
defined object Fun
scala> Fun(3)
res50: Int = 9

  • 模式匹配

感觉像java中switch case,但功能更加强大

scala> val m = 1
m: Int = 1
scala> m match {
     | case 1 => 100
     | case 2 => 200
     | case _ => 300
     | }
res25: Int = 100

  • 异常

异常也是表达式

scala> val re = try {
     | 1/0
     | "oh yeah"
     | } catch {
     | case e: Exception => println("error happen")
     | "oh no"
     | } finally {
     | println("turn me")
     | }
error happen
turn me
re: String = oh no

  • 集合
image

List(列表)、Set(集)、Tuple(元组)、Map(映射)、Option(选项)

Tuple

元组是在不使用类的前提下,将元素组合起来形成简单的逻辑集合,读取方式使用下标,从1开始,可以使用->创建2元组

scala> val tuple = ("hi", "he", "ha", 12, 'a')
tuple: (String, String, String, Int, Char) = (hi,he,ha,12,a)
scala> tuple._1
res36: String = hi
scala> 1 -> 2
res63: (Int, Int) = (1,2)

Map

scala> Map(1 -> "one", 2 -> "two")
res0: scala.collection.immutable.Map[Int,String] = Map(1 -> one, 2 -> two)
scala> Map((1, "one"), (2, "two"))
res1: scala.collection.immutable.Map[Int,String] = Map(1 -> one, 2 -> two)

Option

表示可能包含值的容器,有Some与None两个子类,特征接口如下

trait Option[T] {
  def isDefined: Boolean
  def get: T
  def getOrElse(t: T): T
}

scala> val map = Map(1 -> "one", 2 -> "two")
map: scala.collection.immutable.Map[Int,String] = Map(1 -> one, 2 -> two)
scala> map.get(1)
res2: Option[String] = Some(one)
scala> map.get(3)
res3: Option[String] = None
scala> res2.get
res4: String = one
scala> res3.get
java.util.NoSuchElementException: None.get
  at scala.None$.get(Option.scala:347)
  at scala.None$.get(Option.scala:345)
  ... 33 elided
scala> res3.isDefined
res6: Boolean = false
scala> res3.getOrElse("three")
res7: String = three

  • 集合操作符(操作符即函数)

map、foreach

map将产生一个新的集合,foreach无返回值,一般仅作遍历使用,取它的副作用

scala> (1 to 10) map (_ * 2)
res21: scala.collection.immutable.IndexedSeq[Int] = Vector(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)
scala> (1 to 10) foreach (println(_))
1
2
3
4
5
6
7
8
9
10

reduceLeft、reduceRight(将集合进行函数计算,得到结果)

scala> (1 to 10) reduceLeft ((m: Int, n: Int) => m+n)
res42: Int = 55
scala> (1 to 10) reduceRight ((m: Int, n: Int) => m+n)
res43: Int = 55

filter(白名单过滤)、partition(分割)

fliter将符合条件的元素产生一个集合返回,partition将其分隔成满足与不满足条件的两元组

scala> (1 to 10) filter (_%2 == 0)
res24: scala.collection.immutable.IndexedSeq[Int] = Vector(2, 4, 6, 8, 10)
scala> (1 to 10) partition (_%2 == 0)
res25: (scala.collection.immutable.IndexedSeq[Int], scala.collection.immutable.IndexedSeq[Int]) = (Vector(2, 4, 6, 8, 10),Vector(1, 3, 5, 7, 9))
scala> res25._1
res26: scala.collection.immutable.IndexedSeq[Int] = Vector(2, 4, 6, 8, 10)
scala> res25._2
res27: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 3, 5, 7, 9)

find、exists、count

find返回符合条件的第一个选项 Option,exists返回Boolean值,count返回满足条件的个数Int

scala> (1 to 10) find (_%2==0)
res30: Option[Int] = Some(2)
scala> (1 to 10) exists (_%2==0)
res50: Boolean = true
scala> (1 to 10) count (_ %2 ==0)
res57: Int = 5

zip

顾名思义就是像拉链一样彼此咬合生成新的集合,长短不一的时候以短为准

scala> Array(1, 2, 3) zip List("a", "b", "c", "d")
res53: Array[(Int, String)] = Array((1,a), (2,b), (3,c))

作者:sleep_NULL
链接:https://www.jianshu.com/p/06808aed982b
来源:
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

你可能感兴趣的:(【Spark学习笔记】Scala语法)