本文转载自:http://xjia.heroku.com/2011/08/11/promising-features-for-languages-in-the-future/
Traceur is Google’s vehicle for JavaScript language design experimentation which allows you to use features from the future today. I have selected several features from Traceur which are supposed to be promising in the future of programming language design.
Destructuring Assignment
Destructuring assignment is a nice way to assign or initialize several variables at once:
let [a, [b], c, d] = ['hello', [', ', 'junk'], ['world']];
print(a + b + c); // hello, world
It can also destructure objects:
let pt = { x: 123, y: 444 };
let rc = { topLeft: { x: 1, y: 2 }, bottomRight: { x: 3, y: 4} };
let {x, y} = pt; // unpack the point
let {topLeft: {x: x1, y: y1}, bottomRight: {x: x2, y: y2}} = rc;
print(x + y); // 567
print([x1, y1, x2, y2].join(',')); // 1,2,3,4
In my view the destructuring assignment is just a limited version of pattern matching. Many applications written in Erlang using pattern matching and Erlang’s bit syntax are really impressive, which are able to parse binary data, e.g. TCP streams, in a clear and concise manner.
Generators
Generators are first-class coroutines, represented as objects encapsulating suspended execution contexts (i.e., function activations). In reality generators can be seen as a useful idiom in using coroutines. For instance, generators can be used to implement the “infinite” sequence of Fibonacci numbers:
function fibonacci() {
let [prev, curr] = [0, 1];
for (;;) {
[prev, curr] = [curr, prev + curr];
yield curr;
}
}
Generators can be iterated over in loops:
for (n of fibonacci()) {
// truncate the sequence at 1000
if (n > 1000) break;
print(n);
}
Generators are iterators:
let seq = fibonacci();
print(seq.next()); // 1
print(seq.next()); // 2
print(seq.next()); // 3
print(seq.next()); // 5
Deferred Functions
Asynchronous programming is very hard in ECMAScript host environments which are typically single threaded and use callbacks for operations which may take a long time like network IO or system timers. For example:
function animate(element) {
let i = -1;
function continue() {
i++;
if (i < 100) {
element.style.left = i;
window.setTimeout(continue, 20);
}
}
}
animate(document.getElementById('box'));
However, pausing execution for long periods is undesirable. Deferred functions allow you to write asynchronous non-blocking code without writing callback functions, which don’t compose well. With deferred functions, you can use control flow constructs that you’re used to, inline with the rest of your code.
function deferredAnimate(element) {
for (let i = 0; i < 100; ++i) {
element.style.left = i;
await deferredTimeout(20);
}
}
deferredAnimate(document.getElementById('box'));
Rest Parameters and Spread Operator
Reset parameters allows your functions to have variable number of arguments.
function push(array, ...items) {
items.forEach(function(item) {
array.push(item);
});
}
The spread operator is like the reverse of rest parameters, which allows you to expand an array into multiple formal parameters.
function push(array, ...items) {
array.push(...items);
}
function add(x, y) {
return x + y;
}
var numbers = [4, 38];
add(...numbers); // 42
Conclusion
Many other features are not listed here since they have been already implemented, though rarely used or even seen, in languages such as Scala orHaskell. I think they are all very useful and essential for skilled programmers and will ease the burden of more and more complicated programming tasks.
分享到:
相关推荐
Driven by the rapid escalation of the wireless capacity requirements imposed by advanced multimedia ...guidelines for NOMA systems and identify promising research opportunities for the future.
《Nonlinear IHS: A Promising Method for Pan-Sharpening》是一篇关于图像处理领域的研究文章,专注于多光谱与全色图像的融合技术。本文提到的Nonlinear IHS(非线性亮度、对比度、饱和度)方法是基于传统IHS(亮度...
Biometric_ Promising Frontiers for Emerging Identification Market指纹图像识别国外文献
根据提供的文档信息,我们可以归纳出以下相关知识点: ### 一、存储环境与IT挑战 #### 数据增长持续 随着数字化转型的加速,企业面临的...随着技术的不断发展和完善,iSCSI有望在未来成为主流的存储网络技术之一。
Furthermore, Python's popularity and extensive documentation make it easier for maintenance and updates in the future. The adoption of a Browser/Server (B/S) architecture for the system design is ...
laic mirrors are used, while ... the concentrator can excel in both high-concentration photovoltaic and concentrating thermal systems, providing valuable insights for future solar energy system designs.
430G2553 as the main control chip. The electric car motor drive adopts the L298N chip, ... Its simplicity and adaptability make it a promising foundation for future advancements in automotive technology.
With this in mind, LTE is the central focus of the mobile industry, as LTE is the most promising technology on the market. For mobile operators running GSM/UMTS systems,LTE is a natural choice for ...
paper explores the potential of UAV images for monitoring degradation of littoral vegetation in Puerto Villamil (Isabela Island, Galapagos, Ecuador). Imagery was captured using two camera types: Red ...
led consortium of over 300 broadcasters, manufacturers, network operators, software developers, regulatory bodies and others in over 40 countries committed to designing global standards for the ...
These features make the DVSI scheme a promising option for microgrid supplying sensitive loads. The topology and control algorithm are validated through extensive simulation and experimental results.
Cognitive radio has emerged as a promising technology for maximizing the utilization of the limited radio bandwidth while accommodating the increasing amount of services and applications in wireless ...
状态 of the topic are analyzed, and common wireless communication methods such as Bluetooth, Zigbee, and LoRa are compared in terms of their features and suitable applications. WiFi is selected as the...
In conclusion, the proposed credit evaluation model based on natural language processing and deep learning is a promising approach for improving the accuracy and efficiency of credit evaluation tasks....
基于拉曼光谱的食用油加热过程中反式脂肪酸含量的快速检测方法,史如意,刘湘江,反式脂肪酸(TFA)是一类在食品工业中广泛存在的不饱和脂肪酸。近年来由于研究报道称TFA的摄取量会增加冠心病,糖尿病等疾病的发生
The proposed approach not only contributes to improving the overall performance of robots but also paves the way for future advancements in the field of robotics and automation, where communication ...
Detection and prevention of global navigation satellite system (GNSS) “spoofing” attacks, or the broadcast of false global navigation ... Our conclusion lists promising areas of future research