官术网_书友最值得收藏!

Why Scala?

Development has evolved a lot since Java was originally invented 20 years ago. Java, as an imperative language, was designed for the Von-Neumann architecture, where a computer consists of a processor, a memory, and a bus that reads both instructions and data from the memory into the processor. In that architecture, it is safe to store values in variables, and then mutate them by assigning new values. Loop controls are thus normal to use, as shown in the following code:

  for ( int i=0; i < 1000000;  i++) {
    a=a+1;
  }

However, over the past decade, hardware engineers have been stressing that the Von-Neumann model is no longer sustainable. Since processors hit physical limitations at high frequencies, engineers look for evolution beyond the single-processor model. Nowadays, manufacturers integrate multiple cores onto a single integrated circuit die—a multiprocessor chip. Similarly, the emergence of cloud computing and Hadoop clusters bring into play another dimension in computing, where resources are distributed across different nodes.

The imperative programming style dictates thinking in terms of time. In distributed programming, we need to think in terms of space: build one block, then another, and then build another block—like building in Lego. When building in space, it is easier to build each block on a different process and parallelize the execution of the required blocks.

Unfortunately, the imperative logic is not compatible with modern distributed systems, cloud applications, and scalable systems. In practice, in parallelized systems, it is unsafe to assign a new value to a variable as this happens in a single node and other nodes are not aware of the local change. For this reason, the simple for loop cannot be parallelized into 10 or 100 nodes.

Effective software development techniques and language design evolved over the past decade, and as such, Scala is an advanced scalable language that restricts imperative features and promotes the development of functional and parallelized code blocks. Scala keeps the object-oriented model and provides functional capabilities and other cool features.

Moreover, Scala significantly reduces boilerplate code. Consider a simple Java class, as shown in the following code:

public class Person { 
  public final String name;
  public final int age;
  Person(String name, int age) { 
    this.name=name;
    this.age=age;
  }
}

The preceding code can be expressed in Scala with just a single line, as shown:

case class Person(val name: String, val age: Int)

For distributed computations and parallelism, Scala offers collections. Splitting an array of objects into two separate arrays can be achieved using distributed collections, as shown in the following code:

val people: Array[Person]
val (minors,adults) = people partition (_.age < 18)

For concurrency, the Actor model provides actors that are similar to objects but inherently concurrent, uses message-passing for communication, and is designed to create an infinite number of new actors. In effect, an actor under stress from a number of asynchronous requests can generate more actors that live in different computers and JVMs and have the network topology updated to achieve dynamic autoscaling through load balancing.

主站蜘蛛池模板: 米林县| 汶上县| 江孜县| 泗阳县| 济宁市| 洪雅县| 邻水| 汶川县| 清水县| 密云县| 墨脱县| 鹤岗市| 中牟县| 延吉市| 晋中市| 达孜县| 宜州市| 平原县| 迁西县| 安多县| 双城市| 古丈县| 银川市| 凤山县| 嘉黎县| 临漳县| 延津县| 荆州市| 北辰区| 南平市| 江孜县| 天等县| 红原县| 枣庄市| 高安市| 芦溪县| 金山区| 介休市| 丘北县| 驻马店市| 祁东县|