快学Scala(第2版)

最新书摘:
  • okfine
    2024-02-22
    By default, Scala futures are executed on the global fork-join pool. That works well for computationally intensive tasks.The Executors class from the Java concurrency library gives you several choices. A cached thread pool works well for I/O intensive workloads.
  • okfine
    2024-02-22
    A Future object is read-only. The value of the future is set implicitly when the task has finished or failed. A Promise is similar, but the value can be set explicitly.The producer, however, has more flexibility when using a Promise.
  • okfine
    2024-02-22
    Scala futures do not have a mechanism for cancellation. If you want to stop unnecessary work, you have to provide your own mechanism.
  • okfine
    2024-02-22
    For better performance, the future should report its result to a callback function.
  • okfine
    2024-02-22
    The java.util.concurrent package has a Future interface that is much more limited than the Scala Future trait. A Scala future is equivalent to the CompletionStage interface in Java 8.
  • okfine
    2024-02-22
    A data structure that assigns tasks to threads is usually called a thread pool. In Java, the Executor interface describes such a data structure. Scala uses the ExecutionContext trait instead.
  • okfine
    2024-02-22
    The traditional approach, in which concurrent tasks have side effects that mutate shared data, is tedious and error-prone. Scala encourages you to think of a computation in a functional way. A computation yields a value, sometime in the future. As long as the computations don’t have side effects, you can let them run concurrently and combine the results when they become available.
  • [已注销]
    2019-01-05
    构造器以如下顺序执行:1. 首先调用超类的构造器2. 特质构造器在超类构造器之后、类构造器之前执行3. 特质由左到右被构造4. 在每个特质中,父特质优先被构造5. 如果每个特质共有一个父特质,而那个父特质已经被构造,则该父特质不会被再次构造6. 所有特质构造完毕,子类被构造
  • 龙三
    2016-06-19
    隐式转换在如下三种各不相同的情况会被考虑:1)当表达式的类型与预期的类型不同时2)当对象访问一个不存在的成员时3)当对象调用某个方法,而该方法的参数声明与传入参数不匹配时
  • geting
    2016-01-27
    闭包由代码和代码用到的任何非局部变量定义构成。def mulBy(factor: Double) = (x: Double) => factor *xval triple = mulBy(3)val half = mulBy(0.5)println(triple(14) + " " + half(14)) // print 42 7mulBy的首次调用将参数变量factor设为3。该变量在(x:Double)=> factor * x函数的函数体内被引用,该函数被存入triple。然后参数变量factor从运行时的栈上被弹出。接下来,mulBy再次被调用,这次factor被设为了0.5。该变量在 (x:Double)=>factor * x函数的函数体内被引用,该函数被存入half。每一个返回的函数都有自己的factor设置
  • 66436682
    2015-03-26
    Can you call friends with that function? Of course you can.你能用这个函数调用 friends 吗?当然可以。
  • lao
    2012-03-24
    A tuple value is formed by enclosing individual values in parentheses. Forexample,(1, 3.14, "Fred")is a tuple of typeTuple3[Int, Double, java.lang.String]which is also written as(Int, Double, java.lang.String)
  • lao
    2012-03-20
    Key points of this chapter: • Use an Array if the length is fixed, and an ArrayBuffer if the length can vary. • Don’t use new when supplying initial values. • Use () to access elements. • Use for (elem <- arr) to traverse the elements. • Use for (elem <- arr if . . . ) . . . yield . . . to transform into a new array. • Scala and Java arrays are interoperable; with ArrayBuffer, use scala.collection.JavaConversions.