-- Penlight 1.11.0-1 | /lua/pl/comprehension.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 a=require'pl.utils'local b,c=pcall(require,"pl.luabalanced")if not b then c=require'luabalanced'end;local d=math.max;local e=table.concat;local f={list={init=' {} ',accum=' __result[#__result+1] = (%s) '},table={init=' {} ',accum=' local __k, __v = %s __result[__k] = __v '},sum={init=' 0 ',accum=' __result = __result + (%s) '},min={init=' nil ',accum=' local __tmp = %s '..' if __result then if __tmp < __result then '..'__result = __tmp end else __result = __tmp end '},max={init=' nil ',accum=' local __tmp = %s '..' if __result then if __tmp > __result then '..'__result = __tmp end else __result = __tmp end '}}local function g(h)local i=1;local j;local k,l=h:match('^%s*([%a_][%w_]*)%s*%(()',i)local m=#h+1;if k then local n,o=c.match_bracketed(h,l-1)assert(n,'syntax error')if h:match('^%s*$',o)then j=k;m=o-1;i=l end end;j=j or"list"local p;p,i=c.match_explist(h,i)assert(p,"syntax error: missing expression list")p=e(p,', ')local q={}local r={}local s={}while 1 do local l=h:match('^%s*for%s+()',i)if not l then break end;i=l;local t;t,i=c.match_namelist(h,i)assert(#t>0,'syntax error: zero variables')for u,v in ipairs(t)do assert(not v:match'^__',"identifier "..v.." may not contain __ prefix")end;r[#r+1]=t;local w,l=h:match('^(=)%s*()',i)if not w then w,l=h:match('^(in)%s+()',i)end;if w then i=l;local x;x,i=c.match_explist(h,i)assert(#x>0,'syntax error: zero expressions')assert(w~='='or#x==2 or#x==3,'syntax error: numeric for requires 2 or three expressions')q[#r]=w;s[#r]=x else q[#r]=false;s[#r]=false end end;assert(#r>0,'syntax error: missing "for" clause')local y={}while 1 do local l=h:match('^%s*if%s+()',i)if not l then break end;i=l;local z;z,i=c.match_expression(h,i)assert(z,'syntax error: predicated expression not found')y[#y+1]=z end;local A=''c.gsub(h,function(B,C)if B=='e'then A=A..' '..C..' 'end end)local D=0;A:gsub('[%a_][%w_]*',function(E)local E=E:match('^_(%d+)$')if E then D=d(D,tonumber(E))end end)if i~=m then assert(false,"syntax error: unrecognized "..h:sub(i))end;return p,q,r,s,y,j,D end;local function F(p,q,r,s,y,j,D)local G=assert(f[j])local H=G.accum:gsub('%%s',p)for I=#y,1,-1 do local z=y[I]H=' if '..z..' then '..H..' end 'end;for I=#r,1,-1 do if not q[I]then local J='__in'..I;local K='__idx'..I;H=' for '..K..' = 1, #'..J..' do '..' local '..r[I][1]..' = '..J..'['..K..'] '..H..' end 'else H=' for '..e(r[I],', ')..' '..q[I]..' '..e(s[I],', ')..' do '..H..' end 'end end;H=' local __result = ( '..G.init..' ) '..H;return H end;local function L(H,M,D,s,N)assert(M>0)local O={}for I=1,D do O[#O+1]='_'..I end;for I=1,M do if not s[I]then local P='__in'..I;O[#O+1]=P end end;if#O>0 then H=' local '..e(O,', ')..' = ... '..H end;H=H..' return __result 'local Q,R=a.load(H,'tmp','t',N)if not Q then assert(false,R..' with generated code '..H)end;return Q end;local function S(h,N)local p,q,r,s,y,j,D=g(h)local H=F(p,q,r,s,y,j,D)local Q=L(H,#r,D,s,N)return Q end;local function T(N)if not N then N=a.getfenv(2)end;local U={}local V=setmetatable({},U)function U:__index(h)local Q=S(h,N)self[h]=Q;return Q end;U.__call=U.__index;V.new=T;return V end;local W={}W.new=T;return W
