Do any programming languages have elegant solutions for the following usage?
#include <iostream>
#include <string>
#include <vector>
int increment(int v) {
return v + 1;
}
int decrement(int v) {
return v - 1;
}
using func_call = int(*)(int);
void pretty_print(std::string ornament, func_call fn) {
std::cout << ornament << ' ' << fn << '\n';
}
int main()
{
std::vector<int> v {1, 2, 3, 4};
// pseudo-code
v.map(pretty_print("incr", increment(/*pass v elements here*/)));
v.map(pretty_print("decr", decrement(/*pass v elements here*/)));
// or
v.map.increment().pretty_print("incr");
v.map.decrement().pretty_print("decr");
}
https://wandbox.org/permlink/7XHWnjazvQWNDAIs
The idea is to be able to run contents of a container through some mutation and then print the result with some ornamentation.
Esthetically, I prefer this declarative/pipe style over raw loops, but I feel like C-like languages don't have a first-class support for that. Am I mistaken? Any languages that encourage you to write code in this way?