MVEL – Running a script against the MVEL interpreter

// Script: functional.mvel

// test using functional’ish aspect of MVEL

// expression based lang

 

System.out.println(“Hello!”);

 

// define a method

hello = def () {

    System.out.println(“Hello!”);

};

hello();

 

// define a couple of methods with the same arity

adder = def (arg1, arg2 ) {

    arg1 + arg2;

};

muliplier = def (arg1, arg2 ) {

    arg1 * arg2;

};

 

System.out.println(adder(1, 2));

System.out.println(muliplier(1, 2));

 

// function that takes a function and 2 parameters

// just like Scala’s approach

func2 = def (f2, arg1, arg2) {

    f2(arg1, arg2);

};

 

// pass the 2 functions as variables

var a = 1;

var b = 2;

var c = func2(adder, a, b);

System.out.println(c);

 

c = func2(muliplier, a, b);

System.out.println(c);

 

$ java -jar mvel2-2.2.8.Final.jar functional.mvel

Hello!

Hello!

3

2

3

2