It is known that a function pointer can be passed to a function in C and C++, whereas, we must declare the structure of a function using interfaces for doing same job in Java.
In Fuzuli, a Fuzuli source code can be directly passed to a function. This feature allows us to create generic functions easly. Let's show it using an example.
The code below creates four expressions that sum, subtract, product and divide two numbers, respectively.
(let expr1 (expression (+ a b))) (let expr2 (expression (- a b))) (let expr3 (expression (* a b))) (let expr4 (expression (/ a b)))
The expression directive defines a runnable code using the directive eval as we will see later. Let's define a generic function that changes its behaviour respect to a expression parameter:
(function generic_function (params e x y) (let a x) (let b y) (return (eval e)) )
The function generic_function takes three parameters. The first one defines the real action. x and y are parameters that will be passed to expression later. Let's call this generic function using previously defined expressions:
(let enter "\n") (let x1 15)(let x2 5) (print "x1=" x1 ", x2=" x2 enter) (print "+ : " (generic_function expr1 x1 x2) enter) (print "- : " (generic_function expr2 x1 x2) enter) (print "* : " (generic_function expr3 x1 x2) enter) (print "/ : " (generic_function expr4 x1 x2) enter)
In first line we define the enter variable for printing output with line feed. In second line, we set x1 to 15 and x2 to 5. In third line, we are reporting the values of these variables.
The whole story lies at last four lines. In line four, we are calling the function generic_function using the predefined summation expression. In the next line, the same function is called using a different expression which calculates x1 - x2 . As it is clear to see that, last two lines calls the same generic function using two different expressions for getting the product and division of two numbers, respectively.
The output is :
+ : 20.0
- : 10.0
* : 75.0
/ : 3.0
You can try this code using the online interpreter: http://fuzuliproject.org/index.php?node=tryonline
or you can download the JFuzuli Editor: http://mhsatman.com/fuzuli-programming-language-facebook-face/