From 6a6eab8db54381db93c80237083e39646f29abb7 Mon Sep 17 00:00:00 2001 From: ValKmjolnir Date: Sun, 11 Sep 2022 15:10:30 +0800 Subject: [PATCH] :zap: change parameter list of --- stl/sort.nas | 10 +++++++--- test/coroutine.nas | 15 +++++++++------ test/quick_sort.nas | 27 ++++++--------------------- test/word_collector.nas | 4 ++-- 4 files changed, 24 insertions(+), 32 deletions(-) diff --git a/stl/sort.nas b/stl/sort.nas index 2b04e47..cb2cf34 100644 --- a/stl/sort.nas +++ b/stl/sort.nas @@ -5,7 +5,7 @@ # only using < or > may cause infinite loop or the program may crash var sort=func(){ srand(); # be aware! this causes global changes - return func(vec,left,right,cmp=func(a,b){return a<=b;}){ + var quick_sort_core=func(vec,left,right,cmp){ if(left>=right) return nil; var base=left+int(rand()*(right-left)); (vec[left],vec[base])=(vec[base],vec[left]); @@ -19,8 +19,12 @@ var sort=func(){ vec[j]=vec[i]; } vec[i]=tmp; - sort(vec,left,i-1,cmp); - sort(vec,i+1,right,cmp); + quick_sort_core(vec,left,i-1,cmp); + quick_sort_core(vec,i+1,right,cmp); + return nil; + } + return func(vec,cmp=func(a,b){return a<=b;}){ + quick_sort_core(vec,0,size(vec)-1,cmp); return nil; } }(); \ No newline at end of file diff --git a/test/coroutine.nas b/test/coroutine.nas index 52baa1f..0c4b07d 100644 --- a/test/coroutine.nas +++ b/test/coroutine.nas @@ -12,11 +12,11 @@ var fib=func(){ } return; } -# different coroutines don't share the same local scope var co=[coroutine.create(fib),coroutine.create(fib)]; for(var i=0;i<45;i+=1){ var res=[coroutine.resume(co[0]),coroutine.resume(co[1])]; - println('co[0]: ',res[0]==nil?nil:res[0][0],'\nco[1]: ',res[1]==nil?nil:res[1][0]); + if(res[0]==nil or res[1]==nil or res[0][0]!=res[1][0]) + die("different coroutines don't share the same local scope"); } # test if coroutine can get upvalues @@ -28,8 +28,11 @@ func(){ x+=1; } }); - for(var i=0;i<16;i+=1) - println(coroutine.resume(co)); + for(var i=0;i<16;i+=1){ + var res=coroutine.resume(co); + if(res==nil or res[0]!=x or res[1]!=i) + die("coroutine should have the ability to get upvalues"); + } }(); # pressure test @@ -37,7 +40,7 @@ var productor=func(){ for(var i=0;;i+=1) coroutine.yield(i); } -var total=4000; # ms +var total=1000; # ms var co=coroutine.create(productor); var tm=maketimestamp(); @@ -48,7 +51,7 @@ var consumer=func(){ for(var i=0;i<5;i+=1) coroutine.resume(co); var rate=(tm.elapsedMSec()+1)/total; - print(bar.bar(rate)," ",rate*100,"% \r"); + print(bar.bar(rate)," ",rate*100,"% \r"); } tm.stamp(); diff --git a/test/quick_sort.nas b/test/quick_sort.nas index 7f21791..2dcc2dc 100644 --- a/test/quick_sort.nas +++ b/test/quick_sort.nas @@ -1,22 +1,7 @@ -var sort=func(vec,left,right){ - if(left>=right) return; - var (L,R,tmp)=(left,right,vec[left]); - while(left=vec[left]) - left+=1; - if(left!=right) - (vec[left],vec[right])=(vec[right],vec[left]); - } - (vec[L],vec[left])=(vec[left],tmp); - sort(vec,L,left-1); - sort(vec,left+1,R); - return; -} -var vec=[]; -rand(time(0)); -for(var i=0;i<1e4;i+=1) - append(vec,int(rand()*1e5)); -sort(vec,0,size(vec)-1); +import.stl.sort; +var vec=[]; +rand(time(0)); +for(var i=0;i<1e4;i+=1) + append(vec,int(rand()*1e5)); +sort(vec); println(vec); \ No newline at end of file diff --git a/test/word_collector.nas b/test/word_collector.nas index 4e63552..95fadcb 100644 --- a/test/word_collector.nas +++ b/test/word_collector.nas @@ -15,7 +15,7 @@ foreach(var f;argv){ } if(!all_exists){ - exit(-1);; + exit(-1); } var to_lower=func(c){ @@ -45,5 +45,5 @@ for(var i=0;i