This project has moved. For the latest updates, please go here.
An anonymous function with a default return value could be used as lambda expression in ReoScript.

Define anonymous function by using lambda expression

    var plus = (a, b) => a + b;

It is same as:
    function plus(a, b) {
      return a + b;
    }

To use the anonymous function:
    var result = plus(1, 2);

The result is:
    3

Array extension with lambda expression support

Here is a function named 'where' which finds matched elements from an array by given predicate lambda expression.
    Array.prototype.where = predicate => {
      var result = [];
        
      for(var element in this)
        if(predicate(element))
          result.push(element);

      return result;
    };

Use it to find the elements what is greater than 10:
    var arr = [1, 2, 5, 7, 8, 10, 12, 15, 17, 18, 19];
    var result = arr.where(n => n > 10);

The result is:
    [12, 15, 17, 18, 19]

If elements in array is object type, a selector lambda could be defined and used to decide which property from the object should be used to iterate. e.g.:
    Array.prototype.sum = selector => {
      var total = 0;
        
      for (element in this) {
        total += (selector == null ? element : selector(element));
      }

      return total;
    };

Use lambda expression to pass the selector:
    function Sales(year, amount) {
      this.year = year;
      this.amount = amount;
    }
      
    var arr = [ new Sales(2005, 130),
                new Sales(2006, 100),
                new Sales(2007, 210),
                new Sales(2008, 190), ];

    var result = arr.sum(obj => obj.amount);

The result is:
    630

Standard Array Extension

There is a set of prototype extension library called Standard Array Extension Library available since ReoScript v1.2. It is located in the source code as below:
    scripts/array.rs

Last edited Jan 14, 2015 at 6:51 AM by unvell, version 2