lua-users home
lua-l archive

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


 > steve donovan <steve.j.donovan@gmail.com> writes:
 > 
 > > PS the more I play with things like |x| x*(x-1), I wish that this
 > > syntax was supported in vanilla Lua. A very convenient bit of
 > > syntactical sugar if you are fond of the functional style.
 > 
 > function(x) return x*(x-1) end
 > 
 > is terse enough not to pine for shortcuts looking like line noise.

No it's not.  I've written too much "\x -> x * (x-1)" to be satisfied
with standard Lua syntax.  This syntax, from Haskell, looks
like line noise only if you've never seen lambda calculus.

Look at the information design: in the standard Lua example you give,
there are 30 characters of which 8 are unique to this particular
function, and the rest are syntactic noise.  The Haskell version is 15
characters with 10 unique ones, or if you compress to "\x -> x*(x-1)"
(apples to apples), 13 characters of which 8 are unique to the
function.  The information density for the Haskell syntax is much higher.

Lua's standard syntax militates against anonymous functions, and it
often drives what would be one-line functions onto a second line.
For example this simple example does not fit in 80 columns:

  local sizes = table.map(function(s) return s:gsub('%A', ''):lower() end, names)

but

  local sizes = table.map(\s -> s:gsub('%A', ''):lower(), names)

fits easily and is (in my opinion) easier to read.

I recognize that Lua is not an expression-oriented language and that a
special syntax for functions does not meet Lua's goal of being easily
accessible to Pascal programmers.  But it frustrates me that Lua is a
functional language with full lexical closures and yet the syntactic
overhead for anonymous functions is a heavy burden.  (I'm also
frustrated that the library support for functional programming is
poor---table.map is a function I had to define myself.)


Norman Ramsey