《Scala for the Impatient》的原文摘录

  • 更好的做法是使用字符串插值(string interpolation): print(f"Hello, $name! In six months, you'll be ${age + 0.5}%7.2f years old%n") (查看原文)
    [已注销] 2赞 2019-01-05 16:54:07
    —— 引自第24页
  • 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. (查看原文)
    lao 2012-03-20 22:08:55
    —— 引自章节:Chapter3 working with arrays
  • A tuple value is formed by enclosing individual values in parentheses. For example, (1, 3.14, "Fred") is a tuple of type Tuple3[Int, Double, java.lang.String] which is also written as (Int, Double, java.lang.String) (查看原文)
    lao 2012-03-24 15:20:30
    —— 引自章节:Chapter4 Maps and Tuples
  • Can you call friends with that function? Of course you can. 你能用这个函数调用 friends 吗?当然可以。 (查看原文)
    66436682 2015-03-26 04:02:41
    —— 引自第253页
  • 闭包由代码和代码用到的任何非局部变量定义构成。 def mulBy(factor: Double) = (x: Double) => factor *x val triple = mulBy(3) val half = mulBy(0.5) println(triple(14) + " " + half(14)) // print 42 7 mulBy的首次调用将参数变量factor设为3。该变量在(x:Double)=> factor * x函数的函数体内被引用,该函数被存入triple。然后参数变量factor从运行时的栈上被弹出。 接下来,mulBy再次被调用,这次factor被设为了0.5。该变量在 (x:Double)=>factor * x函数的函数体内被引用,该函数被存入half。 每一个返回的函数都有自己的factor设置 (查看原文)
    geting 2016-01-27 17:10:52
    —— 引自第156页
  • 隐式转换在如下三种各不相同的情况会被考虑: 1)当表达式的类型与预期的类型不同时 2)当对象访问一个不存在的成员时 3)当对象调用某个方法,而该方法的参数声明与传入参数不匹配时 (查看原文)
    龙三 2016-06-19 12:38:27
    —— 引自第326页
  • 构造器以如下顺序执行: 1. 首先调用超类的构造器 2. 特质构造器在超类构造器之后、类构造器之前执行 3. 特质由左到右被构造 4. 在每个特质中,父特质优先被构造 5. 如果每个特质共有一个父特质,而那个父特质已经被构造,则该父特质不会被再次构造 6. 所有特质构造完毕,子类被构造 (查看原文)
    [已注销] 2019-01-05 17:04:36
    —— 引自第145页
  • 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. (查看原文)
    okfine 2024-02-22 23:32:50
    —— 引自章节:17.1 在future中运行任务 278
  • 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 23:32:50
    —— 引自章节:17.1 在future中运行任务 278
  • 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 23:32:50
    —— 引自章节:17.1 在future中运行任务 278
  • For better performance, the future should report its result to a callback function. (查看原文)
    okfine 2024-02-22 23:32:50
    —— 引自章节:17.4 回调 282
  • 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 23:32:50
    —— 引自章节:17.7 Future对象中的方法288
  • 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 23:32:50
    —— 引自章节:17.8 Promise.289
  • 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 23:32:50
    —— 引自章节:17.9 执行上下文291