Review_深入理解Scala书评-查字典图书网
查字典图书网
当前位置: 查字典 > 图书网 > 编程 > 深入理解Scala > Review
sencouyan 深入理解Scala 的书评 发表时间:2015-02-14 00:02:03

Review

P18
scala> "Hello".filter(_ != 'l')
res1: String = Hello
应为
scala> "Hello".filter(_ != 'l')
res1: String = Heo

P19
译文:领域特定语言是用于特定领域的伪编程语言,这种语言专门用来解决手头的某个领域。
建议:领域特定语言是用于特定领域的伪编程语言,这种语言专门用来解决手头的某个领域的问题。
原文:A DSL is pseudo programming language that deals with a particular domain.

P19
而且能帮助你确定你得表达式是否能编译。
"得" -> "的"

P45
译文:很多 Scala 用户来自 Java 和 Ruby 语言,
建议:很多 Scala 用户来自 Java 和 Ruby 语言阵营,

P71
译文:早期成员定义解决的问题是当特质定义抽象成员并且其具体成员依赖此抽象成员时发生的。
原文:Early member definitions solve issues that occur when a trait defines an abstract value and relies on it in other concrete values.
这里的 value 翻译成 成员 很赞。

P72
`def handleMessage(msg: SimulationMessage, ctx: Simulation)` -> `def handleMessage(msg: SimulationMessage, ctx: SimulationContext)`

P73
排版问题:`译者注`可以加黑处理,但是接下来的"Scala的特质有个非常有用的..."这段不属于`译者注`,不应该加黑。

P74
`trait NetworkENtity` -> `trait NetworkEntity`

P74
值得商榷的地方

1. 如图 4.2 所示,MixableParent 紧跟在 NetworkEntity 后调用,
   As you can see, the MixableParent class is being called directly after NetworkEntity
2. 但是在 Router 之前。这意味着 NetworkEntity (译者注:应该是 Router) 中的行为不会被调用,
   but before Router. This means that the behavior in NetworkEntity is never called
3. 因为 MixableParent 不调用其超类!这意味着我们需要想个办法把 MixableParent(译者注:应该为 Router)移到线性化的前面去。
   because the MixableParent doesn’t call its super! Therefore, we have to find a way of getting MixableParent earlier in the linearization.
4. 因为 Scala 的线性化是从右往左,也就是说我们得创建个 MixableParent with Router with NetworkEntity,
   Because things linearize right to left in Scala, we want to try creating a MixableParent with Router with NetworkEntity.
5. 这首先需要我们把 Router 变成特质。在现实中这不一定可行,但我们就继续练习,我们来看看在 REPL 里会是什么效果。
   That first requires turning the Router class into a trait. This might not be feasible in real life, but let’s continue the exercise. We’ll see what this looks like in a Scala REPL session

这段是针对上一段的 Router with NetworkEntity 说的
针对 2 ,因为 NetworkEntity 是 extend MixableParent 的,那么在 Router <-- NetworkEntity <-- MixableParent 中最先调用的就是 MixableParent,而 MixableParent 中没有调用 super.handleMessage,所以原文说 “这意味着 NetworkEntity 中的行为不会被调用,”我认为是对的。
针对 3 ,我也认为原文的"我们需要想个办法把 MixableParent 移到线性化的前面的"是对的,我个人认为"线性化"的"前面"是指靠左边,而执行是从线性化的后面,即靠右边 开始的。最后这个 "线性化"表示为 MixableParent <-- Router <-- NetworkEntity。

像这样 MixableParent with Router with NetworkEntity 的话 Router 的功能就得到了调用,而 Router with NetworkEntity 是做不到的。
当然最后,原文也说了"同时 Router 特质存在跟 MixableParent 一样的问题 -- 不代理给父类!在我们的案例里是可以的,因为 Router 是个实体,是被混入其他行为的(而不像一般的 trait是用来混入其他实体的)"(P76)。这段也说明了在线性化 MixableParent <-- Router <-- NetworkEntity 中,调用在 Router 时就断了 MixableParent 也不会被调用了。

P77
清单 4.7 这个代码是编译不通过的,trait Logger 有一个 def log,但是在 trait DataAccess 中只写了 val logger = new Logger。

P79
清单 4.10 trait RemoteLogger 在复写 log 方法时 需要再前面加上 override。

P80
标题“经典构造器 with a twist”,原文“classic constructors with a twist”,这个真心不好翻,不如保留原文吧。

P82

4.3.3 总结
在 Scala 里有很多做“组合”的新方法,我的建议是选择......请确保在完全理解后再决定您的组合策略。

这段不是总结,而正文的一部分。原版中在这段下面还有一段“总结”,译文版中丢失了。
In section 11.3.2, we show an alternative means of composing objects using a func- tional approach. Although the concepts behind this approach are advanced, the approach offers a good middle ground between using constructors with default argu- ments and abstract member composition.

P84

译文:最有趣的这个类不但编译通过而且还能运行
建议:最有趣的是这个类不但编译通过而且还能运行

P84

译文:请注意这些类成功的链接
建议:请注意这些类成功地链接
原文:Notice that the classes link fine;

P84

译文:好吧,如果我们真相调用默认实现
建议:好吧,如果我们真想调用默认实现

P86

译文:4.5 public 接口应当提供返回值
原文:4.5 Provide return types in your public APIs
个人认为 接口 还是直接用 API 表示比较好,程序员都懂的。

P88

4.6 总结
为避免泄漏...那部分代码

这段不是总结,而是正文的一部分。缺少原文中真正的一段总结。
Scala’s object system is powerful and elegant. The body...This leads to the best use of objects in Scala.

P99

trait BinaryFormat[T] {
  def asBinary(entity: T): Array[Bytes]
}
这里的 Bytes 应该是 Byte

object Foo {
  ...
  def asBinary(entity: Foo) = "serializedFoo".toBytes
  ...
}
这里的 toBytes 应该是 getBytes

P105
译文:当然最好是对应用做一些探测(profile)来确定关键区域,而不是假定 HotSpot 会处理分配问题。
原文:Again, it’s best to profile an application to deter- mine critical regions rather than assuming HotSpot will take care of allocations.

个人认为 profile 翻译成 探测 不好,见<http://en.wikipedia.org/wiki/Profiling_(computer_programming)>,之前见网上一些翻译成 性能分析 。

P106

译文:Scala 隐式视图给用户...隐式视图能极大地减轻整合多个类似但又小区别的接品时的痛苦,
这里的“又”应该为“有”

P121
Scala 里的类型通过相对的绑定或路径来引用(type within scala arereferenced related to a bingidng or path)。
这段的英文里 arereferenced 印刷时写一起了。

P122
清单 6.2 中的
class Outer {
  ...
  def bar(x: X#Inner) = null
}
这里的 X 应为 Outer。

P125
清单 6.3 中的
scala> Resource.close(System.in)
这里的 close 应为 closeResource

P128
这页开头的代码示例中缺少 callbacks 的定义。我自己用 `private var callbacks = Map[Handle, this.type => Unit]()`可行。

P131
译文:因为 Scala 是一种多态的面向对象语言(译者注:这里应该是指 scala 支持泛型)
原文:Because Scala is a polymorphic object-oriented language,

在 Coursera 上 Martin Odersky 讲的 Functional Programming Principles in Scala 中 Week 4: Types and Pattern Matching 的 Lecture 4.1 - Polymorphism 里有这样说
Polymorphism means that a function type comes "in many forms".
In programming it means that
- the function can be applied to arguments of many types, or
- the type can have instances of many types.
We have seen two principal forms of polymorphism:
- subtyping: instances of a subclass can be passed to a base class
- generics: instances of a function or class are created by type parameterization.

结合后文,“多态意味着当需要编译时类型为 Traversable 时,可以使用 Set 类的实例,类为 Set 类继承了 Traversable。”
所以,我认为原文这里的 polymorphism 还是应该指 subtyping,也就是一般我们说的“多态”。

P161

译文:编译器报告 FIleLike.children
FIleLike -> FileLike

P178

译文:一眼看去可能会吓到。
建议:一眼看去可能会被吓到。

译文:因此理解不同集合类型的性能和使用模式就是变得非常重要了。
建议:因此理解不同集合类型的性能和使用模式就变得非常重要了。

P183

译文:在 Scala 里,有些控制流,比如非局部的 closure 返回 (non-local closure return)和 break 语句,是通过继承 scala.util.control.ControlThrowable 来实现的。
读者在读到这句话时可能会非常不理解,什么是 non-local closure return 呢? 到底是怎么继承 ControlThrowable 来实现的呢?
建议做一个译者注,比如写一个 non-local closure,
object Test {
  def closure(xs: List[Int]): Int = {
    var rec = 0
    for (x <- xs) {
      if (x == 0) {
        return rec
      }
      rec += x
    }
    rec
  }
}
然后 scalac 编译成 class 后,反编译看下就知道,反编译代码里有类似这样的"throw new NonLocalReturnControl.mcI.sp(this.nonLocalReturnKey1$1, Test..this.elem);",而这个 NonLocalReturnControl 就是继承 ControlThrowable 的。

P184
scala> val addresss = ...
address 的复数形式貌似是 addresses。

P189
译文: 最大的区别在于 HashSet 用用元素的 hash 值...
"用用" --> "用"

P196
这一页第一个代码块和第三个代码块重复了,第一个代码块在原文中是
scala> fibs drop 3 take 5 toList
res0: List[Int] = List(2, 3, 5, 8, 13)
scala> fibs
res1: Stream[Int] = Stream(0, 1, 1, 2, 3, 5, 8, 13, ?)

P197
原文:并且要知道那个方法是操作当前集合的值而不是创建个新集合
“那” --> “哪”

P198
8.3.2 节这个例子无法模拟,evt: Message[Int] with Undoable 不知道是什么意思?

P203

scala> (1 to 1000).par.foldLeft(Set[String]()) {
    | (set,value) =>
    | set + Thread.currentThread.toString()
    | }
这里的 set + Thread.currentThread.toString() 应为 set :+ Thread.currentThread.toString()

P209

原文:所以在考虑要把一个针对集合的方法写到多抽象时需要认真取舍
建议:所以在考虑要把一个针对集合的方法写到多么抽象时需要认真取舍

展开全文


推荐文章

猜你喜欢

附近的人在看

推荐阅读

拓展阅读

对“Review”的回应

青年哪吒 2015-03-10 09:42:22

N多不通的和错误的,源码和内容都有