|
On Sun, Dec 6, 2009 at 3:27 PM, Marcus Irven <marcus@marcusirven.com> wrote:Tables can be array-like, dictionary-like, or both, which is always a
> Announcing Underscore.lua -- http://mirven.github.com/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
> (http://documentcloud.github.com/underscore/).
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?
index e30ab1d..2c1dd9b 100644
diff --git a/lib/underscore.lua b/lib/underscore.lua
--- a/lib/underscore.lua@@ -222,6 +222,26 @@ function Underscore.funcs.sort(iter, comparison_func)
+++ b/lib/underscore.lua
return array
end
+-- dictionaries
+
+function Underscore.funcs.assoc(table)
+ local assoc = {}
+ for k,v in pairs(table) do
+ assoc[#assoc+1] = {k,v}
+ end
+ return assoc
+end
+
+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
+end
+
-- arrays
If you're interested, I pushed this+specs to:
http://github.com/sam-github/underscore.lua/commit/5d314b27545566229500f1df6dae986fcb98c4f6
Cheers,
Sam