[proposal] infix function syntax sugar

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

[proposal] infix function syntax sugar

Xavier Wang

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~

Reply | Threaded
Open this post in threaded view
|

Re: [proposal] infix function syntax sugar

Rena
2011/12/17 Xavier Wang <[hidden email]>:

> 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~

That would be an interesting idea. Funny I was just thinking of infix
functions a few minutes ago too!

--
Sent from my toaster.

Reply | Threaded
Open this post in threaded view
|

Re: [proposal] infix function syntax sugar

Dimiter 'malkia' Stanev
In reply to this post by Xavier Wang
-- 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

local function add(a,b)
   return a + b
end

print( infix( 12 ,add, 3 ,add, 10 ) )



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~
>


Reply | Threaded
Open this post in threaded view
|

Re: [proposal] infix function syntax sugar

Rena
2011/12/18 Dimiter "malkia" Stanev <[hidden email]>:

> -- 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
>
> local function add(a,b)
>   return a + b
> end
>
> print( infix( 12 ,add, 3 ,add, 10 ) )
>
>
>
> 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~
>>
>
>

I had done some hacking of metatables to make the following valid syntax:
foo = (3) '<<' (x)

It's still pretty ugly and not terribly efficient, but it works... :p

--
Sent from my toaster.

Reply | Threaded
Open this post in threaded view
|

Re: [proposal] infix function syntax sugar

Dimiter 'malkia' Stanev
And even uglier one, that can handle arithmetic brackets, but reusing
lua's { }

very crude

local function infix(...)
    local function eval(t)
       local l = t[1]
       if type(l) == "table" then l = eval(l) end
       for k = 2,#t,2 do
         local f, r = t[k], t[k+1]
         if type(r) == "table" then r = eval(r) end
         l = f(l, r)
       end
       return l
    end
    return eval(...)
end

local function add(a,b)
    return a + b
end

local function sub(a,b)
    return a - b
end

local function mul(a,b)
    return a * b
end

local function div(a,b)
    return a / b
end

-- Here be { brackets, not (
print( infix{ 12 ,add, 3 ,add, 10 ,sub, { 30 ,mul, 10 } } )



On 12/18/11 1:11 AM, HyperHacker wrote:

> 2011/12/18 Dimiter "malkia" Stanev<[hidden email]>:
>> -- 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
>>
>> local function add(a,b)
>>    return a + b
>> end
>>
>> print( infix( 12 ,add, 3 ,add, 10 ) )
>>
>>
>>
>> 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~
>>>
>>
>>
>
> I had done some hacking of metatables to make the following valid syntax:
> foo = (3) '<<' (x)
>
> It's still pretty ugly and not terribly efficient, but it works... :p
>


Reply | Threaded
Open this post in threaded view
|

Re: [proposal] infix function syntax sugar

Dimiter 'malkia' Stanev
-- untabified version (what's up with aquamacs?)

local function infix(...)
    local function eval(t)
       local l = t[1]
       if type(l) == "table" then l = eval(l) end
       for k = 2,#t,2 do
          local f, r = t[k], t[k+1]
         u if type(r) == "table" then r = eval(r) end
          l = f(l, r)
       end
       return l
    end
    return eval(...)
end

local function add(a,b)
    return a + b
end

local function sub(a,b)
    return a - b
end

local function mul(a,b)
    return a * b
end

local function div(a,b)
    return a / b
end

-- Here be { brackets, not (
print( infix{ 12 ,add, 3 ,add, 10 ,sub, { 30 ,mul, 10 } } )


Reply | Threaded
Open this post in threaded view
|

Re: [proposal] infix function syntax sugar

Dimiter 'malkia' Stanev
-- Some typo in the previous version

local function infix(...)
    local function eval(t)
       local l = t[1]
       if type(l) == "table" then l = eval(l) end
       for k = 2,#t,2 do
         local f, r = t[k], t[k+1]
         if type(r) == "table" then r = eval(r) end
         l = f(l, r)
       end
       return l
    end
    return eval(...)
end

local function add(a,b)
    return a + b
end

local function sub(a,b)
    return a - b
end

local function mul(a,b)
    return a * b
end

local function div(a,b)
    return a / b
end

print(
    infix{
       12 ,add, 3 ,add, 10 ,sub, { 30 ,mul, 10 } ,div, { 5 ,add, 5 } } )

-- Should print: -27.5

Reply | Threaded
Open this post in threaded view
|

Re: [proposal] infix function syntax sugar

Michal Kottman
In reply to this post by Xavier Wang


18.12.2011 6:23, "Xavier Wang" <[hidden email]>:
>
> 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)

If you can load bytecode on your embedded device, or can afford slower compilation of scripts, try Metalua. It supports exactly the syntax you want, see http://metalua.luaforge.net/manual003.html#toc3

Reply | Threaded
Open this post in threaded view
|

Re: [proposal] infix function syntax sugar

Philipp Janda
In reply to this post by Dimiter 'malkia' Stanev
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):

local function infix( o1, ... )
  -- 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~
>>
>


Reply | Threaded
Open this post in threaded view
|

Re: [proposal] infix function syntax sugar

Xavier Wang

在 2011-12-18 傍晚6:32,"Philipp Janda" <[hidden email]>写道:
>
> 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~
> >>
> >
>
>

Reply | Threaded
Open this post in threaded view
|

Re: [proposal] infix function syntax sugar

Philipp Janda
On 20.12.2011 11:34, Xavier Wang wrote:
> 在 2011-12-18 傍晚6:32,"Philipp Janda"<[hidden email]>写道:
>>
>> [...]
>>
>> (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.

Sorry to hear that.

>
>
> 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.

As far as I know, f can be anything including numbers, lightuserdata and
strings. With some C hacking you could probably even use boolean (which
is an int in the C implementation).

> 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.

That depends on what f is. If you want type-safety, f is a table or a
full userdata and you need to allocate memory. If you don't need
type-safety (i.e. if you are the only one using lightuserdata), and you
are ok with implementation-defined behavior, you would probably use
lightuserdata. Else you have only numbers.

To implement type-safe enums, I would add something like

struct {
  uint32_t value;
  uint32_t enum_id;
} e;

to the Value union in lobject.h and a global counter (in the registry,
maybe) to create unique enum_ids. This wouldn't bloat Lua objects for
the common case that lua_Number is a double, and you would have lots of
different type-safe enum types of 32 Bits each. You could even
reimplement booleans as a special case of one of those enum types.

There was an enum patch[1] some time ago, but I think it allocated extra
memory for the enums.

  [1]: http://lua-users.org/lists/lua-l/2006-09/msg01085.html

>
> regards.
>

Philipp