在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):rxi/lume开源软件地址(OpenSource Url):https://github.com/rxi/lume开源编程语言(OpenSource Language):Lua 100.0%开源软件介绍(OpenSource Introduction):LumeA collection of functions for Lua, geared towards game development. InstallationThe lume.lua file should be dropped into an existing project and required by it: lume = require "lume" Function Referencelume.clamp(x, min, max)Returns the number lume.round(x [, increment])Rounds lume.round(2.3) -- Returns 2
lume.round(123.4567, .1) -- Returns 123.5 lume.sign(x)Returns lume.lerp(a, b, amount)Returns the linearly interpolated number between lume.lerp(100, 200, .5) -- Returns 150 lume.smooth(a, b, amount)Similar to lume.pingpong(x)Ping-pongs the number lume.distance(x1, y1, x2, y2 [, squared])Returns the distance between the two points. If lume.angle(x1, y1, x2, y2)Returns the angle between the two points. lume.vector(angle, magnitude)Given an local x, y = lume.vector(0, 10) -- Returns 10, 0 lume.random([a [, b]])Returns a random number between lume.randomchoice(t)Returns a random value from array lume.randomchoice({true, false}) -- Returns either true or false lume.weightedchoice(t)Takes the argument table lume.weightedchoice({ ["cat"] = 10, ["dog"] = 5, ["frog"] = 0 })
-- Returns either "cat" or "dog" with "cat" being twice as likely to be chosen. lume.isarray(x)Returns lume.push(t, ...)Pushes all the given values to the end of the table local t = { 1, 2, 3 }
lume.push(t, 4, 5) -- `t` becomes { 1, 2, 3, 4, 5 } lume.remove(t, x)Removes the first instance of the value local t = { 1, 2, 3 }
lume.remove(t, 2) -- `t` becomes { 1, 3 } lume.clear(t)Nils all the values in the table local t = { 1, 2, 3 }
lume.clear(t) -- `t` becomes {} lume.extend(t, ...)Copies all the fields from the source tables to the table local t = { a = 1, b = 2 }
lume.extend(t, { b = 4, c = 6 }) -- `t` becomes { a = 1, b = 4, c = 6 } lume.shuffle(t)Returns a shuffled copy of the array lume.sort(t [, comp])Returns a copy of the array 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. lume.array(string.gmatch("Hello world", "%a+")) -- Returns {"Hello", "world"} lume.each(t, fn, ...)Iterates the table lume.each({1, 2, 3}, print) -- Prints "1", "2", "3" on separate lines
lume.each({a, b, c}, "move", 10, 20) -- Does x:move(10, 20) on each value lume.map(t, fn)Applies the function lume.map({1, 2, 3}, function(x) return x * 2 end) -- Returns {2, 4, 6} lume.all(t [, fn])Returns true if all the values in lume.all({1, 2, 1}, function(x) return x == 1 end) -- Returns false lume.any(t [, fn])Returns true if any of the values in lume.any({1, 2, 1}, function(x) return x == 1 end) -- Returns true lume.reduce(t, fn [, first])Applies lume.reduce({1, 2, 3}, function(a, b) return a + b end) -- Returns 6 lume.unique(t)Returns a copy of the lume.unique({2, 1, 2, "cat", "cat"}) -- Returns {1, 2, "cat"} lume.filter(t, fn [, retainkeys])Calls lume.filter({1, 2, 3, 4}, function(x) return x % 2 == 0 end) -- Returns {2, 4} lume.reject(t, fn [, retainkeys])The opposite of lume.reject({1, 2, 3, 4}, function(x) return x % 2 == 0 end) -- Returns {1, 3} 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. 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. lume.concat({1, 2}, {3, 4}, {5, 6}) -- Returns {1, 2, 3, 4, 5, 6} lume.find(t, value)Returns the index/key of lume.find({"a", "b", "c"}, "b") -- Returns 2 lume.match(t, fn)Returns the value and key of the value in table lume.match({1, 5, 8, 7}, function(x) return x % 2 == 0 end) -- Returns 8, 3 lume.count(t [, fn])Counts the number of values in the table lume.count({a = 2, b = 3, c = 4, d = 5}) -- Returns 4
lume.count({1, 2, 4, 6}, function(x) return x % 2 == 0 end) -- Returns 3 lume.slice(t [, i [, j]])Mimics the behaviour of Lua's lume.slice({"a", "b", "c", "d", "e"}, 2, 4) -- Returns {"b", "c", "d"} lume.first(t [, n])Returns the first element of an array or nil if the array is empty. If lume.first({"a", "b", "c"}) -- Returns "a" lume.last(t [, n])Returns the last element of an array or nil if the array is empty. If lume.last({"a", "b", "c"}) -- Returns "c" lume.invert(t)Returns a copy of the table where the keys have become the values and the values the keys. lume.invert({a = "x", b = "y"}) -- returns {x = "a", y = "b"} lume.pick(t, ...)Returns a copy of the table filtered to only contain values for the given keys. lume.pick({ a = 1, b = 2, c = 3 }, "a", "c") -- Returns { a = 1, c = 3 } lume.keys(t)Returns an array containing each key of the table. lume.clone(t)Returns a shallow copy of the table lume.fn(fn, ...)Creates a wrapper function around function local f = lume.fn(print, "Hello")
f("world") -- Prints "Hello world" lume.once(fn, ...)Returns a wrapper function to local f = lume.once(print, "Hello")
f() -- Prints "Hello"
f() -- Does nothing lume.memoize(fn)Returns a wrapper function to fib = lume.memoize(function(n) return n < 2 and n or fib(n-1) + fib(n-2) end) lume.combine(...)Creates a wrapper function which calls each supplied argument in the order they
were passed to local f = lume.combine(function(a, b) print(a + b) end,
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
lume.call(print, "Hello world") -- Prints "Hello world" lume.time(fn, ...)Inserts the arguments into function lume.time(function(x) return x end, "hello") -- Returns 0, "hello" lume.lambda(str)Takes a string lambda and returns a function. local f =
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论