KoboldAI-Client/extern/lualibs/pl/seq.lua

7 lines
6.3 KiB
Lua

-- Penlight 1.11.0-1 | /lua/pl/seq.lua | https://github.com/lunarmodules/Penlight | License: MIT | Minified using https://www.npmjs.com/package/luamin/v/1.0.4
-- Copyright (C) 2009-2016 Steve Donovan, David Manura.
-- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
local next,assert,pairs,tonumber,type,setmetatable=next,assert,pairs,tonumber,type,setmetatable;local a,b=string.find,string.format;local c=math.random;local d,e=table.sort,table.insert;local io=io;local f=require'pl.utils'local g=require'pl.types'.is_callable;local h=f.function_arg;local i=f.assert_arg;local j=require'debug'local k={}function k.greater_than(l)return function(m)return tonumber(m)>l end end;function k.less_than(l)return function(m)return tonumber(m)<l end end;function k.equal_to(l)if type(l)=="number"then return function(m)return tonumber(m)==l end else return function(m)return m==l end end end;function k.matching(n)return function(m)return a(m,n)end end;local o;function k.list(p)i(1,p,'table')if not o then o=ipairs{}end;local q,r=0;return function()q,r=o(p,q)return r end end;function k.keys(p)i(1,p,'table')local q;return function()q=next(p,q)return q end end;local s=k.list;local function t(u)if type(u)=='table'then return s(u)else return u end end;k.iter=t;function k.range(v,w)local x=v-1;return function()x=x+1;if x>w then return nil else return x end end end;function k.count(u,y,z)local x=0;k.foreach(u,function(A)if y(A,z)then x=x+1 end end)return x end;function k.minmax(u)local B,C=1e70,-1e70;for m in t(u)do m=tonumber(m)if m<B then B=m end;if m>C then C=m end end;return B,C end;function k.sum(u,D)local n=0;local x=0;for m in t(u)do if D then m=D(m)end;n=n+m;x=x+1 end;return n,x end;function k.copy(u)local E,F={},1;for m in t(u)do E[F]=m;F=F+1 end;setmetatable(E,require('pl.List'))return E end;function k.copy2(u,G,H)local E,F={},1;for I,J in u,G,H do E[F]={I,J}F=F+1 end;return E end;function k.copy_tuples(u)u=t(u)local E={}local K={u()}while#K>0 do e(E,K)K={u()}end;return E end;function k.random(L,M,N)local O;assert(type(L)=='number')if N then O=function()return c(M,N)end elseif M then O=function()return c(M)end else O=c end;return function()if L==0 then return nil else L=L-1;return O()end end end;function k.sort(u,P)local p=k.copy(u)d(p,P)return s(p)end;function k.zip(Q,R)Q=t(Q)R=t(R)return function()return Q(),R()end end;function k.count_map(u)local p={}local m;for n in t(u)do m=p[n]if m then p[n]=m+1 else p[n]=1 end end;return setmetatable(p,require('pl.Map'))end;function k.unique(u,S)local p=k.count_map(u)local E,F={},1;for q in pairs(p)do E[F]=q;F=F+1 end;table.sort(E)if S then return E else return s(E)end end;function k.printall(u,T,U,V)local W=io.write;if not T then T=' 'end;if not U then if T=='\n'then U=1e30 else U=7 end end;if V then local X=V;V=function(m)return b(X,m)end end;local F=1;for m in t(u)do if V then m=V(m)end;if F<U then W(m,T)F=F+1 else W(m,'\n')F=1 end end;W'\n'end;function k.splice(Q,R)Q=t(Q)R=t(R)local u=Q;return function()local Y=u()if Y==nil then if u==Q then u=R;return u()else return nil end else return Y end end end;function k.map(D,u,z)D=h(1,D)u=t(u)return function()local I,J=u()if I==nil then return nil end;return D(I,z or J)or false end end;function k.filter(u,Z,z)Z=h(2,Z)return function()local I,J;while true do I,J=u()if I==nil then return nil end;if Z(I,z or J)then return I,J end end end end;function k.reduce(D,u,_)D=h(1,D)u=t(u)local A=_ or u()if A==nil then return nil end;for m in u do A=D(A,m)end;return A end;function k.take(u,L)u=t(u)return function()if L<1 then return end;local a0,a1=u()if not a0 then return end;L=L-1;return a0,a1 end end;function k.skip(u,L)L=L or 1;for x=1,L do if u()==nil then return s{}end end;return u end;function k.enum(u)local x=0;u=t(u)return function()local a0,a1=u()if not a0 then return end;x=x+1;return x,a0,a1 end end;function k.mapmethod(u,a2,a3,a4)u=t(u)return function()local A=u()if not A then return end;local D=A[a2]if not D then error(type(A).." does not have method "..a2)end;return D(A,a3,a4)end end;function k.last(u)u=t(u)local A,M=u(),nil;if A==nil then return s{}end;return function()A,M=u(),A;if A==nil then return nil end;return A,M end end;function k.foreach(u,D)D=h(2,D)for G,H,a5 in t(u)do D(G,H,a5)end end;local a6;local function a7(u,...)if g(u)then return setmetatable({iter=u},a6)else return u,...end end;local a8,a9,aa=k.map,k.reduce,k.mapmethod;local ab={map=function(self,ac,z)return a8(ac,self,z)end,reduce=function(self,ac,_)return a9(ac,self,_)end}a6={__index=function(ad,q)local D=ab[q]or k[q]if D then return function(ae,...)return a7(D(ae.iter,...))end else return function(ae,...)return a7(aa(ae.iter,q,...))end end end,__call=function(ae)return ae.iter()end}setmetatable(k,{__call=function(ad,u,af)if not g(u)then if type(u)=='table'then u=k.list(u)else return u end end;if af then return setmetatable({iter=function()return u(af)end},a6)else return setmetatable({iter=u},a6)end end})function k.lines(ag,...)local u,ah;if ag=='STDIN'then ag=io.stdin elseif type(ag)=='string'then u,ah=io.lines(ag,...)elseif not ag.read then error("Pass either a string or a file-like object",2)end;if not u then u,ah=ag:lines(...)end;if ah then local ai,aj=u,ah;u=function()return ai(aj)end end;return a7(u)end;function k.import()j.setmetatable(function()end,{__index=function(ad,q)local n=ab[q]or k[q]if n then return n else return function(n,...)return k.mapmethod(n,q,...)end end end})end;return k