Popularity contest

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Popularity contest

Artur Galyamov
26.09.11, 14:43, "Egil Hjelmeland" <[hidden email]>:
>  Even more generic:
>  
>     local a,b,c from t
>  
>  being equivalent to
>  
>     local a,b,c = t.a, t.b, t.c

If I remember correctly, it was suggested several months ago, but using "in" keyword:

    local min, max, floor in math

No new keywords there. I think it would be nice way to easily localize globals.

Reply | Threaded
Open this post in threaded view
|

Re: Popularity contest

steve donovan
2011/9/26 Artur Galyamov <[hidden email]>:
>     local min, max, floor in math
>
> No new keywords there. I think it would be nice way to easily localize globals.

Yes, it's convenient. I agree with Dirk actually that it's not worth
adding new keywords for this convenience, but it _is_ less ugly than
the full version.

steve d.

Reply | Threaded
Open this post in threaded view
|

Re: Popularity contest

Krunal Rao
steve donovan <steve.j.donovan <at> gmail.com> writes:

>
> 2011/9/26 Artur Galyamov <artur-pub <at> yandex.ru>:
> >     local min, max, floor in math
> >
> > No new keywords there. I think it would be nice way to easily localize
globals.
>
> Yes, it's convenient. I agree with Dirk actually that it's not worth
> adding new keywords for this convenience, but it _is_ less ugly than
> the full version.
>
> steve d.

I think this proposal is quite convenient. At the moment I do something like:

local min, max, sin, cos = ridx(math, "min, max, sin, cos")

which allows me to copy&paste lhs to rhs to minize typing errors.

I also like to proposal to have obj:f returns a closure with "binded" self
argument. I actually thought about this proposal before reading it here, but
I guess it's not that hard to immagine :P

KR




Reply | Threaded
Open this post in threaded view
|

Re: Popularity contest

Enrico Tassi-3
On Mon, Sep 26, 2011 at 05:05:41PM +0000, KR wrote:

> steve donovan <steve.j.donovan <at> gmail.com> writes:
>
> >
> > 2011/9/26 Artur Galyamov <artur-pub <at> yandex.ru>:
> > >     local min, max, floor in math
> > >
> > > No new keywords there. I think it would be nice way to easily localize
> globals.
> >
> > Yes, it's convenient. I agree with Dirk actually that it's not worth
> > adding new keywords for this convenience, but it _is_ less ugly than
> > the full version.
> >
> > steve d.
>
> I think this proposal is quite convenient. At the moment I do something like:
>
> local min, max, sin, cos = ridx(math, "min, max, sin, cos")

Nice trick. You could also make rdix a table with a special metatable so
that the following works too

local min, max, sin, cos = from.math "min, max, sin, cos"

But of course you have to repeate the function names twice...

Cheers
--
Enrico Tassi

Reply | Threaded
Open this post in threaded view
|

Re: Popularity contest

Manabu eo
In reply to this post by Artur Galyamov
This proposal really didn't make to Lua 5.2: http://lua-users.org/lists/lua-l/2010-07/msg00479.html ?

I liked it a lot, being optional, backwards compatible (minus one extra keyword, of course), and solving a significative part of the problem w/o drastic measures. No need to declare all default library functions. Only add "global xyz;" at the top of the file to enable.
Reply | Threaded
Open this post in threaded view
|

Re: Popularity contest

David Manura
In reply to this post by Krunal Rao
On Mon, Sep 26, 2011 at 1:05 PM, KR <[hidden email]> wrote:
> local min, max, sin, cos = ridx(math, "min, max, sin, cos")

Warning, this is a hack:

local function from(t)
  local j, k
  for i=1, math.huge do
    local name = debug.getlocal(2, i)
    if not name then break end
    if name == '_' then j = i end
    k =  i
  end
  assert(j, 'missing variable `_`')
  for i=j+1, k do
    local name = debug.getlocal(2, i)
    debug.setlocal(2, i, t[name])
  end
end

local _, lower, upper from(string)
local _, sqrt, abs from(math)
local x

assert(sqrt(4) == 2 and abs(-2) == 2)
assert(lower('Abc') == 'abc' and upper('Abc') == 'ABC')
assert(x == nil)

print 'DONE'

Reply | Threaded
Open this post in threaded view
|

Re: Popularity contest

steve donovan
On Tue, Sep 27, 2011 at 3:51 AM, David Manura <[hidden email]> wrote:
> Warning, this is a hack:

That's definitely the hack of the month:  modifying the locals from
the enclosing scope directly!

It looks ungrammatical but the call to from() is actually a separate statement.

steve d.

Reply | Threaded
Open this post in threaded view
|

Re: Popularity contest

Xavier Wang
In reply to this post by Artur Galyamov
2011/9/26 Artur Galyamov <[hidden email]>:

> 26.09.11, 14:43, "Egil Hjelmeland" <[hidden email]>:
>>  Even more generic:
>>
>>     local a,b,c from t
>>
>>  being equivalent to
>>
>>     local a,b,c = t.a, t.b, t.c
>
> If I remember correctly, it was suggested several months ago, but using "in" keyword:
>
>     local min, max, floor in math
>
> No new keywords there. I think it would be nice way to easily localize globals.
>
>

Yes, I like this way, too :)

Reply | Threaded
Open this post in threaded view
|

Re: Popularity contest

Dirk Laurie
On Tue, Sep 27, 2011 at 10:33:40AM +0200, Xavier Wang wrote:
> 2011/9/26 Artur Galyamov <[hidden email]>:
> >     local min, max, floor in math
> >
> > No new keywords there. I think it would be nice way to easily localize
> > globals.
> Yes, I like this way, too :)

All that you really gain from that sort of syntax is that you do not
need to type the names min, max, floor twice, since the support for e.g.

    from = require "from"
    local min, max, floor = from.math "min, max, floor"

is easy to write directly in Lua.  And if you want to use other names
than the ones in the library, you lose.

I can see myself using this proposed construction if it is available,
yes.  But there are so many things I would rather have.  

   1. tuples, i.e. fully internalized immutable lists.
         assert( (1,2,3) == (1,2,3) )
   2. table constructor for keys similar to the one for values, e.g.
         <a,b,c>  is the same as  {a=1,b=2,c=3}
   3. bit rather than bit32 library, operates on numbers or strings
   4. lpeg, lfs to become standard libraries
   5. A table function to go with wish #2:
         table.invert{10,20,30}  is the same as  <10,20,30>
   6. `for k in t do` is OK if non-function 't' has __iter metamethod

Dirk


Reply | Threaded
Open this post in threaded view
|

Re: Popularity contest

steve donovan
On Tue, Sep 27, 2011 at 12:36 PM, Dirk Laurie <[hidden email]> wrote:
>   1. tuples, i.e. fully internalized immutable lists.
>         assert( (1,2,3) == (1,2,3) )

I grant you they are useful, but such functionality can be written in
plain Lua; I use a tuple object originally by David Manura for
structuring tests in Penlight:

asserteq(T(("hello"):find("hell")),T(1,4))

>   4. lpeg, lfs to become standard libraries

Nothing is standard in the Lua universe ;)

>   6. `for k in t do` is OK if non-function 't' has __iter metamethod

I've always liked this one.

steve d.

Reply | Threaded
Open this post in threaded view
|

Re: Popularity contest

Artur Galyamov
In reply to this post by Dirk Laurie
27.09.11, 14:36, "Dirk Laurie" <[hidden email]>:

>  > >     local min, max, floor in math
>  > >
>  > > No new keywords there. I think it would be nice way to easily localize
>  > > globals.
>  > Yes, I like this way, too :)
>  
>  All that you really gain from that sort of syntax is that you do not
>  need to type the names min, max, floor twice, since the support for e.g.
>  
>      from = require "from"
>      local min, max, floor = from.math "min, max, floor"
>  
>  is easy to write directly in Lua.  And if you want to use other names
>  than the ones in the library, you lose.

Agree. But as someone said, simple things should be simple, complex
things should be possible. That's why I love Perl, but embed Lua :]

Reply | Threaded
Open this post in threaded view
|

Re: Popularity contest

Krunal Rao
In reply to this post by Dirk Laurie
On 27 September 2011 11:36, Dirk Laurie <[hidden email]> wrote:
> All that you really gain from that sort of syntax is that you do not
> need to type the names min, max, floor twice, since the support for e.g.
>
>    from = require "from"
>    local min, max, floor = from.math "min, max, floor"
>
> is easy to write directly in Lua.  And if you want to use other names
> than the ones in the library, you lose.

Yes I agree that the only advantage would be saving some typing, but I
find myself frequently copying and pasting this kind of code (and I
guess someone else is doing the same :P) so I think it may be worth
considering as proposal.

I think Lua is in general extremely good at allowing a developer to
produce concise code without repeating itself (I find myself learning
new "patterns" for this each day), and the syntax above could
potentially improve on that.

Cheers
KR