Skip to content

Instantly share code, notes, and snippets.

@OldMetalmind
Last active October 21, 2023 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OldMetalmind/9d5df10fb4600963521c87f1b8d2e446 to your computer and use it in GitHub Desktop.
Save OldMetalmind/9d5df10fb4600963521c87f1b8d2e446 to your computer and use it in GitHub Desktop.
Dynamic and Function
dynamic applyA(dynamic x) {
if (x is Function) {
return (dynamic f) {
return (dynamic y) {
return f(x(y));
};
};
} else {
return (dynamic f) {
return f(x);
};
}
}
dynamic applyB(dynamic x) {
return (dynamic f) {
return (dynamic y) {
if (x is Function) {
return f(x(y));
} else {
return f(x);
}
};
};
}
void main() {
const one = 1;
addOne(int x) => x + 1;
addTwo(int x) => x + 2;
final resultA = applyA(one)(addOne);
final resultB = applyB(one)(addOne);
final resultC = applyA(addOne)(addTwo)(one);
final resultD = applyB(addOne)(addTwo)(one);
print('A=$resultA\nB=$resultB\nC=$resultC\nD=$resultD');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment