18 Commits

Author SHA1 Message Date
rxi
c6ab30cf7d Version 2.0.0 2015-01-24 15:21:35 +00:00
rxi
c07194490b Updated license copyright year 2015-01-24 15:20:58 +00:00
rxi
5305cb34c8 Lua 5.3 compatibility fixes 2015-01-12 19:51:24 +00:00
rxi
f7b3eb64bb Fixed typo in lume.sort()'s README section 2015-01-11 15:27:06 +00:00
rxi
edd7c99382 Added lume.ripairs() function, updated README and tests 2015-01-11 14:58:57 +00:00
rxi
c773acdcf4 Removed retainkeys argument from lume.set() 2015-01-11 14:33:56 +00:00
rxi
756d30416d Added lume.wordwrap(); updated README.md and tests 2015-01-10 21:53:52 +00:00
rxi
9b5abf3d58 Changed lume.merge() functionality, updated README and doc
- The old functionality of lume.merge() is now spread across
  lume.merge() (for tables) and lume.concat() (for arrays). Both these
  functions create new tables rather than operating in-place
- Changed lume.fn() to use lume.concat() internally
2015-01-10 17:31:35 +00:00
rxi
397dce4c5e Added lume.concat(), updated README and tests 2015-01-10 17:13:15 +00:00
rxi
ce86f4b0ea Fixed nil iterator in internal getiter() 2015-01-10 17:08:14 +00:00
rxi
c565f0739b Fixed lume.call() section of README.md 2015-01-10 16:56:32 +00:00
rxi
7a0f5a0831 Added lume.sort(), updated tests & README 2015-01-10 16:54:23 +00:00
rxi
b873c043c5 Added lume.call(), updated tests and README 2015-01-10 16:29:13 +00:00
rxi
4373a202dc Changed lume.shuffle() to not operate in-place, updated doc + tests 2015-01-10 16:24:31 +00:00
rxi
ac10d54b47 Changed internal func isarray() to handle nil 2015-01-10 14:32:58 +00:00
rxi
067e58b30f Changed lume.count() to return length if table is array 2015-01-10 14:31:55 +00:00
rxi
a42cbb12f0 Changed all applicable functions to use iter func based on table 2015-01-10 14:23:25 +00:00
rxi
0c21cfcf5b Updated lume.each() to use iter func based on table type 2015-01-10 14:13:07 +00:00
4 changed files with 252 additions and 42 deletions

View File

@@ -1,4 +1,4 @@
Copyright (c) 2014, rxi
Copyright (c) 2015, rxi
Permission is hereby granted, free of charge, to any person obtaining a copy of

View File

@@ -75,7 +75,17 @@ lume.weightedchoice({ ["cat"] = 10, ["dog"] = 5, ["frog"] = 0 })
```
### lume.shuffle(t)
Shuffles the values of array `t` in place, returns the array.
Returns a shuffled copy of the array `t`.
### lume.sort(t [, comp])
Returns a copy of the array `t` with all its items sorted. If `comp` is a
function it will be used to compare the items when sorting. If `comp` is a
string it will be used as the key to sort the items by.
```lua
lume.sort({ 1, 4, 3, 2, 5 }) -- Returns { 1, 2, 3, 4, 5 }
lume.sort({ {z=2}, {z=3}, {z=1} }, "z") -- Returns { {z=1}, {z=2}, {z=3} }
lume.sort({ 1, 3, 2 }, function(a, b) return a > b end) -- Returns { 3, 2, 1 }
```
### lume.array(...)
Iterates the supplied iterator and returns an array filled with the values.
@@ -125,10 +135,8 @@ an error is raised,
lume.reduce({1, 2, 3}, function(a, b) return a + b end) -- Returns 6
```
### lume.set(t [, retainkeys])
Returns a copy of the `t` table with all the duplicate values removed. If
`retainkeys` is true the table is not treated as an array and retains its
original keys.
### lume.set(t)
Returns a copy of the `t` array with all the duplicate values removed.
```lua
lume.set({2, 1, 2, "cat", "cat"}) -- Returns {1, 2, "cat"}
```
@@ -141,12 +149,17 @@ an array and retains its original keys.
lume.filter({1, 2, 3, 4}, function(x) return x % 2 == 0 end) -- Returns {2, 4}
```
### lume.merge(t, t2 [, retainkeys])
Merges all the values from the table `t2` into `t` in place. If `retainkeys` is
true the table is not treated as an array and retains its original keys; if `t`
and `t2` have a conflicting key, the value from `t2` is used.
### lume.merge(...)
Returns a new table with all the given tables merged together. If a key exists
in multiple tables the right-most table's value is used.
```lua
lume.merge({2, 3}, {4, 5}) -- Returns {2, 3, 4, 5}
lume.merge({a=1, b=2, c=3}, {c=8, d=9}) -- Returns {a=1, b=2, c=8, d=9}
```
### lume.concat(...)
Returns a new array consisting of all the given arrays concatenated into one.
```lua
lume.concat({1, 2}, {3, 4}, {5, 6}) -- Returns {1, 2, 3, 4, 5, 6}
```
### lume.find(t, value)
@@ -244,6 +257,13 @@ local f = lume.combine(function(a, b) print(a + b) end,
f(3, 4) -- Prints "7" then "12" on a new line
```
### lume.call(fn, ...)
Calls the given function with the provided arguments and returns its values. If
`fn` is `nil` then no action is performed and the function returns `nil`.
```lua
lume.call(print, "Hello world") -- Prints "Hello world"
```
### lume.time(fn, ...)
Inserts the arguments into function `fn` and calls it. Returns the time in
seconds the function `fn` took to execute followed by `fn`'s returned values.
@@ -294,6 +314,15 @@ instead of whitespace.
lume.trim(" Hello ") -- Returns "Hello"
```
### lume.wordwrap(str [, limit])
Returns `str` wrapped to `limit` number of characters per line, by default
`limit` is `72`. `limit` can also be a function which when passed a string,
returns `true` if it is too long for a single line.
```lua
-- Returns "Hello world\nThis is a\nshort string"
lume.wordwrap("Hello world. This is a short string", 14)
```
### lume.format(str [, vars])
Returns a formatted string. The values of keys in the table `vars` can be
inserted into the string by using the form `"{key}"` in `str`; numerical keys
@@ -332,6 +361,17 @@ lume.hotswap("lume") -- Reloads the lume module
assert(lume.hotswap("inexistant_module")) -- Raises an error
```
### lume.ripairs(t)
Performs the same function as `ipairs()` but iterates in reverse; this allows
the removal of items from the table during iteration without any items being
skipped.
```lua
-- Prints "3->c", "2->b" and "1->a" on separate lines
for i, v in lume.ripairs({ "a", "b", "c" }) do
print(i .. "->" .. v)
end
```
### lume.rgba(color)
Takes the 32bit integer `color` argument and returns 4 numbers, one for each
channel, with a range of 0 - 255. The returned values can be used as the

175
lume.lua
View File

@@ -1,13 +1,13 @@
--
-- lume
--
-- Copyright (c) 2014, rxi
-- Copyright (c) 2015, rxi
--
-- This library is free software; you can redistribute it and/or modify it
-- under the terms of the MIT license. See LICENSE for details.
--
local lume = { _version = "1.5.0" }
local lume = { _version = "2.0.0" }
local pairs, ipairs = pairs, ipairs
local type, assert, unpack = type, assert, unpack or table.unpack
@@ -16,11 +16,18 @@ local math_floor = math.floor
local math_ceil = math.ceil
local math_random = math.random
local math_cos = math.cos
local math_atan2 = math.atan2
local math_atan2 = math.atan2 or math.atan
local math_sqrt = math.sqrt
local math_abs = math.abs
local math_pi = math.pi
local noop = function()
end
local identity = function(x)
return x
end
local patternescape = function(str)
return str:gsub("[%(%)%.%%%+%-%*%?%[%]%^%$]", "%%%1")
end
@@ -35,8 +42,22 @@ local iscallable = function(x)
return mt and mt.__call ~= nil
end
local identity = function(x)
return x
local isarray = function(x)
return (x and x[1]) and true or false
end
local iternil = function()
return noop
end
local getiter = function(x)
if x == nil then
return iternil
elseif isarray(x) then
return ipairs
else
return pairs
end
end
local iteratee = function(x)
@@ -129,11 +150,30 @@ end
function lume.shuffle(t)
local rtn = {}
for i = 1, #t do
local r = math_random(#t)
t[i], t[r] = t[r], t[i]
local r = math_random(i)
if r ~= i then
rtn[i] = rtn[r]
end
rtn[r] = t[i]
end
return t
return rtn
end
function lume.sort(t, comp)
local rtn = lume.clone(t)
if comp then
if type(comp) == "string" then
table.sort(rtn, function(a, b) return a[comp] < b[comp] end)
else
table.sort(rtn, comp)
end
else
table.sort(rtn)
end
return rtn
end
@@ -145,10 +185,11 @@ end
function lume.each(t, fn, ...)
local iter = getiter(t)
if type(fn) == "string" then
for _, v in pairs(t) do v[fn](v, ...) end
for _, v in iter(t) do v[fn](v, ...) end
else
for _, v in pairs(t) do fn(v, ...) end
for _, v in iter(t) do fn(v, ...) end
end
return t
end
@@ -156,15 +197,17 @@ end
function lume.map(t, fn)
fn = iteratee(fn)
local iter = getiter(t)
local rtn = {}
for k, v in pairs(t) do rtn[k] = fn(v) end
for k, v in iter(t) do rtn[k] = fn(v) end
return rtn
end
function lume.all(t, fn)
fn = iteratee(fn)
for k, v in pairs(t) do
local iter = getiter(t)
for k, v in iter(t) do
if not fn(v) then return false end
end
return true
@@ -173,7 +216,8 @@ end
function lume.any(t, fn)
fn = iteratee(fn)
for k, v in pairs(t) do
local iter = getiter(t)
for k, v in iter(t) do
if fn(v) then return true end
end
return false
@@ -188,10 +232,10 @@ function lume.reduce(t, fn, first)
end
function lume.set(t, retainkeys)
function lume.set(t)
local rtn = {}
for k, v in pairs(lume.invert(t)) do
rtn[retainkeys and v or (#rtn + 1)] = k
rtn[#rtn + 1] = k
end
return rtn
end
@@ -199,24 +243,44 @@ end
function lume.filter(t, fn, retainkeys)
fn = iteratee(fn)
local iter = getiter(t)
local rtn = {}
for k, v in pairs(t) do
for k, v in iter(t) do
if fn(v) then rtn[retainkeys and k or (#rtn + 1)] = v end
end
return rtn
end
function lume.merge(t, t2, retainkeys)
for k, v in pairs(t2) do
t[retainkeys and k or (#t + 1)] = v
function lume.merge(...)
local rtn = {}
for i = 1, select("#", ...) do
local t = select(i, ...)
local iter = getiter(t)
for k, v in iter(t) do
rtn[k] = v
end
end
return t
return rtn
end
function lume.concat(...)
local rtn = {}
for i = 1, select("#", ...) do
local t = select(i, ...)
local iter = getiter(t)
for k, v in iter(t) do
rtn[#rtn + 1] = v
end
end
return rtn
end
function lume.find(t, value)
for k, v in pairs(t) do
local iter = getiter(t)
for k, v in iter(t) do
if v == value then return k end
end
return nil
@@ -225,7 +289,8 @@ end
function lume.match(t, fn)
fn = iteratee(fn)
for k, v in pairs(t) do
local iter = getiter(t)
for k, v in iter(t) do
if fn(v) then return v, k end
end
return nil
@@ -234,13 +299,17 @@ end
function lume.count(t, fn)
local count = 0
local iter = getiter(t)
if fn then
fn = iteratee(fn)
for k, v in pairs(t) do
for k, v in iter(t) do
if fn(v) then count = count + 1 end
end
else
for k in pairs(t) do count = count + 1 end
if isarray(t) then
return #t
end
for k in iter(t) do count = count + 1 end
end
return count
end
@@ -278,7 +347,8 @@ end
function lume.keys(t)
local rtn = {}
for k, v in pairs(t) do rtn[#rtn + 1] = k end
local iter = getiter(t)
for k, v in iter(t) do rtn[#rtn + 1] = k end
return rtn
end
@@ -292,9 +362,9 @@ end
function lume.fn(fn, ...)
assert(iscallable(fn), "expected a function as the first argument")
local args = {...}
local args = { ... }
return function(...)
local a = lume.merge(lume.clone(args), {...})
local a = lume.concat(args, { ... })
return fn(unpack(a))
end
end
@@ -344,6 +414,13 @@ function lume.combine(...)
end
function lume.call(fn, ...)
if fn then
return fn(...)
end
end
function lume.time(fn, ...)
local start = os.clock()
local rtn = {fn(...)}
@@ -403,6 +480,32 @@ function lume.trim(str, chars)
end
function lume.wordwrap(str, limit)
limit = limit or 72
local check
if type(limit) == "number" then
check = function(str) return #str >= limit end
else
check = limit
end
local rtn = {}
for j, line in ipairs(lume.split(str, "\n")) do
local str
for i, word in ipairs(lume.split(line)) do
local s = (str and (str .. " ") or "") .. word
if check(s) then
rtn[#rtn + 1] = str
str = word
else
str = s
end
end
rtn[#rtn + 1] = str
end
return table.concat(rtn, "\n")
end
function lume.format(str, vars)
if not vars then return str end
local f = function(x)
@@ -417,7 +520,9 @@ function lume.trace(...)
local t = { "[" .. info.short_src .. ":" .. info.currentline .. "]" }
for i = 1, select("#", ...) do
local x = select(i, ...)
x = (type(x) == "number") and lume.round(x, .01) or (x or "nil")
if type(x) == "number" then
x = string.format("%g", lume.round(x, .01))
end
t[#t + 1] = tostring(x)
end
print(table.concat(t, " "))
@@ -475,6 +580,20 @@ function lume.hotswap(modname)
end
local ripairs_iter = function(t, i)
i = i - 1
local v = t[i]
if v then return i, v end
end
function lume.ripairs(t)
if t == nil then
return noop
end
return ripairs_iter, t, (#t + 1)
end
function lume.rgba(color)
local a = math_floor((color / 16777216) % 256)
local r = math_floor((color / 65536) % 256)

View File

@@ -109,12 +109,24 @@ end
-- lume.shuffle
tests["lume.shuffle"] = function()
local t = {1, 2, 3, 4, 5}
lume.shuffle(t)
t = lume.shuffle(t)
table.sort(t)
testeq( t, {1, 2, 3, 4, 5} )
testeq( lume.shuffle({}), {} )
end
-- lume.sort
tests["lume.sort"] = function()
local t = { 1, 5, 2, 4, 3 }
local fn = function(a, b) return a > b end
testeq( t == lume.sort(t), false )
testeq( lume.sort(t), { 1, 2, 3, 4, 5 } )
testeq( lume.sort(t, fn), { 5, 4, 3, 2, 1 } )
testeq( t, { 1, 5, 2, 4, 3 } )
local t = { { id = 2 }, { id = 3 }, { id = 1 } }
testeq( lume.sort(t, "id"), { { id = 1 }, { id = 2 }, { id = 3 } })
end
-- lume.array
tests["lume.array"] = function()
local t = lume.array(pairs({a=0, b=0, c=0}))
@@ -204,8 +216,17 @@ end
-- lume.merge
tests["lume.merge"] = function()
testeq( lume.merge({1, 2, 3}, {8, 9, 0}), {1, 2, 3, 8, 9, 0} )
testeq( lume.merge({a=1, b=2}, {b=3, c=4}, true), {a=1, b=3, c=4} )
testeq( lume.merge(), {} )
testeq( lume.merge({x=1, y=2}), {x=1, y=2} )
testeq( lume.merge({a=1, b=2}, {b=3, c=4}), {a=1, b=3, c=4} )
end
-- lume.concat
tests["lume.concat"] = function()
testeq( lume.concat(nil), {} )
testeq( lume.concat({1, 2, 3}), {1, 2, 3} )
testeq( lume.concat({1, 2, 3}, {4, 5, 6}), {1, 2, 3, 4, 5, 6} )
testeq( lume.concat({1, 2, 3}, {4, 5, 6}, nil, {7}), {1, 2, 3, 4, 5, 6, 7} )
end
-- lume.find
@@ -351,6 +372,14 @@ tests["lume.combine"] = function()
testeq( acc, 230 )
end
-- lume.call
tests["lume.call"] = function()
local add = function(a, b) return a + b end
testeq( lume.call(), nil )
testeq( lume.call(nil, 1, 2, 3), nil )
testeq( lume.call(add, 1, 2), 3 )
end
-- lume.time
tests["lume.time"] = function()
local t, a, b, c = lume.time(function(x) return 50, 60, x end, 70)
@@ -402,6 +431,16 @@ tests["lume.trim"] = function()
testeq( lume.trim("^.hello world]^", "^.]"), "hello world" )
end
-- lume.wordwrap
tests["lume.wordwrap"] = function()
local str = "A small string with some words and then some more words"
local b = "A small string with\nsome words and then\nsome more words"
local fn = function(str) return #str >= 20 end
testeq( lume.wordwrap(str), str )
testeq( lume.wordwrap(str, 20), b )
testeq( lume.wordwrap(str, fn), b )
end
-- lume.format
tests["lume.format"] = function()
local str = lume.format("a {a} in a {b}", {a = "mouse", b = "house"})
@@ -447,6 +486,18 @@ tests["lume.hotswap"] = function()
testeq( type(err), "string" )
end
-- lume.ripairs
tests["lume.ripairs"] = function()
local t = { "a", "b", "c" }
local r = {}
for i, v in lume.ripairs(t) do
table.insert(r, { i, v })
end
testeq( r, { { 3, "c" }, { 2, "b" }, { 1, "a" } })
for i, v in lume.ripairs(nil) do
end
end
-- lume.rgba
tests["lume.rgba"] = function()
local r, g, b, a = lume.rgba(0x12345678)