Created
June 8, 2024 04:03
-
-
Save crazymonkyyy/bddfcb58e92a9897725a97a906e660eb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import std; | |
auto takeUntil(R)(R base,R other){ | |
static struct Result{ | |
R base; | |
R other; | |
alias base this; | |
bool empty()=>base==other; | |
} | |
return Result(base,other); | |
} | |
unittest{ | |
iota(5).takeUntil(iota(5).drop(3)).writeln; | |
} | |
template slideeach(alias cat,F...){ | |
void slideeach(R)(R r){ | |
struct sliceeach_{ | |
R active; | |
R store; | |
auto front(){ | |
int catagory=cat(store.front); | |
//assert(catagory<F.length);assert(catagory>=0); | |
while( ! active.empty && cat(active.front)==catagory){ | |
active.popFront; | |
} | |
lable: switch(catagory){ | |
static foreach(i;0..F.length){ | |
case i:F[i](store.takeUntil(active)); break lable; | |
} | |
default: assert(0); | |
} | |
//F[catagory](store.takeUntil(active)); | |
} | |
void popFront(){ | |
store=active; | |
} | |
bool empty()=>store.empty; | |
} | |
auto temp=sliceeach_(r,r); | |
while( ! temp.empty){ | |
temp.front(); | |
temp.popFront; | |
} | |
}} | |
unittest{ | |
int fizzbuzz(int i){ | |
if(i%15==0)return 0; | |
if(i%5==0)return 1; | |
if(i%3==0)return 2; | |
return 3; | |
} | |
template print(string s){ | |
void print(R)(R r)=>writeln(s,":",r); | |
} | |
[15,30,45,3,6,5,10,9,1,2,4,6].slideeach!( | |
fizzbuzz, | |
print!"fizzbuzz", | |
print!"buzz", | |
print!"fizz", | |
writeln); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment