Wikia

Planet JFX

Functional Programming

118pages on
this wiki
Talk0

This is a simple example about using functions as parameters.

function reduce(f: function(Number,Number):Number, list: Number[], nilArg: Number):Number {
    if( list == [] ) then
        nilArg
    else
        f( list[0], reduce(f, list[x|indexof x > 0], nilArg))
}

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

function product( x: Number, y: Number ): Number {x * y }

def list : Number[] = [1,2,3,4];

function p( s: String ) { println( s ); }

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

Around Wikia's network

Random Wiki