# Arraylist full of functions?

**URL:** https://discourse.processing.org/t/arraylist-full-of-functions/10786
**Category:** Coding Questions
**Created:** [April 30, 2019, 4:14pm UTC](https://discourse.processing.org/t/arraylist-full-of-functions/10786 "2019-04-30T16:14:45Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![DerekCresswell](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/derekcresswell/32/4868_2.png) [@DerekCresswell](https://discourse.processing.org/u/DerekCresswell)
#### Post date: [April 30, 2019, 4:14pm UTC](https://discourse.processing.org/t/arraylist-full-of-functions/10786/1 "2019-04-30T16:14:45Z")

</div>

I need to store a list of functions that I can execute from said array.  
I also need to be able to pass them as variables.  
Lambda doesn’t seem to work in processing. How can I do this?  
Thanks!

---

<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: [April 30, 2019, 6:31pm UTC](https://discourse.processing.org/t/arraylist-full-of-functions/10786/2 "2019-04-30T18:31:31Z")

</div>

> [@Function as argument of another function](https://discourse.processing.org/t/function-as-argument-of-another-function/6383/4):
>
> 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 [Predicate](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: crazy_face[Forum.Processing.org/two/discussion/21761/querying-arraylist#Item\_2](http://Forum.Processing.org/two/discussion/21761/querying-arraylist#Item_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. roll_eyes

---

<div class="post-metadata">

### Author: ![humayung](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/humayung/32/6089_2.png) [@humayung](https://discourse.processing.org/u/humayung)
#### Post date: [April 30, 2019, 7:02pm UTC](https://discourse.processing.org/t/arraylist-full-of-functions/10786/3 "2019-04-30T19:02:47Z")

</div>

i dont know if you have to use processing, but if you really need lambda expression try using processing as java library from another ide

---

<div class="post-metadata">

### Author: ![clankill3r](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/clankill3r/32/1098_2.png) [@clankill3r](https://discourse.processing.org/u/clankill3r)
#### Post date: [April 30, 2019, 8:31pm UTC](https://discourse.processing.org/t/arraylist-full-of-functions/10786/4 "2019-04-30T20:31:33Z")

</div>

Can you list all input parameters and all return types.

---

<div class="post-metadata">

### Author: ![DerekCresswell](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/derekcresswell/32/4868_2.png) [@DerekCresswell](https://discourse.processing.org/u/DerekCresswell)
#### Post date: [May 1, 2019, 9:17pm UTC](https://discourse.processing.org/t/arraylist-full-of-functions/10786/5 "2019-05-01T21:17:13Z")

</div>

No inputs, all floats.

---

<div class="post-metadata">

### Author: ![monkstone](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/monkstone/32/64_2.png) [@monkstone](https://discourse.processing.org/u/monkstone)
#### Post date: [May 2, 2019, 1:01pm UTC](https://discourse.processing.org/t/arraylist-full-of-functions/10786/6 "2019-05-02T13:01:36Z")

</div>

[Sam Pottinger](https://github.com/sampottinger/processing) has done an amazing job in updating processing, not only to update to OpenJDK11, but also to support java 8 syntax in the processing ide. I’m sure he would welcome your support. Here is an example using lambda syntax in the processing ide.

 ![lambda](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/6/6329742f95785d58cc270b4c9b2d8888144a1dc6.png)

```java
interface MyFunction {
  float calculate(float x);
}

float[] values = new float[2];

void setup() {
  // lambda expression to define the calculate method
  MyFunction doubling = (float x)->x * 2;
  values[0] = doubling.calculate(5);
  values[1] = doubling.calculate(12);
  display();
}

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

```

The code can be even more elegant in [JRubyArt](https://ruby-processing.github.io/JRubyArt/)

```ruby
attr_reader :values

def setup
  # lambda expression to define the calculate method
  doubling = ->(x) { x * 2 }
  @values = [5.0, 12.0].map { |x| doubling.call(x) }
  display
end

def display
  puts values
end

```

---

<div class="post-metadata">

### Author: ![monkstone](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/monkstone/32/64_2.png) [@monkstone](https://discourse.processing.org/u/monkstone)
#### Post date: [May 2, 2019, 2:14pm UTC](https://discourse.processing.org/t/arraylist-full-of-functions/10786/7 "2019-05-02T14:14:22Z")

</div>

Or as GoToLoop has already posted for the faint hearted see [Function as argument of another function](https://discourse.processing.org/t/function-as-argument-of-another-function/6383/4), but the update to jdk8 syntax is somewhat overdue.
