user
A language has first-class functions if it can do each of the following without recursively invoking a compiler or interpreter or otherwise metaprogramming:

* Create new functions from preexisting functions at run-time
* Store functions in collections
* Use functions as arguments to other functions
* Use functions as return values of other functions

Task

Write a program to create an ordered collection *A* of functions of a real number. At least one function should be built-in and at least one should be user-defined; try using the sine, cosine, and cubing functions. Fill another collection *B* with the inverse of each function in *A*. Implement function composition as in Functional Composition. Finally, demonstrate that the result of applying the composition of each function in *A* and its inverse in *B* to a value, is the original value. (Within the limits of computational accuracy).

(A solution need not actually call the collections "A" and "B". These names are only used in the preceding paragraph for clarity.)

Related task

First-class Numbers
assistant
(formerly Perl 6)
Here we use the Z ("zipwith") metaoperator to zip the 𝐴 and 𝐵 lists with builtin composition operator, ∘ (or just o). The .() construct invokes the function contained in the $\_ (current topic) variable.

```
my \𝐴 = &sin,  &cos,  { $_ ** <3/1> }
my \𝐵 = &asin, &acos, { $_ ** <1/3> }

say .(.5) for 𝐴 Z∘ 𝐵

```

Output:

```
0.5
0.4999999999999999
0.5000000000000001

```

It is not clear why we don't get exactly 0.5, here.

Operators, both builtin and user-defined, are first class too.

```
my @a = 1,2,3;
my @op = &infix:<+>, &infix:<->, &infix:<*>;
for flat @a Z @op -> $v, &op { say 42.&op($v) }

```

Output:

```
43
40
126
```