Skip to content

Instantly share code, notes, and snippets.

@rikonor
Created December 17, 2020 19:27
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 rikonor/98c5879efb5fcc38439ed46d8c0f6cdf to your computer and use it in GitHub Desktop.
Save rikonor/98c5879efb5fcc38439ed46d8c0f6cdf to your computer and use it in GitHub Desktop.
void main() {
var d = StubDoer(() {
print("Also doing...");
});
d = wrapWithTimes(100)(d);
d = wrapWithMetrics()(d);
d = wrapWithTracing()(d);
doStuff(d);
}
typedef WrapFunc<T> = T Function(T);
abstract class Doer {
void Do();
}
typedef DoFn = Function();
class StubDoer extends Doer {
DoFn doFn;
StubDoer(this.doFn);
@override
void Do() {
doFn();
}
}
WrapFunc<Doer> wrapWithTimes(int n) {
return (Doer d) {
return StubDoer(() {
for (int i = 0; i < n; i++) {
d.Do();
}
});
};
}
WrapFunc<Doer> wrapWithMetrics() {
return (Doer d) {
return StubDoer(() {
var startTime = DateTime.now();
d.Do();
var durMs = DateTime.now().difference(startTime).inMicroseconds;
print('Took $durMs us');
});
};
}
WrapFunc<Doer> wrapWithTracing() {
return (Doer d) {
return StubDoer(() {
d.Do();
});
};
}
void doStuff(Doer d) {
d.Do();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment