Chunks is a small functional library for Lua, and a port of the original “Chunks” library written in JavaScript.
The annotated source code is available.
Installation
luarocks build https://raw.github.com/gummesson/chunks.lua/master/chunks-dev-1.rockspec
API
Collections
Chunks.each
Chunks.each(tbl, iterator)
Executes the iterator
on every item in the tbl
.
Examples
local results = {}
Chunks.each({1, 2, 3}, function(v, k)
table.insert(results, v)
end)
--- {1, 2, 3}
local results = {}
Chunks.each({one = 1, two = 2, three = 3}, function(v, k)
table.insert(results, v)
end)
--- {1, 2, 3}
Chunks.map
Chunks.map(tbl, iterator)
Returns a new and transformed table by executing the iterator
on every item in the tbl
.
Examples
Chunks.map({1, 2, 3}, function(v, k)
return v + 1
end)
-- {2, 3, 4}
Chunks.map({one = 1, two = 2, three = 3}, function(v, k)
return v + 1
end)
-- {2, 3, 4}
Chunks.filter
Chunks.filter(tbl, iterator)
Returns a new table by executing the iterator
on every item in the tbl
. Only the items that evalute to true
in the iterator
will be added to the table.
Examples
Chunks.filter({1, 2, 3}, function(v, k)
return v > 1
end)
-- {2, 3}
Chunks.filter({one = 1, two = 2, three = 3}, function(v, k)
return v > 1
end)
-- {2, 3}
Chunks.reduce
Chunks.reduce(tbl, iterator[, memo])
Returns a single value by executing the iterator
on every item in the tbl
and passing the previous value, memo
, to the iterator
.
Examples
Chunks.reduce({1, 2, 3}, function(m, v)
return m + v
end)
-- 6
Chunks.reduce({one = 1, two = 2, three = 3}, function(m, v)
return m + v
end, 1)
-- 7
Chunks.some
Chunks.some(tbl, iterator)
Returns either true
or false
by executing the iterator
on every item in the tbl
. It'll return true
if one of the items in the tbl
matches the iterator
.
Examples
Chunks.some({1, 2, 3}, function(v, k)
return v == 3
end)
-- true
Chunks.some({one = 1, two = 2, three = 3}, function(v, k)
return v > 4
end)
-- false
Chunks.every
Chunks.every(tbl, iterator)
Returns either true
or false
by executing the iterator
on every item in the tbl
. It'll return true
if all of the items in the tbl
matches the iterator
.
Examples
Chunks.every({1, 2, 3}, function(v, k)
return v < 5
end)
-- true
Chunks.every({one = 1, two = 2, three = 3}, function(v, k)
return v > 2
end)
-- false
Chunks.index
Chunks.index(tbl, item)
Returns the index of the item
in the tbl
.
Examples
Chunks.index({1, 2, 3}, 2)
-- 2
Chunks.index({one = 1, two = 2, three = 3}, 4)
-- nil
Chunks.contains
Chunks.contains(tbl, item)
Determines if the tbl
contains the item
. Returns either true
or false
.
Examples
Chunks.contains({1, 2, 3}, 2)
-- true
Chunks.contains({one = 1, two = 2, three = 3}, 4)
-- false
Chunks.size
Chunks.size(tbl)
Returns the size of the tbl
.
Examples
Chunks.size({1, 2, 3})
-- 3
Chunks.size({one = 1, two = 2, three = 3})
-- 3
Chunks.clone
Chunks.clone(tbl)
Returns a clone of the tbl
.
Examples
Chunks.clone({1, 2, 3})
-- {1, 2, 3}
Chunks.clone({one = 1, two = 2, three = 3})
-- {one = 1, two = 2, three = 3}
Chunks.sort
Chunks.sort(tbl[, comp])
Returns a new table with the values from the tbl
sorted, either by the default order or the comp
function.
Examples
Chunks.sort({1, 3, 2})
-- {1, 2, 3}
Chunks.sort({one = 1, two = 2, three = 3}, function(a, b)
return a > b
end)
-- {3, 2, 1}
Types
Chunks.isArray
Chunks.isArray(tbl)
Determines if the tbl
is array-like. Returns either true
or false
.
Examples
Chunks.isArray({1, 2, 3})
-- true
Chunks.isArray({one = 1, two = 2, three = 3})
-- false
Chunks.isDict
Chunks.isDict(tbl)
Determines if the tbl
is dictionary-like. Returns either true
or false
.
Examples
Chunks.isDict({one = 1, two = 2, three = 3})
-- true
Chunks.isDict({1, 2, 3})
-- false
Arrays
Chunks.first
Chunks.first(tbl)
Returns the first item in the tbl
.
Example
Chunks.first({1, 2, 3})
-- 1
Chunks.last
Chunks.last(tbl)
Returns the last item in the tbl
.
Example
Chunks.last({1, 2, 3})
-- 3
Chunks.flatten
Chunks.flatten(tbl[, memo])
Returns a new table by flattening the nested tables in the tbl
. If the memo
is provided it'll get used as a starting point for the new table.
Examples
Chunks.flatten({{1}, {{2}, {3}}})
-- {1, 2, 3}
Chunks.flatten({{2}, {{3}, {4}}}, {1})
-- {1, 2, 3, 4}
Dictionaries
Chunks.keys
Chunks.keys(tbl)
Returns a new table with keys from the tbl
.
Example
Chunks.keys({one = 1, two = 2, three = 3})
-- {'one', 'two', 'three'}
Chunks.values
Chunks.values(tbl)
Returns a new table with values from the tbl
.
Example
Chunks.values({one = 1, two = 2, three = 3})
-- {1, 2, 3}
Chunks.has
Chunks.has(tbl, key)
Determines if the tbl
has the key
. Returns either true
or false
.
Example
Chunks.has({one = 1, two = 2, three = 3}, 'two')
-- true
Chunks.extend
Chunks.extend(tbl, src)
Merges the properties in the tbl
together with the properties in the src
.
Example
Chunks.extend({one = 1, two = 2, three = 3}, {three = 4})
-- {one = 1, two = 2, three = 4}