Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- This file: -- http://anggtwu.net/LUA/Comprehensions1.lua.html -- http://anggtwu.net/LUA/Comprehensions1.lua -- (find-angg "LUA/Comprehensions1.lua") -- Author: Eduardo Ochs <eduardoochs@gmail.com> -- -- (defun e () (interactive) (find-angg "LUA/Comprehensions1.lua")) -- (mpgp 9 "comprehension-tables") -- (mpga "comprehension-tables") -- (mpga "comprehension-tables" "Stop") -- (find-angg "LUA/Rect.lua") -- Path.addLUAtopath() -- require "Rect" -- (find-angg "LUA/Rect.lua") MTree = Class { type = "MTree", _ = function (o) return MTree {[0]=o} end, from = function (o) if type(o) == "number" then o = tostring(o) end if type(o) == "string" then return MTree {[0]=o} end local nodes = map(MTree.from, o) nodes[0] = o[0] return MTree(nodes) end, tosyntree = function (o) return SynTree.from(o) end, torect = function (o) return MTree.tosyntree(o):torect() end, __tostring = function (o) return MTree.torect(o):tostring() end, totexmatrixrect = function (o) o = MTree.from(o) if #o == 0 then return Rect {(o[0] or "").." \\\\"} end local RR = Rect {} for _,node in ipairs(o) do for _,line in ipairs(MTree.totexmatrixrect(node)) do table.insert(RR, line) end end if not o[0] then return RR end local Ltop = format("%s & ", o[0]) local Lblank = Ltop:gsub("[^&]", " ") local LL = Rect {Ltop} while #LL < #RR do table.insert(LL, Lblank) end return LL..RR end, totexmatrix = function (o) return MTree.totexmatrixrect(o):tostring() end, __index = { tosyntree = function (m) return MTree.tosyntree(m) end, torect = function (m) return MTree.torest(m) end, totexmatrixrect = function (m) return MTree.totexmatrixrect(m) end, totexmatrix = function (m) return MTree.totexmatrix(m) end, tostring = function (m) return tostring(m) end, addto = function (m,mouter) table.insert(mouter,m); return m end, add = function (m,a) table.insert(m,a); return m end, addstopifempty = function (m) if #m == 0 then m:add("\\Stop") end; return m end, }, } Test1 = function () local T_ = MTree._() for x=1,2 do local Tx = MTree._(x):addto(T_) for y=3,4 do local Ty = MTree._(y):addto(Tx) for z=5,6 do local Tx = MTree._(z):addto(Ty) end end end return T_ end --[[ * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) dofile "Comprehensions1.lua" T3 = {[0]=3, 5, 6} T4 = {[0]=4, 5, 6} T1 = {[0]=1, T3, T4} T2 = {[0]=2, T3, T4} T = { T1, T2} = MTree.from(T) = MTree.totexmatrixrect(T) = MTree.totexmatrix(T) T = Test1() = T; = T:totexmatrix(); --]] tocurly = function (L) return "{"..mapconcat(tostring, L, ",").. "}" end tocurly = function (L) return "\\{"..mapconcat(tostring, L, ",").."\\}" end Test2 = function () local T = MTree._() for x=1,5 do local lim = 6-x local range = tocurly(seq(x,lim)) local T = MTree._(x):addto(T) local T = MTree._(lim):addto(T) local T = MTree._(range):addto(T) for y=x,6-x do local T = MTree._(y):addto(T) local T = MTree._(format("(%s,%s)",x,y)):addto(T) end T:addstopifempty() end return T end Test3 = function () local T = MTree._() for x=1,5 do local lim = 6-x --local range = tocurly(seq(x,lim)) local T = MTree._(x):addto(T) --local T = MTree._(lim):addto(T) --local T = MTree._(range):addto(T) for y=x,6-x do local T = MTree._(y):addto(T) local T = MTree._(format("(%s,%s)",x,y)):addto(T) end T:addstopifempty() end return T end --[[ * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) dofile "Comprehensions1.lua" = tocurly(seq(2,10,3)) = tocurly(seq(2,10,-1)) T = Test2() = T; = T:totexmatrix(); T = Test3() = T; = T:totexmatrix(); --]] -- Local Variables: -- coding: utf-8-unix -- End: