fbpx

C++: First-Class Functions

Bjarne-stroustrup
 

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

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.)

#include <functional>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>

using std::cout;
using std::endl;
using std::vector;
using std::function;
using std::transform;
using std::back_inserter;

typedef function<double(double)> FunType;

vector<FunType> A = {sin, cos, tan, [](double x) { return x*x*x; } };
vector<FunType> B = {asin, acos, atan, [](double x) { return exp(log(x)/3); } };

template <typename A, typename B, typename C>
function<C(A)> compose(function<C(B)> f, function<B(A)> g) {
	return [f,g](A x) { return f(g(x)); };
}

int main() {
	vector<FunType> composedFuns;
	auto exNums = {0.0, 0.2, 0.4, 0.6, 0.8, 1.0};

	transform(B.begin(), B.end(),
	A.begin(),
	back_inserter(composedFuns),
	compose<double, double, double>);

	for (auto num: exNums)
	for (auto fun: composedFuns)
	cout << u8"f\u207B\u00B9.f(" << num << ") = " << fun(num) << endl;

	return 0;
}

SOURCE

Content is available under GNU Free Documentation License 1.2.