Scala,一门「特立独行」的语言!

开发 后端
入门 Spark 的路上很难不接触 Scala 。Scala 似乎是为 java 提供了很多『类似函数式编程』的语法糖,这里记录一下这个语言独特的地方分享给读者朋友们。

[[410308]]

本文转载自微信公众号「Piper蛋窝」,作者Piper蛋 。转载本文请联系Piper蛋窝公众号。

入门 Spark 的路上很难不接触 Scala 。Scala 似乎是为 java 提供了很多『类似函数式编程』的语法糖,这里记录一下这个语言独特的地方分享给读者朋友们。

参考资料主要有:

  • 曹洁 . Spark大数据分析技术(Scala版)[M]. 北京航空航天大学出版社, 2021. ISBN:9787512433854
  • 陈欢 , 林世飞 . Spark最佳实践[M]. 人民邮电出版社, 2016. ISBN:9787115422286

Scala 基本思想与注意事项

Sacla 即 Scalable Language ,正如其名,是一门可伸缩的编程语言:

  • 基于 java 的虚拟机( Scala 会被编译成 JVM 字节码)
  • 但是既可以当脚本使用,又可以构造大型系统
  • 是静态语言,但是可以像动态语言那样支持交互式编程
  • 面型对象:每一个值都是对象,每一次运算都是一次方法调用
  • 函数式编程:所有函数都是对象,函数是“一等公民”
  • Scala 中几乎一切都是表达式

scala 是解释器, scalac 是编译器;可以直接 scala test.scala ,也可以 scalac test.scala & scala test (先把源码编译为字节码,再把字节码放到虚拟机中解释运行)。还可用输入 scala 进入交换编程界面。

所以要注意的是,需要先安装 JDK ,并且设置好环境变量 JAVA_HOME 。此外,更加重要的是, Scala 小版本兼容:2.12.x 与 2.13.x 这两者不兼容,2.12.10 与 2.12.11 才兼容。

最基本的语法示例

类型的声明、控制结构(for、模式匹配、case)

  1. // 变量 
  2. val two: Int = 1 + 1 
  3.  
  4. var one: Int = 1 
  5. var one: String = 'one' 
  6.  
  7. // 函数 
  8. def addOne(x: Int): Int = x + 1 
  9.  
  10. def add(x: Int, y: Int): Int = { 
  11.     x + y 
  12.  
  13. // 部分控制结构 
  14. var filename =  
  15.     if (!args.isEmpty) args(0) 
  16.     else "default.txt" 
  17.  
  18. for (i <- 1 to 4) 
  19.     println("iteration " + i) 

1 to 4 是 [1,2,3,4] ,而 i until 4 是 [1,2,3] 。

关于 for 还有一些奇技淫巧。

  1. // 多个区间 
  2. for (a <- 1 to 2; b <- 1 to 2) { 
  3.     println("a: " + a + ", b: " + b) 
  4. // 结果 
  5. a: 1, b: 1 
  6. a: 1, b: 2 
  7. a: 2, b: 1 
  8. a: 2, b: 2 
  9.  
  10. // 过滤器 
  11. val list1 = List(3, 5, 2, 1, 7) 
  12. for (x <- list1 if x % 2 == 1) print(" " + x) 
  13. // 3 5 1 7 

关于模式匹配,则有更多奇技淫巧。这里我直接参考:scala中case的用法[1]

  1. // 一.简单匹配,值匹配: 
  2.  
  3. val bools = List(truefalse
  4. for (bool <- bools) { 
  5.     bool match { 
  6.         case true => println("heads"
  7.         case false => println("tails"
  8.         case _ => println("something other than heads or tails (yikes!)"
  9.     } 
  10.  
  11. import scala.util.Random 
  12. val randomInt = new Random().nextInt(10) 
  13. randomInt match { 
  14.     case 7 => println("lucky seven!"
  15.     case otherNumber => println("boo, got boring ol' " + otherNumber) 
  16.  
  17. // 二. 类型匹配 
  18.  
  19. val sundries = List(23, "Hello", 8.5, 'q'
  20. for (sundry <- sundries) { 
  21.     sundry match { 
  22.         case i: Int => println("got an Integer: " + i) 
  23.         case s: String => println("got a String: " + s) 
  24.         case f: Double => println("got a Double: " + f) 
  25.         case other => println("got something else: " + other) 
  26.  
  27. // 三 根据顺序匹配 
  28.  
  29. val willWork = List(1, 3, 23, 90) 
  30. val willNotWork = List(4, 18, 52) 
  31. val empty = List() 
  32. for (l <- List(willWork, willNotWork, empty)) { 
  33.     l match { 
  34.         case List(_, 3, _, _) => println("Four elements, with the 2nd being '3'."
  35.         case List(_*) => println("Any other list with 0 or more elements."
  36.     } 
  37.  
  38. // 四 case里面用 guard 的数组匹配 
  39.  
  40. val tupA = ("Good""Morning!"
  41. val tupB = ("Guten""Tag!"
  42.     for (tup <- List(tupA, tupB)) { 
  43.         tup match { 
  44.             case (thingOne, thingTwo) if thingOne == "Good" => 
  45.             println("A two-tuple starting with 'Good'."
  46.             case (thingOne, thingTwo) =>println("This has two things: " + thingOne + " and " + thingTwo) 
  47.         } 
  48.  
  49. // 五 对象深度匹配 
  50.  
  51. case class Person(name: String, age: Int
  52. val alice = new Person("Alice", 25) 
  53. val bob = new Person("Bob", 32) 
  54. val charlie = new Person("Charlie", 32) 
  55. for (person <- List(alice, bob, charlie)) { 
  56.     person match { 
  57.         case Person("Alice", 25) => println("Hi Alice!"
  58.         case Person("Bob", 32) => println("Hi Bob!"
  59.         case Person(name, age) => 
  60.             println("Who are you, " + age + " year-old person named " + name + "?"
  61.     } 
  62.  
  63. // 六 正则表达式匹配 
  64.  
  65. val BookExtractorRE = """Book: title=([^,]+),\s+authors=(.+)""".r 
  66. val MagazineExtractorRE = """Magazine: title=([^,]+),\s+issue=(.+)""".r 
  67.  
  68. val catalog = List( 
  69.     "Book: title=Programming Scala, authors=Dean Wampler, Alex Payne"
  70.     "Magazine: title=The New Yorker, issue=January 2009"
  71.     "Book: title=War and Peace, authors=Leo Tolstoy"
  72.     "Magazine: title=The Atlantic, issue=February 2009"
  73.     "BadData: text=Who put this here??" 
  74.  
  75. for (item <- catalog) { 
  76.     item match { 
  77.         case BookExtractorRE(title, authors) => 
  78.             println("Book \"" + title + "\", written by " + authors) 
  79.         case MagazineExtractorRE(title, issue) => 
  80.             println("Magazine \"" + title + "\", issue " + issue) 
  81.         case entry => println("Unrecognized entry: " + entry) 
  82.     } 

关于 case ,我想强调其在“解包”中的应用:

  1. dict = Map("Piper" -> 95, "Bob" -> 90) 
  2. dict.foreach { 
  3.     case (k, v) => printf( 
  4.         "grade of %s is %s/n", k, v 
  5.     ) 
  6.  
  7. grade of Piper is 95 
  8. grade of Bob is 90 

上述:使用了 foreach { case () => {} } ,注意 foreach 的大括号。与下面等效。

  1. dict = Map("Piper" -> 95, "Bob" -> 90) 
  2. dict.foreach ( 
  3.     x => println( 
  4.         s"grade of ${x._1} is ${x._2}" 
  5.     ) 
  6.  
  7. grade of Piper is 95 
  8. grade of Bob is 90 

Scala 语法独特的地方

无参数方法,调用时不用加括号:args.isEmpty。

  1. def width: Int = if (height == 0) 0 else contents(0).length 
  2.  
  3. width  // 调用 

for 中使用 <- ,相当于 Python 的 in 。

继承用关键字 extends :class A(a: Int) extends B 。

单实例对象 / 静态成员变量与方法定义在 object 中:

  1. object Timer { 
  2.     var count = 0 
  3.     def currentCount() : Long = { 
  4.         count += 1 
  5.         count 
  6.     } 
  7.  
  8. Timer.currentCount()  // 直接调用 
  9.  
  10. class Timer { 
  11.     ... 

函数返回不必非要加 return ,默认最后一个表达式。

函数式:匿名函数作为参数,并且还可以更简洁

  1. val numbers = List(1, -3, -5, 9, 0) 
  2.  
  3. numbers.filter((x) => x > 0) 
  4. numbers.filter(x => x > 0) 
  5. numbers.filter(_ > 0)  // 一个参数且函数中仅被使用一次时 

_ 具有特殊的意义与工作(占位)

  1. // 部分应用函数 
  2. def adder(m: Int, n: Int) = m + n 
  3.  
  4. val add2 = adder(2, _: Int)  // add2: (Int) => Int = <function1> 
  5. add2(3)  // res1: Int = 5 
  6.  
  7. // 柯里化 currying 
  8. def curriedSum(x: Int)(y: Int) = x + y 
  9. curriedSum (1)(2) 
  10.  
  11. val onePlus = curriedSum(1)_  // 注意这里使用了 _ 
  12. onePlus(2) 
  13.  
  14. // 模式匹配 
  15. var times = 1 
  16. times match { 
  17.     case 1 => "one" 
  18.     case 2 => "two" 
  19.     case _ => "other" 

Scala 的面向对象与一等公民“函数”

  1. (1).+(2)  // 3 

如上,(1)是对象,.+(2)是方法调用。Scala 中万物皆对象。

  1. var increase = (x: Int) => x + 1 

如上,函数是一等公民,可以赋值给变量。

基本数据结构

有以下概念:

  • 不可变列表 List 与可变列表 ListBuffer
  • 定长数组 Array 与变长数组 ArrayBuffer
  • 不可变集合 Set 与可变集合 scala.collection.mutable.Set
  • 映射 Map 与 可变映射 scala.collection.mutable.Map
  • 元组 Tuple

注意事项与 Scala 奇技淫巧

使用 until 是遍历数组的好办法,by 和 _* 特殊意义:

  1. for (i <- 0 until.length) { } 
  2.  
  3. Array (1,3,5,7,9,11)  // 等价于 
  4. Array[Int](1 to 11 by 2:_* "Int")  // _* 有种解包的意味 

使用 yield 生成数组

  1. val a = Array(1, 2, 3, 4) 
  2. val res1 = for (ele <- a) yield 2 * ele 
  3. // 2, 4, 6, 8 

元组的下标从 1 开始

  1. val person = (1, 2, "ABC"
  2. person._1  // 1 

拉链操作 zip

  1. val symbols = Array("<""-"">"
  2. val counts = Array(2, 10, 2) 
  3. val pairs = symbols.zip(counts) 
  4. // Array[(String, Int)] = Array((<, 2), (-, 10), (>, 2)) 
  5. for ((s, n) <- pairs) print(s * n) 
  6. <<---------->> 

Map 神奇操作

  1. // 创建 
  2. val dict = Map("Piper" -> 95, "Bob" -> 90) 
  3. val kv   = Map(("Piper", 95), ("Bob", 90)) 
  4.  
  5. // 取值 
  6. dict("Piper"
  7.  
  8. // 合并 ++ 
  9. dict ++ kv 
  10. dict.++(kv) 
  11.  
  12. // 添加 + ,删除 - 
  13. val n = dict + ("Tom" -> 91) 
  14. val l = dict - "Tom" 

对于可变 Map :

  1. // += -= 
  2. dict += (("Tom", 91), ("Jerry", 87)) 
  3. dict -= "Tom" 
  4. dict -= ("Jerry""Bob"
  5.  
  6. // ++= --= 与其他集合相联系 
  7. dict ++= List(("Tom", 91), ("Jerry", 87)) 
  8. dict --= List("Jerry", "Bob") 

:: 与 ::: 创建列表

  1. 1::3::5::Nil  // List[Int] = List(1, 3, 5) 

注意 :: 是右结合的:(1::(3::(5::Nil))) 。

  1. // ::: 用来连接列表 
  2. val L4 = L3 ::: List("Hadoop""Hbase"

关于数据结构的讨论(List or Array?)

  • 多用 List 而非 Array
  • 列表的结构是递归的(即链表,linkedList),而数组是平等的

参考:

  • scala中List、Array、ListBuffer、ArrayList、Set、元组区别[2]
  • Scala学习笔记5 (集合 Collections)[3]

参考资料

[1]scala中case的用法: https://blog.csdn.net/qq_41669665/article/details/86158993

[2]scala中List、Array、ListBuffer、ArrayList、Set、元组区别: https://blog.csdn.net/mar_ljh/article/details/81910286

[3]Scala学习笔记5 (集合 Collections): https://blog.csdn.net/lyrebing/article/details/20362227

【责任编辑:武晓燕 TEL:(010)68476606】

 

责任编辑:武晓燕 来源: Piper蛋窝
相关推荐

2011-05-01 21:48:54

Ubuntu 11.0

2019-11-18 11:00:58

程序员编程语言

2015-07-28 15:35:48

学习语言

2011-12-30 09:33:02

程序员语言

2014-12-03 09:48:36

编程语言

2012-03-28 09:40:40

JavaScript

2012-09-04 11:20:31

2022-02-27 14:45:16

编程语言JavaC#

2022-11-04 11:11:15

语言入职项目

2017-04-07 10:45:43

编程语言

2017-04-07 16:49:00

语言程序编程

2022-02-21 11:15:59

编程语言后端开发

2023-02-08 07:35:43

Java语言面向对象

2020-09-27 15:52:02

编程语言C 语言Python

2017-10-26 11:44:19

工具语言编写

2011-07-14 17:58:11

编程语言

2022-09-07 08:05:32

GScript​编程语言

2015-08-17 15:12:56

新技术语言框架

2013-07-26 10:23:04

2020-11-12 07:00:50

JavaScript前端编程语言
点赞
收藏

51CTO技术栈公众号