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

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.

主站蜘蛛池模板: 永寿县| 阿克苏市| 汾西县| 宣汉县| 崇州市| 陕西省| 义乌市| 开封市| 盐池县| 黄骅市| 高阳县| 文山县| 镇巴县| 荥经县| 遂昌县| 娄烦县| 尉犁县| 隆德县| 叙永县| 崇义县| 乡城县| 淄博市| 阿拉善盟| 甘孜| 渝北区| 新泰市| 秦皇岛市| 巧家县| 宜章县| 沭阳县| 成安县| 衡阳县| 肇源县| 南投市| 从化市| 洪雅县| 神池县| 香格里拉县| 汕头市| 巫山县| 乡城县|