Last active
October 21, 2023 13:14
-
-
Save OldMetalmind/9d5df10fb4600963521c87f1b8d2e446 to your computer and use it in GitHub Desktop.
Dynamic and Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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