C++ foldl

Run Settings
LanguageC++
Language Version
Run Command
#include <iostream> #include <assert.h> using namespace std; template<typename T> T foldl(T *array, size_t length, T (*f)(T, T)) { assert(length != 0); T result = array[0]; for(size_t i = 1; i < length; ++i) { result = f(result, array[i]); } return result; } template<typename T> T sub(T a, T b) { return a - b; } template<typename T> T add(T a, T b) { return a + b; } int main() { int arr[] = {5, 3, 1}; cout << foldl(arr, 3, &sub) << endl; float arrf[] = {5.1, 3.2, 1.4}; cout << foldl(arrf, 3, &sub) << endl; cout << foldl(arr, 3, &add) << endl; cout << foldl(arrf, 3, &add) << endl; return 0; }
Editor Settings
Theme
Key bindings
Full width
Lines