15 Commits

Author SHA1 Message Date
Rose Liverman
e2a9e009af Merge branch 'rxi:master' into master 2025-07-20 17:53:40 -06:00
rxi
98847e7812 Updated copyright year; 2018 => 2020 2020-02-12 21:11:41 +00:00
rxi
6389f859c6 Made lume.reduce check initial value for nil instead of non-truthy
Fixes #32
2020-02-12 21:09:33 +00:00
rxi
d8c2eddc10 Merge pull request #30 from jaythomas/cleanup
Remove unnecessary expression; Add test
2019-03-14 23:10:46 +00:00
Jay Thomas
9e0f56e3e9 Remove unnecessary expression; Add test
First half of the expression returns a boolean already so the second
half is redundant
2019-03-08 09:58:40 -05:00
Paul Liverman III
c3089b0eb0 upd readme 2018-04-23 02:43:11 -07:00
Paul Liverman III
fb7b826942 added seed() fn, corrected err with local math_random 2018-04-23 02:41:29 -07:00
Paul Liverman III
0eccde530a upd version / ReadMe 2018-04-23 02:35:20 -07:00
Paul Liverman III
cea8f76bb3 lume uses love.math.random if available 2018-04-23 02:33:27 -07:00
Paul Liverman III
78805a5e42 using a local for math.random calls 2018-04-23 02:31:26 -07:00
rxi
0980d07eaa Fixed lume.ripairs() for falsey values; added test 2018-04-08 15:22:13 +01:00
rxi
09035882f1 Updated copyright year, moved full license to lume.lua 2018-03-10 15:30:04 +00:00
rxi
2b10ce1f98 Fixed README punctuation for lume.reduce 2018-03-10 14:57:56 +00:00
rxi
64aae8d473 Renamed lume.set -> lume.unique 2018-03-10 14:54:27 +00:00
rxi
758067dd33 Removed lume.rgba (superseded by lume.color) 2018-03-10 14:52:02 +00:00
4 changed files with 59 additions and 50 deletions

View File

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

View File

@@ -2,6 +2,10 @@
A collection of functions for Lua, geared towards game development.
This fork incorporates randomization from the LOVE engine's math library if
available, uses a local for random calls, and adds a function for setting the
seed for its randomizer.
## Installation
@@ -58,6 +62,11 @@ Given an `angle` and `magnitude`, returns a vector.
local x, y = lume.vector(0, 10) -- Returns 10, 0
```
#### lume.seed([low [, high]])
Sets random seed. If using LOVE engine's math library, both `low` and `high`
arguments are used, else, only one argument is accepted.
#### lume.random([a [, b]])
Returns a random number between `a` and `b`. If only `a` is supplied a number
between `0` and `a` is returned. If no arguments are supplied a random number
@@ -175,15 +184,15 @@ Applies `fn` on two arguments cumulative to the items of the array `t`, from
left to right, so as to reduce the array to a single value. If a `first` value
is specified the accumulator is initialised to this, otherwise the first value
in the array is used. If the array is empty and no `first` value is specified
an error is raised,
an error is raised.
```lua
lume.reduce({1, 2, 3}, function(a, b) return a + b end) -- Returns 6
```
#### lume.set(t)
#### lume.unique(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"}
lume.unique({2, 1, 2, "cat", "cat"}) -- Returns {1, 2, "cat"}
```
#### lume.filter(t, fn [, retainkeys])
@@ -442,14 +451,6 @@ lume.color("#00ffff", 256) -- Returns 0, 256, 256, 256
lume.color("rgb(255, 0, 0)", 256) -- Returns 256, 0, 0, 256
```
#### 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
arguments to [LÖVE](http://love2d.org)'s setColor() function.
```lua
lume.rgba(0xFF304050) -- Returns 48, 64, 80, 255
```
#### lume.chain(value)
Returns a wrapped object which allows chaining of lume functions. The function
result() should be called at the end of the chain to return the resulting

View File

@@ -1,13 +1,28 @@
--
-- lume
--
-- Copyright (c) 2017 rxi
-- Copyright (c) 2020 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.
-- Permission is hereby granted, free of charge, to any person obtaining a copy of
-- this software and associated documentation files (the "Software"), to deal in
-- the Software without restriction, including without limitation the rights to
-- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
-- of the Software, and to permit persons to whom the Software is furnished to do
-- so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in all
-- copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
--
local lume = { _version = "2.3.0" }
local lume = { _version = "2.4.0-Guard13007-fork" }
local pairs, ipairs = pairs, ipairs
local type, assert, unpack = type, assert, unpack or table.unpack
@@ -17,6 +32,8 @@ local math_ceil = math.ceil
local math_atan2 = math.atan2 or math.atan
local math_sqrt = math.sqrt
local math_abs = math.abs
local math_random = love and love.math and love.math.random or math.random
local math_randomseed = love and love.math and love.math.setRandomSeed or math.randomseed
local noop = function()
end
@@ -115,15 +132,20 @@ function lume.vector(angle, magnitude)
end
function lume.seed(low, high)
return math_randomseed(low, high)
end
function lume.random(a, b)
if not a then a, b = 0, 1 end
if not b then b = 0 end
return a + math.random() * (b - a)
return a + math_random() * (b - a)
end
function lume.randomchoice(t)
return t[math.random(#t)]
return t[math_random(#t)]
end
@@ -143,7 +165,7 @@ end
function lume.isarray(x)
return (type(x) == "table" and x[1] ~= nil) and true or false
return type(x) == "table" and x[1] ~= nil
end
@@ -198,7 +220,7 @@ end
function lume.shuffle(t)
local rtn = {}
for i = 1, #t do
local r = math.random(i)
local r = math_random(i)
if r ~= i then
rtn[i] = rtn[r]
end
@@ -271,8 +293,8 @@ end
function lume.reduce(t, fn, first)
local started = first ~= nil
local acc = first
local started = first and true or false
local iter = getiter(t)
for _, v in iter(t) do
if started then
@@ -287,7 +309,7 @@ function lume.reduce(t, fn, first)
end
function lume.set(t)
function lume.unique(t)
local rtn = {}
for k in pairs(lume.invert(t)) do
rtn[#rtn + 1] = k
@@ -662,7 +684,7 @@ end
function lume.uuid()
local fn = function(x)
local r = math.random(16) - 1
local r = math_random(16) - 1
r = (x == "x") and (r + 1) or (r % 4) + 9
return ("0123456789abcdef"):sub(r, r)
end
@@ -709,7 +731,9 @@ end
local ripairs_iter = function(t, i)
i = i - 1
local v = t[i]
if v then return i, v end
if v ~= nil then
return i, v
end
end
function lume.ripairs(t)
@@ -739,15 +763,6 @@ function lume.color(str, mul)
end
function lume.rgba(color)
local a = math_floor((color / 16777216) % 256)
local r = math_floor((color / 65536) % 256)
local g = math_floor((color / 256) % 256)
local b = math_floor((color) % 256)
return r, g, b, a
end
local chain_mt = {}
chain_mt.__index = lume.map(lume.filter(lume, iscallable, true),
function(fn)

View File

@@ -141,7 +141,6 @@ tests["lume.remove"] = function()
testeq(t, { 2, 4, 5 })
lume.remove(t, 5)
testeq(t, { 2, 4 })
local m = { a = 1, b = 2, c = 3 }
local x = lume.remove(t, 123)
testeq(x, 123)
end
@@ -245,6 +244,7 @@ end
tests["lume.reduce"] = function()
local concat = function(a, b) return a .. b end
local add = function(a, b) return a + b end
local any = function(a, b) return a or b end
testeq( lume.reduce({"cat", "dog"}, concat, ""), "catdog" )
testeq( lume.reduce({"cat", "dog"}, concat, "pig"), "pigcatdog" )
testeq( lume.reduce({"me", "ow"}, concat), "meow" )
@@ -254,16 +254,18 @@ tests["lume.reduce"] = function()
testeq( lume.reduce({}, concat, "potato"), "potato" )
testeq( lume.reduce({a=1, b=2}, add, 5), 8 )
testeq( lume.reduce({a=1, b=2}, add), 3 )
testeq( lume.reduce({false, false, false}, any), false )
testeq( lume.reduce({false, true, false}, any), true )
tester.test.error(lume.reduce, {}, add)
end
-- lume.set
tests["lume.set"] = function()
testeq( lume.set({}), {} )
local t = lume.set({1, 2, 3, 2, 5, 6, 6})
-- lume.unique
tests["lume.unique"] = function()
testeq( lume.unique({}), {} )
local t = lume.unique({1, 2, 3, 2, 5, 6, 6})
table.sort(t)
testeq( t, {1, 2, 3, 5, 6} )
local t = lume.set({"a", "b", "c", "b", "d"})
local t = lume.unique({"a", "b", "c", "b", "d"})
table.sort(t)
testeq( t, {"a", "b", "c", "d"} )
end
@@ -581,12 +583,12 @@ end
-- lume.ripairs
tests["lume.ripairs"] = function()
local t = { "a", "b", "c" }
local t = { "a", "b", false, "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" } })
testeq( r, { { 4, "c" }, { 3, false }, { 2, "b" }, { 1, "a" } })
tester.test.error(lume.ripairs, nil)
end
@@ -607,15 +609,6 @@ tests["lume.color"] = function()
tester.test.error(lume.color, "rgba(1, 1, 1, 1")
end
-- lume.rgba
tests["lume.rgba"] = function()
local r, g, b, a = lume.rgba(0x12345678)
testeq( a, 0x12 )
testeq( r, 0x34 )
testeq( g, 0x56 )
testeq( b, 0x78 )
end
-- lume.chain
tests["lume.chain"] = function()
local t = lume.chain({1, 2}):map(function(x) return x * 2 end):result()