Skip to content

Latest commit

 

History

History
 
 

Rhino

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Rhino

To use linq with Rhino it is necessary to modify the source code of linq.js to the conditions of Rhino:

  • Rhino does not support variable declaration with let and const, therefore these must be replaced by var.
  • Rhino does not support export declaration, therefore the line export default Enumerable; at the end must be deleted.
  • Rhino has no console window, therefore it is recommended to replace the code sequences if (typeof console !== Types.Undefined) { console.log(...); } with java.lang.System.out.println(...).

You can find a suitably modified version of linq.js as linq.class.js here. With the load command you can use it inside Rhino JavaScript.

A very detailed example is available here, this shows how to use it.

Hints

// Begin----------------------------------------------------------------

load("linq.class.js");
var Enumerable = LINQ();

// Arrow function expression aka arrow function
var resultArrow = Enumerable.range(1, 10)
  .where((i) => i % 3 == 0)
  .select((i) => i * 10);

java.lang.System.out.println(
  JSON.stringify(resultArrow.toArray())
); // [30,60,90]

// Function expression aka regular function
var resultFunction = Enumerable.range(1, 10)
  .where( function(i) { return i % 3 == 0 } )
  .select( function(i) { return i * 10 } );

java.lang.System.out.println(
  JSON.stringify(resultFunction.toArray())
); // [30,60,90]

// End------------------------------------------------------------------
  • linq supports only lambda syntax (dot notation) and not query expression.
// Begin----------------------------------------------------------------

load("linq.class.js");
var Enumerable = LINQ();

var range = Enumerable.range(1, 10);

// Lambda syntax
var result = range
  .where( function(i) { return i % 3 == 0 } )
  .select( function(i) { return i * 10 } );

/* Query expression is not supported
var result = from i in range where i % 3 == 0 select i * 10;
 */

java.lang.System.out.println(
  JSON.stringify(result.toArray())
); // [30,60,90]

// End------------------------------------------------------------------