Code Examples
A repository of 155 code examples for BeepBeep
functions.FunctionUsage Class Reference

Show the basic usage of Function objects. More...

Classes

class  IntegerDivision
 A function that computes integer division.
 

Static Public Member Functions

static void main (String[] args)
 

Detailed Description

Show the basic usage of Function objects.

Sylvain Hallé Easy

Definition at line 35 of file FunctionUsage.java.

Member Function Documentation

◆ main()

static void functions.FunctionUsage.main ( String []  args)
static

We create an instance of the negation function. The class

Definition at line 37 of file FunctionUsage.java.

38  {
39  /// We create an instance of the negation function. The class
40  // Booleans contains a static reference to an instance of that function
41  Function negation = Booleans.not;
42 
43  // To hold the return value of a function, we must give it an array
44  Object[] out = new Object[1];
45 
46  // Let us evaluate the negation of a Boolean value. This is done
47  // by calling evaluate on the function object.
48  negation.evaluate(new Object[]{true}, out);
49 
50  // We print the return value of the function
51  System.out.println("The return value of the function is: " + out[0]);
52  ///
53 
54  //* Let us now try the same thing with a function that takes
55  // two arguments.
56  Function addition = Numbers.addition;
57 
58  // Since addition takes two arguments, we must pass it an array of
59  // two objects
60  addition.evaluate(new Object[]{2, 3}, out);
61 
62  // We print the return value of the function
63  System.out.println("The return value of the function is: " + out[0]);
64  //*
65 
66  //# Finally, a function does not necessarily have an output arity of 1.
67  // In this example, we call a function that has both an input and an
68  // output arity of 2. From two numbers x and y, it outputs the
69  // quotient and the remainder of the division of x by y.
70  Function int_division = IntegerDivision.instance;
71  Object[] outs = new Object[2];
72  int_division.evaluate(new Object[]{14, 3}, outs);
73  System.out.println("14 divided by 3 equals " +
74  outs[0] + " remainder " + outs[1]);
75  //#
76  }

The documentation for this class was generated from the following file: