`
stfairy
  • 浏览: 564 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

未来编程语言必备的特性 Promising features for languages in the future

阅读更多

 

本文转载自: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.

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics