Groovy Closure: Thoughts on the evolution

Each one of the following constructions has its own characteristics, with benefits and limitations. However, it is hard not to notice certain similarities about the concepts or the implementation mechanisms between them.

C – function_pointer.c

#include

/* gcc --ansi function_pointer.c -o function_pointer ; ./function_pointer */
int add(int a, int b);
int multiply(int a, int b);

typedef int (*math_operation)(int, int);

int main(int argc, char *argv[]) {
  math_operation ops[2] = {add, multiply};
  int i = 0;
  int result = -1;

  for (i = 0; i < 2; i++) {
    result = ops[i](4, 7);
    printf("%i\n", result);
  }

  return 0;
}

int add(int a, int b) {
  return a+b;
}

int multiply(int a, int b) {
  return a*b;
}

C++ – virtual_function.cpp

#include <stdio.h>

/* g++ --ansi virtual_function.cpp -o virtual_function ; ./virtual_function */
class MathOperation {
public:
  virtual int exec(int a, int b) = 0;
};


class Add: public MathOperation {
public:
  int exec(int a, int b) {
    return a+b;
  }
};


class Multiply: public MathOperation {
public:
  int exec(int a, int b) {
    return a*b;
  }
};


int main(int argc, char *argv[]) {
  MathOperation *ops[2] = {new Add(), new Multiply()};
  int i = 0;
  int result = -1;
  
  for (i = 0; i < 2; i++) {
    result = ops[i]->exec(4, 7);
    printf("%i\n", result);
    delete ops[i];
  }

  return 0;
}

Java – VirtualFunction1.java

/* javac VirtualFunction1.java; java -cp . VirtualFunction1 */
interface MathOperation {
    int exec(int a, int b);
}

class Add implements MathOperation {
    public int exec(int a, int b) {
        return a+b;
    }
}

class Multiply implements MathOperation {
    public int exec(int a, int b) {
        return a*b;
    }
}

public class VirtualFunction1 {
    public static void main(String [] args) {
        MathOperation [] ops = {new Add(), new Multiply()};
        int i = 0;
        int result = -1;
        
        for (i = 0; i < ops.length; i++) {
            result = ops[i].exec(4, 7);
            System.out.println("" + result);
        }
        
    }
}

Java – VirtualFunction2.java

/* javac VirtualFunction1.java VirtualFunction2.java; java -cp . VirtualFunction2 */

//interface MathOperation {
//    int exec(int a, int b);
//}

public class VirtualFunction2 {
    public static void main(String [] args) {
        MathOperation add = new MathOperation() {
                public int exec(int a, int b) {return a+b;}
            };
        MathOperation multiply = new MathOperation() {
                public int exec(int a, int b) {return a*b;}
            };
        
        MathOperation [] ops = {add, multiply};
        int result = -1;
        
        for (MathOperation op : ops) {
            result = op.exec(4, 7);
            System.out.println("" + result);
        }
        
    }
}

Groovy – closure.groovy

/* groovy closure.groovy */
/* groovyc closure.groovy; groovy -cp . closure */
def add = {a, b -> a+b}
def multiply = {a, b -> a*b}
def ops = [add, multiply]
int result = -1
for (op in ops) {
  result = op(4, 7)
  println ("" + result)
}

Download the source code.

Additional references:

Leave a Reply

Your email address will not be published. Required fields are marked *

*


eight × = 16

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>