lua-users home
lua-l archive

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


On Tue, Dec 8, 2009 at 3:24 PM, Marcus Irven <marcus@marcusirven.com> wrote:
> On Tue, Dec 8, 2009 at 10:44 AM, steve donovan <steve.j.donovan@gmail.com>
>> First random observation: Lua people tend to use '_' as a throwaway
>> variable, in cases like 'local _,i2 = s:find('%s+')'.  But it can
>> always be aliased if necessary.
>>
>
> I thought of this as I use '_' for throwaway variables myself. I couldn't
> think of anything better though and since this is basically a rip off of the
> javascript library I went with it. Plus as you pointed out you can use "__ =
> _:no_conflict()" or whatever.

Nice library, thanks.

We use _ extensively for throw-away variables, too, and I'd like it to
default to conflict-less.

Also, when I require"underscore", I expect to see underscore as a
table in the global environment, and to be able to make shorthand
local names like:

local u = require"underscore"

Anyhow, I forked and patched it (see
http://github.com/sam-github/underscore.lua):

underscore.git % git diff lib
diff --git a/lib/underscore.lua b/lib/underscore.lua
index 5387e17..6bbab00 100644
--- a/lib/underscore.lua
+++ b/lib/underscore.lua
@@ -24,8 +24,6 @@
 --- Underscore is a set of utility functions for dealing with
 -- iterators, arrays, tables, and functions.

-local previous_underscore = _
-
 local Underscore = { funcs = {} }
 Underscore.__index = Underscore

@@ -57,11 +55,6 @@ function Underscore.identity(value)
        return value
 end

-function Underscore:no_conflict()
-  _ = previous_underscore
-  return self
-end
-
 -- chaining

 function Underscore:chain()
@@ -402,4 +395,7 @@ end

 wrap_functions_for_oo_support()

-_ = Underscore:new()
\ No newline at end of file
+underscore = Underscore:new()
+
+return underscore
+