Scala by Example: 第一个例子

   1: // Java风格的代码
   2: def oldSchoolSort(xs: Array[Int]) {
   3:   //Scala支持嵌套函数
   4:   def sort1(l: Int, r: Int) {
   5:     val pivot = xs((l + r) / 2)
   6:     var i = l
   7:     var j = r
   8:     while(i <= j) {
   9:       while(xs(i) < pivot) i += 1
  10:       while(xs(j) > pivot) j -= 1
  11:       if(i < j && xs(i) != xs(j)) swap(i, j)
  12:       if(i <= j) {
  13:         i += 1
  14:         j -= 1
  15:       }
  16:     }
  17:     if(l < j) sort1(l, j)
  18:     if(r > i) sort1(i, r)
  19:   }
  20:  
  21:   // Scala是种“面向表达式”的语言,每个函数都会返回值。如果没有东西可以返回,那么默认返回 ()
  22:   // () 属于Unit类。返回Unit的函数也被称为procedures。如果函数不是返回Unit,那么,函数体前
  23:   // 面必须加上 "="
  24:   def swap(i: Int, j: Int) {
  25:     val t = xs(i); xs(i) = xs(j); xs(j) = t
  26:     () // 可以省略,加上则明确表明返回Unit
  27:   }
  28:  
  29:   sort1(0, xs.length - 1)
  30: }
  31:  
  32: // 更具函数风格的sort,注意filter为“partially applied function”,即部分实现函数(有译作
  33: // 偏应用函数的,不敢苟同),另外,pivot > 的完整形式为:x => pivot > x
  34: def sort(xs: Array[Int]): Array[Int] = {
  35:   if(xs.length <= 1) xs else {
  36:     val pivot = xs(xs.length / 2)
  37:     Array.concat(sort(xs filter (pivot >)), xs filter (pivot ==),
  38:       sort(xs filter (pivot <)))
  39:   }
  40: }
  41:  
  42: val xs = Array(23, 3, 0, -3, 3, 12)
  43: println(sort(xs).mkString(", "))

以上代码中的oldSchoolSort已经小小的修改了下,原书的例子有些小小的界桩错误,虽然不影响结果,但是却几乎降低了一半的效能。另外,说到效能,不能不提一下内存的问题。不像旧式风格的代码只是在原有的对象上进行修改,函数风格的代码(sort)会生成不变对象来进行传递,运行时需要临时占用更多的内存。

Scala by Example PDF下载,原书为Scala官网上的示例。

PS. 昨天在Groovy和Android项目上遇到的IDEA故障今天出现在了Scala上,我英明神武的断言此乃文件系统权限问题,并一举命中selinux。关闭Selinux后,一切恢复了和谐……

PS II. 和谐了没多久,发现Scala支持的配置机制有bug,遂将Scala运行时从Global级别降级到Project Level,顺利“绕过”问题。

PS III. 然后就发现ibus在IDEA中不断的丢失焦点……也就是说在IDEA里打中文从此只能“偶尔为之”。

PS IV. 最后想用Ctrl+Alt+L格式化代码,结果热键冲突,导致操作系统锁定,只好悲愤的重敲一遍十几位的密码……

Technorati 标签: Scala
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1 Introduction 1 2 A First Example 3 3 Programming with Actors and Messages 7 4 Expressions and Simple Functions 11 4.1 Expressions And Simple Functions . . . . . . . . . . . . . . . . . . . . . . 11 4.2 Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 4.3 Conditional Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 4.4 Example: Square Roots by Newton’s Method . . . . . . . . . . . . . . . . 15 4.5 Nested Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 4.6 Tail Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 5 First-Class Functions 21 5.1 Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 5.2 Currying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 5.3 Example: Finding Fixed Points of Functions . . . . . . . . . . . . . . . . 25 5.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 5.5 Language Elements Seen So Far . . . . . . . . . . . . . . . . . . . . . . . 28 6 Classes and Objects 31 7 Case Classes and Pattern Matching 43 7.1 Case Classes and Case Objects . . . . . . . . . . . . . . . . . . . . . . . . 46 7.2 Pattern Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 8 Generic Types and Methods 51 8.1 Type Parameter Bounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 8.2 Variance Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56iv CONTENTS 8.3 Lower Bounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 8.4 Least Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 8.5 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 8.6 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 9 Lists 63 9.1 Using Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 9.2 Definition of class List I: First Order Methods . . . . . . . . . . . . . . . 65 9.3 Example: Merge sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 9.4 Definition of class List II: Higher-Order Methods . . . . . . . . . . . . . 70 9.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 10 For-Comprehensions 79 10.1 The N-Queens Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 10.2 Querying with For-Comprehensions . . . . . . . . . . . . . . . . . . . . . 81 10.3 Translation of For-Comprehensions . . . . . . . . . . . . . . . . . . . . . 82 10.4 For-Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 10.5 Generalizing For . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 11 Mutable State 87 11.1 Stateful Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 11.2 Imperative Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . 91 11.3 Extended Example: Discrete Event Simulation . . . . . . . . . . . . . . . 92 11.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 12 Computing with Streams 99 13 Iterators 103 13.1 Iterator Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 13.2 Constructing Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 13.3 Using Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
1 Introduction 1 2 A First Example 3 3 Programming with Actors andMessages 7 4 Expressions and Simple Functions 11 4.1 Expressions And Simple Functions . . . . . . . . . . . . . . . . . . . . . . 11 4.2 Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 4.3 Conditional Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 4.4 Example: Square Roots by Newton’sMethod . . . . . . . . . . . . . . . . 15 4.5 Nested Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 4.6 Tail Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 5 First-Class Functions 21 5.1 Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 5.2 Currying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 5.3 Example: Finding Fixed Points of Functions . . . . . . . . . . . . . . . . 25 5.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 5.5 Language Elements Seen So Far . . . . . . . . . . . . . . . . . . . . . . . 28 6 Classes and Objects 31 7 Case Classes and PatternMatching 43 7.1 Case Classes and Case Objects . . . . . . . . . . . . . . . . . . . . . . . . 46 7.2 PatternMatching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 8 Generic Types andMethods 51 8.1 Type Parameter Bounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 8.2 Variance Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 8.3 Lower Bounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 8.4 Least Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 8.5 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 8.6 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Lists 63 9.1 Using Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 9.2 Definition of class List I: First OrderMethods . . . . . . . . . . . . . . . 65 9.3 Example: Merge sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 9.4 Definition of class List II: Higher-OrderMethods . . . . . . . . . . . . . 70 9.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 0 For-Comprehensions 79 10.1 The N-Queens Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 10.2 Querying with For-Comprehensions . . . . . . . . . . . . . . . . . . . . . 81 10.3 Translation of For-Comprehensions . . . . . . . . . . . . . . . . . . . . . 82 10.4 For-Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 10.5 Generalizing For . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 1 Mutable State 87 11.1 Stateful Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 11.2 Imperative Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . 91 11.3 Extended Example: Discrete Event Simulation . . . . . . . . . . . . . . . 92 11.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 2 Computing with Streams 99 3 Iterators 103 13.1 IteratorMethods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 13.2 Constructing Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 13.3 Using Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 4 Lazy Values 109 5 Implicit Parameters and Conversions 113

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值