lua-users home
lua-l archive

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



在 2011-12-18 傍晚6:32,"Philipp Janda" <siffiejoe@gmx.net>写道:
>
> On 18.12.2011 10:09, Dimiter "malkia" Stanev wrote:
> > -- Here is a very poor attempt :)
> >
> > local function infix(...)
> >     local args = {...}
> >     local nargs = #args
> >     local r = args[1]
> >     for k=2,nargs,2 do
> >        r = args[k](r, args[k+1])
> >     end
> >     return r
> > end
>
> This is a very nice idea, one of those gems one sometimes finds on this
> mailing list. I would write it differently, though, to avoid unnecessary
> garbage as requested by the OP (although I still can't see how infix
> functions would help him solve his userdata allocation problem):
>
Sorry for late reply, I'm just fired by boss :-( so I can't online serveral days.
 

think this:
local f = obj:getflags()
if f:contains "FOO, BAR" then
  -- do something
end
 
if you want to write f:something, then f must a userdata or table, so you must allocate a memory for that value.
but if you do conditions like this:
if contains(f, "FOO, BAR") then
  -- do something
end
 
then no memory will allocate. but it's not clearly, think this:
 
if f `contains` "Foo, Bar" then
end
 
it just a syntax sugar of normal form. it's very like f:contains, but don't need allocate any memory.
 
regards.
 

 

> local function infix( o1, ...a  )
>  -- if type( o1 ) == "table" then o1 = infix( unpack( o1 ) ) end
>  for i = 1, select( '#', ... ), 2 do
>    local f, o2 = select( i, ... )
>    -- if type( o2 ) == "table" then o2 = infix( unpack( o2 ) ) end
>    o1 = f( o1, o2 )
>  end
>  return o1
> end
>
>
> >
> > local function add(a,b)
> >     return a + b
> > end
> >
> > print( infix( 12 ,add, 3 ,add, 10 ) )
> >
> >
>
> Philipp
>
>
> >
> > On 12/17/11 9:23 PM, Xavier Wang wrote:
> >> hi robero and list,
> >>
> >> i'm inspired by the post about implement c enum. somebody said the
> >> getflags function shoud return a userdata to implement a contains
> >> function. i think its a good idea, excerpt a details: thus will allocate
> >> memory.
> >>
> >> i'm run lua on a embedded system, embedded systemembedded system, nokia
> >> n73 cellphone. my game will lag if i allocate memory in draw function
> >> since the garbage collection. so we avoid any memory in event loop. but
> >> i really miss the contains function, so i think a syntax allow use
> >> function as infix operator will be great.
> >>
> >> e.g. if i have a function add(a, b), i can call it with a `add` b, its
> >> a syntax sugar of add(a,b)
> >>
> >> its very simple and can used in many situations, e.g. bitop calls, and
> >> flags contains test.
> >>
> >> has anybody agree me? i hope this feature can appear on lua5.3 :)
> >>
> >> thanks everybody~
> >>
> >
>
>