lua-users home
lua-l archive

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


On Mon, Dec 7, 2009 at 1:27 AM, Marcus Irven <marcus@marcusirven.com> wrote:
> 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.

Nice, a well-chosen set of functionalities. I like how you've provided
some aliases.

I quote:

_.({1,2,3,4}):chain():map(function(i) return i+1
end):select(function(i) i%2 == 0 end):value()

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.

Second, support for 'string lambdas' would be cool:

_.({1,2,3,4}):chain():map '|i| i + 1' :select '|i| i%2==0': value()

There's a slight hit to compile them, but thereafter they can be
easily memoized. They don't require any syntax enhancements so David K
can only have style objections ;)

> - I'd like a nice function for dumping any object as well as a good table
> equality function

As for table comparison, this is the one I use in Penlight:

function deepcompare(t1,t2,ignore_mt)
    local ty1 = type(t1)
    local ty2 = type(t2)
    if ty1 ~= ty2 then return false end
    -- non-table types can be directly compared
    if ty1 ~= 'table' and ty2 ~= 'table' then return t1 == t2 end
    -- as well as tables which have the metamethod __eq
    local mt = getmetatable(t1)
    if not ignore_mt and mt and mt.__eq then return t1 == t2 end
    for k1,v1 in pairs(t1) do
        local v2 = t2[k1]
        if v2 == nil or not deepcompare(v1,v2) then return false end
    end
    for k2,v2 in pairs(t2) do
        local v1 = t1[k2]
        if v1 == nil or not deepcompare(v1,v2) then return false end
    end
    return true
end

steve d.