Created
December 17, 2020 19:27
-
-
Save rikonor/98c5879efb5fcc38439ed46d8c0f6cdf to your computer and use it in GitHub Desktop.
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
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