Click here to Skip to main content
12,503,198 members (61,709 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

9.1K views
1 bookmarked
Posted

Promising features for languages in the future

, 13 Aug 2011 CC (ASA 3U)
Rate this:
Please Sign up or sign in to vote.
Several features from Traceur which are supposed to be promising in the future of programming language design

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 or Haskell. I think they are all very useful and essential for skilled programmers and will ease the burden of more and more complicated programming tasks.

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-Share Alike 3.0 Unported License

Share

About the Author

stfairy
Student Shanghai Jiao Tong University
China China
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160919.1 | Last Updated 13 Aug 2011
Article Copyright 2011 by stfairy
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid