lua-users home
lua-l archive

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

On Sun, Dec 6, 2009 at 3:27 PM, Marcus Irven <> wrote:
> Announcing Underscore.lua --
> Underscore.lua is a utility library with 30+ (and growing) functions that
> can be used in a functional or object-oriented manner. It is more than a
> little inspired by Underscore.js
> (

Tables can be array-like,  dictionary-like, or both, which is always a
problem with utility libraries.

Underscore mostly focuses on the  arrayish part of tables, I wonder if
some methods to convert tables in and out of an array representation
would be useful, so that the other methods can then be applied to the
entire table, not just the array part?

diff --git a/lib/underscore.lua b/lib/underscore.lua
index e30ab1d..2c1dd9b 100644
--- a/lib/underscore.lua
+++ b/lib/underscore.lua
@@ -222,6 +222,26 @@ function Underscore.funcs.sort(iter, comparison_func)
        return array

+-- dictionaries
+function Underscore.funcs.assoc(table)
+       local assoc = {}
+       for k,v in pairs(table) do
+               assoc[#assoc+1] = {k,v}
+       end
+       return assoc
+function Underscore.funcs.unassoc(assoc)
+       local table = {}
+       local k, v
+       for _,pair in ipairs(assoc) do
+               k, v = pair[1], pair[2]
+               table[k] = v
+       end
+       return table
 -- arrays

If you're interested, I pushed this+specs to: