未來程式設計語言必備的特性 Promising features for languages in the future

來源:互聯網
上載者:User

轉載自: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 pointlet {topLeft: {x: x1, y: y1}, bottomRight: {x: x2, y: y2}} = rc;print(x + y); // 567print([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());  // 1print(seq.next());  // 2print(seq.next());  // 3print(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.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.