[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Ternary operator patch
- From: Michal Kottman <k0mpjut0r@...>
- Date: Thu, 16 Sep 2010 11:10:58 +0200
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).
- References:
- Re: Ternary operator patch, steve donovan
- Re: Ternary operator patch, Henk Boom
- Re: Ternary operator patch, steve donovan
- Re: Ternary operator patch, Geoff Leyland
- Re: Ternary operator patch, steve donovan
- Re: Ternary operator patch, Miles Bader
- Re: Ternary operator patch, David Kastrup
- Re: Ternary operator patch, Miles Bader
- Re: Ternary operator patch, David Kastrup
- Re: Ternary operator patch, Jonathan Castello
- Re: Ternary operator patch, Roberto Ierusalimschy
- Re: Ternary operator patch, Alexander Gladysh
- Re: Ternary operator patch, steve donovan
- Re: Ternary operator patch, Jeff Pohlmeyer
- Re: Ternary operator patch, David Kastrup
- Re: Ternary operator patch, Michal Kolodziejczyk