lua-users home
lua-l archive

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


On Thu, 2010-09-16 at 10:41 +0200, Michal Kolodziejczyk wrote:
> On 16.09.2010 09:48, David Kastrup wrote:
> > Jeff Pohlmeyer <yetanothergeek@gmail.com> writes:
> > 
> >> On Thu, Sep 16, 2010 at 2:20 AM, steve donovan wrote:
> >>
> >>> Just to be different, how about a pseudo-function
> >>> choose(cond,val1,val2) with the appropriate lazy evaluation?
> >>
> >> pseudo?
> >>
> >> function choose(expr,yes,no)
> >>   if expr then return yes else return no end
> >> end
> > 
> > That evaluation is inappropriately unlazy.
> 
> Right, it should call functions at check time, like this:
> 
> function choose(cond, yesfunction, nofunction)
>   if cond then
>     return yesfunction()
>   else
>     return nofunction()
>   end
> end
> 
> But then it should be called like:
> choose(true, function() print('YES') end, function() print('NO') end)
> or in general case:
> choose(cond, yesfunction, nofunction)

Here, a syntax for short anonymous functions a-la RiscLua (\ and =>) or
Metalua (|x,y| x+y for function(x,y) return x+y end) would help. "Lazy
expressions" could be written as simple anonymous functions, and
choose() would be a normal Lua function (i.e. no "special" part of the
Lua grammar). The code could look like:

local x, y, z
z = choose(y~=0, || x/y, || 0)

However, the syntax sugar for anonymous functions has also been
discussed on this list before (and rejected, AFAIK).