Skip to content

Instantly share code, notes, and snippets.

@lukaszklekot lukaszklekot/main.c
Created Nov 23, 2018

Embed
What would you like to do?
A crude C version of the "Composing state transformations from a list of simple functions" pattern; https://twitter.com/hukl/status/1065554266981625856
#include <stdio.h>
double add_ten(double x) {
return x + 10;
}
double multiply_ten(double x) {
return x * 10;
}
double substract_five(double x) {
return x - 5;
}
typedef double (*transformFunctionPtr)(double);
double transform(double state, transformFunctionPtr fs[], int fsSize) {
for (int i = 0; i < fsSize; ++i) {
state = fs[i](state);
}
return state;
}
double state_transform_a(double state) {
transformFunctionPtr transformations[3] = {&add_ten, &multiply_ten, &substract_five};
return transform(state, transformations, 3);
}
int main(int argc, const char * argv[]) {
double new_state = state_transform_a(23);
printf("%f\n", new_state);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.