Re: lambda function syntax

  • From: Denis Golovan <denis.golovan@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Mon, 11 Apr 2016 09:40:11 +0300

Hi Laurent

My needs for shorter functional syntax are definitely match yours.
So I am interested in your implementation.

However, I'd like know - are you going to support this syntax via some
public repo in case if official LuaJIT maintainers refuse to accept it
now?


2016-04-08 21:44 GMT+03:00 Laurent Deniau <Laurent.Deniau@xxxxxxx>:

Dear All,

Last night, I have extended the parser of LuaJIT to support the very compact
lambda function notation in Lua because I seriously needed a shorter syntax
to create functions. The patch is very minimal and simple as it extends the
parser of anonymous function definition with only few lines, hence the
semantic is strictly the same. I tried to keep the code simple, easy to read
and orthogonal to existing code in case someone would like to extend further
these functions.

Basically, the extension is a direct parsing of the lambda syntaxes:

\arg_list  expr
\arg_list (expr)
\arg_list (expr_list)

which is equivalent to the Lua syntax

function (arg_list) return expr      end
function (arg_list) return expr      end
function (arg_list) return expr_list end

The first lambda parameter name (if any) must “touch” the lambda sign
(backslash). Any space or tab between the lambda sign and a token will be
understood as a void lambda.

To activate the lambda syntax, patch the file lj_parse.c with the attached
patch in the src directory (today HEAD):
patch lj_parse.c < lj_parse_c.patch

and add in the FEATURES section of the Makefile in the same directory the
lines:
#
# Support for lambda function syntax
XCFLAGS+= -DLUAJIT_LAMBDA_SYNTAX
#

Examples of equivalent forms in Lua and Lambda syntaxes:

f=function (x) return x*x end
f=\x x*x
=f(2)
4

f=function (...) return {...} end
f=\... {...}
=f(4)[1]
4

f=function (x,y) return math.sqrt(x*x+y*y) end
f=\x,y math.sqrt(x*x+y*y)
=f(2,3)
3.605551275464

f=function (x) return function (y) return x+y,x-y end end
f=\x\y (x+y,x-y)
=f(2)(3)
5   -1

f=function () return  x end  -- free variable x
f=\ x                        -- note the space
x=10
=f()
10
x=20
=f()
20

f=function () return -x end  -- free variable x
f=\ -x                       -- note the space
x=10
=f()
-10
x=20
=f()
-20

a={ f=function (x,y) return x+y,x-y end, 3 }
a={ f=\x,y (x+y,x-y), 3 }
=a.f(2,3)
5   -1
=a[1]
3

x,y=4,6
a={ f=function (x,y) return x+y end, x-y, 3 }
a={ f=\x,y x+y, x-y, 3 }
=a.f(2,3)
5
=a[1]
-2
=a[2]
3

z=10
a={ f=function (s,y) return s.x*y+z end, x=3 }
a={ f=\s,y s.x*y+z, x=3 }
=a:f(2)
16
a.x=5
=a:f(2)
20

I hope that it will be considered to be useful enough to be included as an
extension in the main trunk of LuaJIT (e.g. like complex numbers), and maybe
proposed as a useful syntax extension to the Lua language.

Best,
Laurent

--

Laurent Deniau                           http://cern.ch/mad

Accelerators Beam Physics        mad@xxxxxxx
CERN, CH-1211 Geneva 23       Tel: +41 (0) 22 767 4647


Other related posts: