Skip to content

Instantly share code, notes, and snippets.

@crazymonkyyy
Created September 23, 2022 17:22
Show Gist options
  • Save crazymonkyyy/4ee39e5256a905e2fad323d73c7ca9d9 to your computer and use it in GitHub Desktop.
Save crazymonkyyy/4ee39e5256a905e2fad323d73c7ca9d9 to your computer and use it in GitHub Desktop.
import std.datetime;
import core.thread;
import std.stdio;
auto now()=>Clock.currTime;
auto sleep(int i)=>Thread.sleep( dur!("seconds")(i));
alias time=typeof(now());
unittest{
auto a=now;
sleep(1);
time b=now;
(a-b).writeln;
}
struct timed(T,Duration dur){
struct pair{
T a;
time t;
}
pair[] arr;
auto opOpAssign(string s:"~")(T a) => arr~=pair(a,now);
auto get(){
import std.algorithm;
auto F(pair p)=>now-p.t<dur;
auto i=arr.countUntil!F;
arr=arr[i==-1?$:i..$];
return arr.map!"a.a";
}
}
unittest{
timed!(int,dur!"seconds"(3)) foo;
foreach(i;0..5){
foo~=i;
sleep(1);
foo.get.writeln;
}
sleep(5);
foo.get.writeln;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment