Skip to content

mikelovesrobots/lua-enumerable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

lua-enumerable is a quick and dirty port of the ruby Enumerable library plus some extras.

It's a native lua library.  Installation is as simple as dropping lua-enumerable.lua in 
your project and requiring it:

    require("lua-enumerable")

Synopsis:

    local test = {1,2,3}

    table.each(test, function (x) print(x) end)
    -- 1
    -- 2
    -- 3

	local holes = {1,2,7}
	table.each(holes, function(x) print(x) end)
	-- 1 2 7
	table.every(holes, function(x) print(x) end)
	-- 1 2 7
	holes["1"] = "one" 
	holes["2"] = "two"
	holes["7"] = "seven"
	table.every(holes, function(x) print(x) end)
	-- 1 2 7 one seven two
	--BUT!
	table.each(holes, function(x) print(x) end)
	--1 2 7

    table.collect(test, function (x) return x + 1 end)
    -- {2, 3, 4}

    table.select(test, function (x) return x < 3 end)
    -- {1, 2}

    table.reject(test, function (x) return x < 3 end)
    -- {3}
    
    table.without(test, 3)
    -- {1, 2}

    table.partition(test, function (x) return x < 3 end)
    -- {1, 2}, {3}

    table.includes(test, 3)
    -- true

    table.detect(test, function (x) return x == 3 end)
    -- 3

    table.detect(test, function (x) return x == 4 end)
    -- nil

    table.merge({a=1, b=2}, {c=3})
    -- {a=1, b=2, c=3}
    -- Note: modifies the first table

    table.times(4, function (x) print(x) end)
    -- 1
    -- 2
    -- 3
    -- 4

    table.keys({a=1,b=2,c=3})
    -- {"a", "c", "b"}

Provides some array manipulation functions:

    table.pop(test)
    -- 3
    -- Note: modifies the input array {1,2}
     
    table.shift(test)
    -- 1
    -- Note: modifies the input array {2,3}

    table.unshift(test, 4)
    -- {4, 1, 2, 3}
    -- Note: modifies the input array

    table.push(test, 4)
    -- {1, 2, 3, 4}
    -- Note: modifies the input array

    table.reverse({1,2,3})
    -- {3,2,1}

Random elements:

    table.random(test)
    -- {3}
    
    table.shuffle(test)
    -- {3,2,1}
    -- Note: modifies the input table

Boolean tests for sexy if statements:

    table.empty({})
    -- true
    
    table.empty(nil)
    -- true

    table.empty({1,2,3})
    -- false

    table.present({})
    -- false
    
    table.present(nil)
    -- false

    table.present({1,2,3})
    -- true

Misc:

    table.dup({1,2,3})
    -- {1,2,3}


If you've got any useful functions or suggestions, send it to me via a pull request,
email ([email protected]) or paste into an issue here in github.  (The transmission
method doesn't really matter.  What matters is that you're being awesome.) 

Thanks for checking out my humble little library.

About

A port of ruby's Enumerable module to Lua

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages