# [Proposal] Lua-y lambda syntax

25 messages
12
Open this post in threaded view
|

## [Proposal] Lua-y lambda syntax

 So, out of all syntax proposals for lambas, I'm surpised nobody thought of this beauty. You see, we have `local x = function(a,b,c) return thing end` syntax. BUT: 1. It's a syntax error to use `function(a,b,c) =thing end`, even if the REPL accepts `=` as an alias for `return`. 2. Do we need `end` if we restrict it to a single expression? Combine these 2, and the simplest solution seems to be: local x = function(a,b,c) = thing AND we can still get multiple returns: local x = function(a,b,c) = select(1,a,b,c) print(x(1, 2, 3)) --> 1, 2, 3 The idea is, function(a,b,c) starts a plain old function. If the next token is a =, this has to be a lambda. Otherwise it has to be a function. There's no ambiguity here since the proposed syntax is currently a syntax error. It also looks very much like Lua. -- Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.
Open this post in threaded view
|

## Re: [Proposal] Lua-y lambda syntax

 On Thu, Apr 27, 2017 at 5:43 PM, Soni L. wrote:So, out of all syntax proposals for lambas, I'm surpised nobody thought of this beauty. You see, we have `local x = function(a,b,c) return thing end` syntax. BUT: 1. It's a syntax error to use `function(a,b,c) =thing end`, even if the REPL accepts `=` as an alias for `return`. 2. Do we need `end` if we restrict it to a single expression? Combine these 2, and the simplest solution seems to be: local x = function(a,b,c) = thing AND we can still get multiple returns: local x = function(a,b,c) = select(1,a,b,c) print(x(1, 2, 3)) --> 1, 2, 3 The idea is, function(a,b,c) starts a plain old function. If the next token is a =, this has to be a lambda. Otherwise it has to be a function. There's no ambiguity here since the proposed syntax is currently a syntax error. It also looks very much like Lua. The following looks nice:local swap = function(a,b) = b,aBut what does the following nested lambdalocal x = function(a,b) = function() = a, bmean:local x = function(a,b) = (function() = a, b)orlocal x = function(a,b) = (function() = a), b?
Open this post in threaded view
|

## Re: [Proposal] Lua-y lambda syntax

 On 2017-04-27 02:33 PM, Egor Skriptunoff wrote: > On Thu, Apr 27, 2017 at 5:43 PM, Soni L. <[hidden email] > > wrote: > >     So, out of all syntax proposals for lambas, I'm surpised nobody >     thought of this beauty. > >     You see, we have `local x = function(a,b,c) return thing end` >     syntax. BUT: > >     1. It's a syntax error to use `function(a,b,c) =thing end`, even >     if the REPL accepts `=` as an alias for `return`. >     2. Do we need `end` if we restrict it to a single expression? > >     Combine these 2, and the simplest solution seems to be: > >     local x = function(a,b,c) = thing > >     AND we can still get multiple returns: > >     local x = function(a,b,c) = select(1,a,b,c) >     print(x(1, 2, 3)) --> 1, 2, 3 > >     The idea is, function(a,b,c) starts a plain old function. If the >     next token is a =, this has to be a lambda. Otherwise it has to be >     a function. > >     There's no ambiguity here since the proposed syntax is currently a >     syntax error. It also looks very much like Lua. > > > The following looks nice: > local swap = function(a,b) = b,a local swap = (function(a,b) = b), a > > But what does the following nested lambda > local x = function(a,b) = function() = a, b > mean: > local x = function(a,b) = (function() = a, b) > or > local x = function(a,b) = (function() = a), b > ? > You need to use select() if you want multiple return values. It's a single end-of-list-expr. (OT: Sorry for accidentally replying wrong. Some thunderbird update changed the behaviour of the "reply" button so instead of defaulting to reply-to-list I need to actually click the reply-to-list button, which I'm not used to doing.) -- Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.
Open this post in threaded view
|

## Re: [Proposal] Lua-y lambda syntax

 On Thu, Apr 27, 2017 at 8:59 PM, Soni L. wrote: You need to use select() if you want multiple return values.That's awkward.
Open this post in threaded view
|

## Re: [Proposal] Lua-y lambda syntax

 On 2017-04-27 03:12 PM, Egor Skriptunoff wrote: > On Thu, Apr 27, 2017 at 8:59 PM, Soni L. <[hidden email] > > wrote: > > >     You need to use select() if you want multiple return values. > > > That's awkward. local function nop(...) = select(1, ...) local function swap(a,b) = nop(b,a) It's the best way to add lambdas without introducing wildly different syntax. -- Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.
Open this post in threaded view
|

## Re: [Proposal] Lua-y lambda syntax

 On Thu, Apr 27, 2017 at 11:39 AM, Soni L. <[hidden email]> wrote: > > > On 2017-04-27 03:12 PM, Egor Skriptunoff wrote: >> >> On Thu, Apr 27, 2017 at 8:59 PM, Soni L. <[hidden email] >> > wrote: >> >> >>     You need to use select() if you want multiple return values. >> >> >> That's awkward. > > local function nop(...) = select(1, ...) > > local function swap(a,b) = nop(b,a) > > It's the best way to add lambdas without introducing wildly different > syntax. > > > -- > Disclaimer: these emails may be made public at any given time, with or > without reason. If you don't agree with this, DO NOT REPLY. > > I was just contemplating how one would add a linq like feature to Lua for searching and returning result sets as tables without having to write iterators. This syntax would solve that problem quite nicely, wouldn't it? However, would that mean the need to add other functions like where(), join() and similar 'SQL like' concepts? Russ
Open this post in threaded view
|

## Re: [Proposal] Lua-y lambda syntax

 In reply to this post by Soni "They/Them" L. On 04/27/2017 06:59 PM, Soni L. wrote: > You need to use select() if you want multiple return values. It's a > single end-of-list-expr. How about requiring parens as part of the syntax? e.g. function(a,b) =( b,a ) function() =( "foo" ) Or would that push the parser too far? Scott
Open this post in threaded view
|

## Re: [Proposal] Lua-y lambda syntax

 In reply to this post by Russell Haley On 2017-04-27 04:00 PM, Russell Haley wrote: > On Thu, Apr 27, 2017 at 11:39 AM, Soni L. <[hidden email]> wrote: >> >> On 2017-04-27 03:12 PM, Egor Skriptunoff wrote: >>> On Thu, Apr 27, 2017 at 8:59 PM, Soni L. <[hidden email] >>> > wrote: >>> >>> >>>      You need to use select() if you want multiple return values. >>> >>> >>> That's awkward. >> local function nop(...) = select(1, ...) >> >> local function swap(a,b) = nop(b,a) >> >> It's the best way to add lambdas without introducing wildly different >> syntax. >> >> >> -- >> Disclaimer: these emails may be made public at any given time, with or >> without reason. If you don't agree with this, DO NOT REPLY. >> >> > I was just contemplating how one would add a linq like feature to Lua > for searching and returning result sets as tables without having to > write iterators. This syntax would solve that problem quite nicely, > wouldn't it? However, would that mean the need to add other functions > like where(), join() and similar 'SQL like' concepts? > > Russ > ... Linq? Anyway, a DSL for SQL queries in Lua sounds awesome. I guess this "lambda syntax" I proposed would do for some impressive DSLs. -- Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.
Open this post in threaded view
|

## Re: [Proposal] Lua-y lambda syntax

 In reply to this post by Scott Morgan On 2017-04-27 09:14 PM, Scott Morgan wrote: > On 04/27/2017 06:59 PM, Soni L. wrote: >> You need to use select() if you want multiple return values. It's a >> single end-of-list-expr. > How about requiring parens as part of the syntax? > > e.g. > function(a,b) =( b,a ) > function() =( "foo" ) > > Or would that push the parser too far? > > Scott > > Ehh... not sure how I feel about this. return (thing) already has a meaning. Requiring parens would be awful, but optional parens would be confusing (is "=(val)" the same as "return val" or "return (val)"? read the docs to find out!)... -- Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.
Open this post in threaded view
|

## Re: [Proposal] Lua-y lambda syntax

 In reply to this post by Russell Haley [Is there a comprehensive list somewhere of all the big ways to build DSLs in Lua?] On Apr 27, 2017, at 3:00 PM, Russell Haley <[hidden email]> wrote: >> > I was just contemplating how one would add a linq like feature to Lua > for searching and returning result sets as tables without having to > write iterators. This syntax would solve that problem quite nicely, > wouldn't it? However, would that mean the need to add other functions > like where(), join() and similar 'SQL like' concepts? For any programming language people who aren't familiar with it, here's the outer language's syntactic view of LINQ: LINQ is a way of writing a kind of AST literal, parsed from a specific query sub-language. The query sublanguage is itself interesting. At runtime, the AST can be executed directly, compiled to bytecode, or unparsed into SQL queries (for example). But what I am especially interested in for Lua are meta-mechanisms, the things that can support multiple little languages. LINQ is a good instance of something it would be nice to be able to implement, then.[1] One solution is to create your language out of function calls, tables, operators, and values, in the way the native LPeg does. As I've complained ad tedium, there is still no good way to create new control mechanisms this way except through lambdas or Smalltalk/Ruby-style blocks; both postpone evaluation of code in the current scope. But maybe you don't need those control mechanisms very much. And even if you do need lambdas, lexical scoping always works the right way. Deserving special mention for constructing DSLs are common syntax hacks, like chaining methods, abusing "f 'name' {}", etc. Another is macros, sometimes just to augment the previous approach just enough. I have convinced myself that there will never be execution at compile-time in Lua (and also convinced myself that it is a good judgement for Lua). Sorta in the same category are various other meta-Luas. The cost is direct source compatibility.[2] My favorite method (but alas the one I use least) is to parse a string at runtime and turn it into a Lua function. So taking the Wikipedia LINQ C# example, it would look something like: results =  linq[[   from c in SomeCollection   where c.SomeProperty < 10   select new {c.SomeProperty, c.OtherProperty} ]] for result in ipairs(results) do   print(result) end Conveniently for me, there are no unbound variables in that query. Because otherwise I'd have to write something like: results = linq{[[   from c in SomeCollection   where c.SomeProperty < limit   select new {c.SomeProperty, c.OtherProperty} ]],   limit=limit} Note that the stuff in linq[[ ]]'s function can't write to any variables in the outer scope.[3] It can write on objects it's passed, though. -- Jay [1]: I'd like it to be possible; that being said, I don't plan on implementing LINQ-for-Lua, unless perhaps a patient person pays me. [2]: You could use LuaMacro 2 if you want macro-transformed source back. The laconic version of the Powell Doctrine is, "Have an exit plan." This applies to software too. [3]: I like to use "%limit" as the syntax for variables exported from Lua. This is what Lua 4.0 did inside procedures, and who doesn't want to be reminded of the Lua before empty proxy-tables were necessary? ;-)
Open this post in threaded view
|

## Re: [Proposal] Lua-y lambda syntax

 On Fri, Apr 28, 2017 at 4:36 PM, Jay Carlson <[hidden email]> wrote: > [Is there a comprehensive list somewhere of all the big ways to build DSLs in Lua?] > > On Apr 27, 2017, at 3:00 PM, Russell Haley <[hidden email]> wrote: >>> >> I was just contemplating how one would add a linq like feature to Lua >> for searching and returning result sets as tables without having to >> write iterators. This syntax would solve that problem quite nicely, >> wouldn't it? However, would that mean the need to add other functions >> like where(), join() and similar 'SQL like' concepts? > > For any programming language people who aren't familiar with it, here's the outer language's syntactic view of LINQ: > > LINQ is a way of writing a kind of AST literal, parsed from a specific query sub-language. > > The query sublanguage is itself interesting. At runtime, the AST can be executed directly, compiled to bytecode, or unparsed into SQL queries (for example). But what I am especially interested in for Lua are meta-mechanisms, the things that can support multiple little languages. LINQ is a good instance of something it would be nice to be able to implement, then.[1] > > One solution is to create your language out of function calls, tables, operators, and values, in the way the native LPeg does. As I've complained ad tedium, there is still no good way to create new control mechanisms this way except through lambdas or Smalltalk/Ruby-style blocks; both postpone evaluation of code in the current scope. But maybe you don't need those control mechanisms very much. And even if you do need lambdas, lexical scoping always works the right way. > > Deserving special mention for constructing DSLs are common syntax hacks, like chaining methods, abusing "f 'name' {}", etc. > > Another is macros, sometimes just to augment the previous approach just enough. I have convinced myself that there will never be execution at compile-time in Lua (and also convinced myself that it is a good judgement for Lua). Sorta in the same category are various other meta-Luas. The cost is direct source compatibility.[2] > > My favorite method (but alas the one I use least) is to parse a string at runtime and turn it into a Lua function. So taking the Wikipedia LINQ C# example, it would look something like: > > results =  linq[[ >   from c in SomeCollection >   where c.SomeProperty < 10 >   select new {c.SomeProperty, c.OtherProperty} ]] > > for result in ipairs(results) do >   print(result) > end > > Conveniently for me, there are no unbound variables in that query. Because otherwise I'd have to write something like: > > results = linq{[[ >   from c in SomeCollection >   where c.SomeProperty < limit >   select new {c.SomeProperty, c.OtherProperty} ]], >   limit=limit} > > Note that the stuff in linq[[ ]]'s function can't write to any variables in the outer scope.[3] It can write on objects it's passed, though. I believe Ronald has provided the functional implementation to those imagined keyword mappings in his Moses library (another post)? http://yonaba.github.io/Moses/doc/His chaining seems to be a "syntax hack you mentioned" but in .net there is a similar syntax to access LINQ without the DSL. Russ
Open this post in threaded view
|

## Re: [Proposal] Lua-y lambda syntax

 In reply to this post by Jay Carlson On Sat, Apr 29, 2017 at 1:36 AM, Jay Carlson <[hidden email]> wrote: > My favorite method (but alas the one I use least) is to parse a string at runtime and turn it into a Lua function. So taking the Wikipedia LINQ C# example, it would look something like: > > results =  linq[[ >   from c in SomeCollection >   where c.SomeProperty < 10 >   select new {c.SomeProperty, c.OtherProperty} ]] > > for result in ipairs(results) do >   print(result) > end I did something like this for constructing queries for columnar data: http://stevedonovan.github.io/Penlight/api/manual/06-data.md.html#Reading_Columnar_Data(see the examples for pl.data) The trick was to convert the pseudo-SQL into equivalent Lua, which is then loaded. Gotta love eval() in all its forms ;) As for macros, I was a believer. But experience suggests that ad-hoc syntax has to be very carefully designed - harder than writing good libraries.  Design sense is relatively rare in the universe.
Open this post in threaded view
|

## Re: [Proposal] Lua-y lambda syntax

 Like most threads, this one has wandered way off topic. I apologize insincerely for the fact that this will look on a threaded reader like a reply to Steve's most recent post. I have never grasped why people want lambda syntax. The following is the existing Lua syntax: > fct = λ (a,b,c) → 10*(10*a+b)+c ▪ > print(fct(2,7,3))  --> 273 I actually have just made a simple hack of the Lua interpreter that did the above. I.e. there is nothing verbose about the syntax, people are just too lazy to type 'function', 'return' and 'end'.
Open this post in threaded view
|

## Re: [Proposal] Lua-y lambda syntax

 It was thus said that the Great Dirk Laurie once stated: > Like most threads, this one has wandered way off topic. I apologize > insincerely for the fact that this will look on a threaded reader like > a reply to Steve's most recent post. > > I have never grasped why people want lambda syntax. The following > is the existing Lua syntax: > > > fct = λ (a,b,c) → 10*(10*a+b)+c ▪ > > print(fct(2,7,3))  --> 273 > > I actually have just made a simple hack of the Lua interpreter that > did the above. > > I.e. there is nothing verbose about the syntax, people are just too > lazy to type 'function', 'return' and 'end'.   +1   -spc (Who is becoming convinced that more and more programmers don't         really want to type anything ... )
Open this post in threaded view
|

## Re: [Proposal] Lua-y lambda syntax

 In reply to this post by Dirk Laurie-2 On 2017-04-29 05:49 AM, Dirk Laurie wrote: > Like most threads, this one has wandered way off topic. I apologize > insincerely for the fact that this will look on a threaded reader like > a reply to Steve's most recent post. > > I have never grasped why people want lambda syntax. The following > is the existing Lua syntax: > >> fct = λ (a,b,c) → 10*(10*a+b)+c ▪ >> print(fct(2,7,3))  --> 273 > I actually have just made a simple hack of the Lua interpreter that > did the above. > > I.e. there is nothing verbose about the syntax, people are just too > lazy to type 'function', 'return' and 'end'. > function a_query(_) =_.SELECT("*").FROM("users").WHERE("age < 100") for x in do_query(a_query) do    ... end No ugly `end`, looks a lot more like SQL. It's a new option for DSLs. -- Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.
Open this post in threaded view
|

## Re: [Proposal] Lua-y lambda syntax

Open this post in threaded view
|

## Re: [Proposal] Lua-y lambda syntax

 On Sat, Apr 29, 2017 at 5:15 PM, Jay Carlson <[hidden email]> wrote: > I should just get used to glancing that manual again before opening my > mouth. As usual with pl, It's In There. Notice it's called pseudo-SQL ;)  Real SQL is a Specialized Problem, which I felt outside the ambit of pl.  Didn't stop me from fooling around with XML, of course ;) > What's pl's compatibility profile these days? 5.1+5.2? I've been avoiding > doing it seriously, but the still-on-rushing JSON juggernaut[2] suggests a > need for jsom. Works with 5.3 - if it doesn't, then that's a Bug. > [1]: SQL is pronounced "squeal". So See-quell is the yahoo version? ;) > Finally, for non-private protocols there will be a mismatch between the > surface expressions and the internal models for it. So we're back to tearing > down JSON trees to build our own trees, and you had to do that for XML too. Rust has an entertaining take on the problem with the Serde framework.. By adding a few attributes to a struct (and doing some clever macro magic) you can very easily create it from JSON of the right shape and spit it out again with a few lines. All done at compile time, very efficient.
Open this post in threaded view
|

## Re: [Proposal] Lua-y lambda syntax

 In reply to this post by Dirk Laurie-2 The token filter below can be used with my ltokenp to convert         fct = Î» (a,b,c) â 10*(10*a+b)+c âª to         fct = function ( a , b , c ) return 10 * ( 10 * a + b ) + c end -- token filter: handle utf symbols as keywords local function emit(x)         io.write(x," ") end local acc="" local convert={         ["\xce\xbb"] = "function",         ["\xe2\x86\x92"] = "return",         ["\xe2\x96\xaa"] = "end", } local function flush()         if acc~="" then                 emit(convert[acc])                 acc=""         end end function FILTER(line,token,text,value)         local t=text         if t=="" or t=="" then flush() return end         if t=="" then value=string.format("%q",value) end         if token>127 and text==value then                 acc=acc..t         else                 flush()                 emit(value)         end end
Open this post in threaded view
|

## Re: [Proposal] Lua-y lambda syntax

 On Apr 30, 2017, at 11:45 AM, Luiz Henrique de Figueiredo <[hidden email]> wrote: > > The token filter below can be used with my ltokenp to convert > > fct = Î» (a,b,c) â 10*(10*a+b)+c âª I think your local mail system, your terminal program, and/or your editor are having a fight about whether they are in UTF-8 or not. That looks a lot like UTF-8 misinterpreted as ISO-8859-1. And from the mail headers: | Content-Type: text/plain; charset=iso-8859-1 I suspect this is Mutt being foolish, as well as wrong. Jay signature.asc (465 bytes) Download Attachment