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

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.

主站蜘蛛池模板: 宝山区| 廊坊市| 伽师县| 安岳县| 淮安市| 巫溪县| 中江县| 武强县| 无锡市| 雷州市| 柘荣县| 宝山区| 双辽市| 香港 | 鱼台县| 兰坪| 平武县| 九寨沟县| 金溪县| 巴楚县| 泰顺县| 新昌县| 元谋县| 会理县| 武安市| 龙山县| 焉耆| 鄄城县| 天柱县| 满城县| 乐亭县| 沁水县| 穆棱市| 闵行区| 望江县| 泰安市| 江阴市| 孙吴县| 三穗县| 长治市| 师宗县|