Recent changes Random page
GAMING
Technology
 
Gaming
Entertainment
Science Fiction
Biggest wikis
Hobbies
Music
See more...

Functional Programming

From Planet JFX

Jump to: navigation, search
 

This is a simple example about using functions as parameters.

import java.lang.System;

function reduce(f: function(a:*,b:*), list: **, nilArg: *) {
  return if( list == [] ) then 
      nilArg 
    else 
      f( list[indexof . == 0], 
         reduce(f, list[indexof .> 0], nilArg)); 
} 

function sum( x: Number, y: Number ): Number { 
  return x+y;
}

function product( x: Number, y: Number ): Number {
  return x*y;
}
var list = [1,2,3,4];

operation p( s: String ) { System.out.println( s ); } 

p("# sum     : {reduce(sum,list,0)}");
p("# product : {reduce(product,list,1)}");

Rate this article:
Share this article: