# Function as argument of another function

**URL:** <https://discourse.processing.org/t/function-as-argument-of-another-function/6383>\
**Category:** Coding Questions\
**Created:** [December 6, 2018, 9:05pm UTC](https://discourse.processing.org/t/function-as-argument-of-another-function/6383 "2018-12-06T21:05:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [December 6, 2018, 9:05pm UTC](https://discourse.processing.org/t/function-as-argument-of-another-function/6383/1 "2018-12-06T21:05:55Z")

</div>

Hi everyone,

I’m trying to pass a function as an argument of another function but can’t figure out how…

I’ve found plenty of infos on solution for Java 8 but sadly we can’t use those solution with processing if I get it correctly.

Then I’ve found infos on the callable interface but not enough to be able to implement it.

Is there someone that could give me a hand or point me to a decent link?

Here a small example of what I would like to do:

```auto
void setup() {
  MyVector vec = new MyVector(5, 12);
  vec.applyFunction(myFunction); // Vec will now be [10, 24]
}

float myFunction(float val) {
  return 2 * val;
}

class MyVector() {
  float[] values; 
  
  MyVector(float a, float b) {
    values = new float[2];
    values[0] = a;
    values[1] = b;
  }
  
  void applyFunction(function f) {
    values[0] = f(values[0]);
    values[1] = f(values[1]);
  }
}

```

---

<div class="post-metadata">

**Author:** ![Architector\_4](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/architector_4/32/813_2.png) [@Architector\_4](https://discourse.processing.org/u/Architector_4)\
**Post date:** [December 6, 2018, 9:22pm UTC](https://discourse.processing.org/t/function-as-argument-of-another-function/6383/2 "2018-12-06T21:22:12Z")

</div>

> [@jb4x](#):
>
> …I’ve found plenty of infos on solution for Java 8…

What you are looking for, functions that are variables, are called lambda expressions. The problem is that they were introduced only in Java 8, and, as far as I know, Processing uses older versions of Java, so you are out of luck with this idea.

Maybe there’s some hacky cool way to implement it - maybe you could just make up new classes for every function, and then juggle them around in a weird way to do hacky things, and each one of these classes has a `f(float a, float b)` type function, but I can’t think of such, at least not right now, and I don’t even know if it’s possible even that way.

Edit: here’s even complete real code of your example:

```auto

void setup(){
  MyVector vec = new MyVector(5, 12);
  vec.applyFunction(myFunction);
}

interface MyCoolInterface{
  float operation(float a,float b);
}

MyCoolInterface myFunction = (a,b) -> a*b;

class MyVector {
  float[] values; 
  
  MyVector(float a, float b) {
    values = new float[2];
    values[0] = a;
    values[1] = b;
  }
  
  void applyFunction(MyCoolInterface f) {
    values[0] = f.operation(values[0],2);
    values[1] = f.operation(values[1],2);
  }
}

```

If you paste it into Processing, however, it would show no errors, except a message talking about Java 8 pointing to the definition of `myFunction`, which is this:  
`Lambda expressions are only allowed at source level 1.8 or above`

If you are interested, here’s a long thread on older forum with people talking about Processing switching to Java 8: [When will Processing IDE shift to Java 8 - Processing 2.x and 3.x Forum](https://forum.processing.org/two/discussion/comment/123676/)

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [December 6, 2018, 10:04pm UTC](https://discourse.processing.org/t/function-as-argument-of-another-function/6383/3 "2018-12-06T22:04:54Z")

</div>

> [@Architector\_4](#):
>
> Maybe there’s some hacky cool way to implement it

It’s not really hacky. Anonymous classes were added in Java 1.1 precisely to support a form of functional programming in exactly this way.

```auto
MyCoolInterface myFunction = new MyCoolInterface() {
  public float operation(float a,float b) {
    return a * b;
  }
};

```

Or you can just use that as a method argument rather than assigning to a variable. All a bit clunky though. Lambdas were added as a cleaner and better performing alternative.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 6, 2018, 11:07pm UTC](https://discourse.processing.org/t/function-as-argument-of-another-function/6383/4 "2018-12-06T23:07:40Z")

</div>

> [@jb4x](#):
>
> I’m trying to pass a function as an argument of another function but can’t figure out how…

I’ve wet my feet a little on the Java package [java.util.function](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/function/package-summary.html) by instantiating a [[u]Predicate[/u]](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/function/Predicate.html) and passing it as an argument at these 2 old sketch links below: 🤪

1. [Querying ArrayList - Processing 2.x and 3.x Forum](http://Forum.Processing.org/two/discussion/21761/querying-arraylist#Item_2)
2. [Forum.Processing.org/two/discussion/22823/how-to-implement-this-method#Item\_2](http://Forum.Processing.org/two/discussion/22823/how-to-implement-this-method#Item_2)

Of course it’s not even close to an actual lambda `->` syntax. But it’s as far as we can go under the PDE. 🙄

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [December 7, 2018, 7:51pm UTC](https://discourse.processing.org/t/function-as-argument-of-another-function/6383/5 "2018-12-07T19:51:21Z")

</div>

Thanks everyone, the following is working great for me:

```auto
interface Function {
  float applyFunction(float a);
}

Function myFunction = new Function() {
  public float applyFunction(float a) {
    return 2 * a;
  }
};

void setup() {
  MyVector vec = new MyVector(5, 12);
  vec.display();
  vec.applyFunction(myFunction);
  vec.display();
}

class MyVector {
  float[] values; 

  MyVector(float a, float b) {
    values = new float[2];
    values[0] = a;
    values[1] = b;
  }

  void applyFunction(Function f) {
    values[0] = f.applyFunction(values[0]);
    values[1] = f.applyFunction(values[1]);
  }

  void display() {
    println(values[0], values[1]);
  }
}

```
