lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


In message <AANLkTinTu7P0jKdsYA4j36tbvGk555DHU4yWYMXmBX0o@mail.gmail.com> you wrote:

> On Thu, Dec 30, 2010 at 5:14 PM, David J. Slate <dslate@speakeasy.net> wrote:
> > Any thoughts on the desirability or feasibility of this idea?

Is this the sort of thing envisaged? OK it is RiscLua, not pure Lua, but
I guess the syntactic differences are not hard to swallow.

#! lua
-- library for tables with value true
do
local metabool
bool = \(t) => setmetatable(t,metabool) end
local paren = "(%s)"
local boolmeths = {
tostring = \(self)
        local o = {}
        for a,v in pairs(self) do
          if v then o[1+#o] = tostring(a) end -- if
         end -- for
         local s = table.concat(o,",")
         => paren:format(s)
         end;
}
metabool = {
__bit_and = \(x,y)
     local o = bool {}
       for a,v in pairs(x) do
         if v and y[a] then o[a] = true end -- if
       end -- for
       => o
     end;
__bit_or = \(x,y)
     local o = bool {}
     for a,v in pairs(x) do
       if v then o[a] = true end -- if
     end -- for
     for a,v in pairs(y) do
       if v then o[a] = true end -- if
     end -- for
     => o
    end;
__bit_xor = \(x,y)
      local o = bool {}
      for a,v in pairs(x) do
        if v and not y[a] then o[a] = true end -- if
      end -- for
      for a,v in pairs(y) do
        if v and not x[a] then o[a] = true end -- if
      end -- for
      => o
    end;
__index = boolmeths;
}

end

x = bool { a = true, b = true, c = true }
y = bool { b = true, c = true, d = true }

print((x&y):tostring())   --> (c,b)
print((x|y):tostring())   --> (a,d,c,b)
print((x^^y):tostring())  --> (a,d)

-- 
Gavin Wraith (gavin@wra1th.plus.com)
Home page: http://www.wra1th.plus.com/