Functions argument

Or even better, use it as an actual full-fledged class, w/ constructors, fields & methods: :triumph:

// https://Discourse.Processing.org/t/functions-argument/11010/5
// GoToLoop (v1.1) (2019/May/09)

final Pair pair = new Pair(5, -10);

void setup() {
  println(pair);             // [ 5, -10 ]
  println(pair.calc('y'));   // [ -50, 540 ]
  println(pair.set(5, -10)); // [ 5, -10 ]
  println(pair.calc('u'));   // [ 3, -2 ]
  println(pair.clone());     // [ 3, -2 ]
  exit();
}

static final class Pair implements Cloneable {
  int a, b;

  Pair() {
  }

  Pair(final int a, final int b) {
    set(a, b);
  }

  Pair set(final int aa, final int bb) {
    a = aa;
    b = bb;
    return this;
  }

  Pair calc(final char ch) {
    if (ch == 'y') {
      a *= b;
      b = (a + 5) * (b - 2);
    } else {
      b /= a;
      a += b;
    }
    return this;
  }

  @Override Pair clone() {
    try {
      return (Pair) super.clone();
    }
    catch (final CloneNotSupportedException e) {
      throw new RuntimeException(e);
    }
  }

  @Override String toString() {
    return "[ " + a + ", " + b + " ]";
  }
}
4 Likes