From 0aae46ecc38005236210f7e243f02cac39ab1dc3 Mon Sep 17 00:00:00 2001 From: Alexander Pickering Date: Sun, 27 Jan 2019 10:32:09 -0500 Subject: Inital commit --- client/data/fn.lua | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 client/data/fn.lua (limited to 'client/data/fn.lua') diff --git a/client/data/fn.lua b/client/data/fn.lua new file mode 100644 index 0000000..e2fa59e --- /dev/null +++ b/client/data/fn.lua @@ -0,0 +1,120 @@ +--[[ +Functional programming primitives that are occasionally useful + +This is not a complete set of primitives, I make them when I need them. Anywhere I use these +heavily (where someone unfamiliar with the functional paradigm might get confused) +I have tried to add comments above or below explaining what it does. +]] +local fn = {} + +--Returns a function that "stores" the arguments this function was called with +--For example +--[[ +local hiprint = fn.curry(print,"[hi]") +hiprint("world") -- calls print("[hi]","world"), prints "[hi] world" +]] +function fn.curry(func,...) + local args = {...} + return function(...) + local nargs = {} + for k,v in pairs(args) do nargs[k] = v end + for k,v in pairs({...}) do + nargs[#nargs + 1] = v + end + func(table.unpack(nargs)) + end +end + +--Returns a function that calls all functions starting with the rightmost function +--and calling the "next" function with the returns of the previous function. +--The first function can be called with any arguments like normal +--Anything returned from the last function is returned "out" of this function. +--Example: +-- +--local fn = require("fn") +--local printf = fn.compose(print,string.format) +--printf("This is %s shit","funky") --prints "This is some funky shit" +function fn.compose(...) + local nargs = {...} + local lastresult + return function(...) + lastresult = {...} + for n = #nargs, 1, -1 do + lastresult = {nargs[n](table.unpack(lastresult))} + end + return table.unpack(lastresult) + end +end + +--Returns a function that applies the given function on a table called with it. +--Example: +-- +--local fn = require("fn") +--local add_five = fn.map(function(e) return e + 5 end) +--add_five({1,4,9}) --Returns a table {6,10,14} +function fn.map(func) + return function(tbl) + local ret = {} + for k,v in pairs(tbl) do + ret[k] = func(v) + end + return ret + end +end + +--Returns a function that removes anything NOT matching the given function +--Example: +-- +--local fn = require("fn") +--local important_skills = fn.filter(function(e) return e > 10 end) +--local usable_skills = imporant_skills({ +-- ["wood cutting"] = 5, +-- ["mining"] = 21, +-- ["fighting"] = 12, +-- ["fishing"] = 10, +--}) +--for k,v in pairs(usable_skills) print(k,":",v) end --prints: +--mining : 21 +--fighting : 12 +function fn.filter(func) + return function(tbl) + local ret = {} + for k,v in pairs(tbl) do + if not func(v) then + ret[k] = v + end + end + return ret + end +end + +--Get the keys of a table in an array +function fn.keys(tbl) + local ret = {} + for k,v in pairs(tbl) do + table.insert(ret,k) + end + return ret +end + +--Get the values of a table in an array +function fn.values(tbl) + local ret = {} + for k,v in pairs(tbl) do + table.insert(ret,v) + end + return ret +end + +--Reverses an array +function fn.reverse(tbl) + local ret = {} + local len = #tbl + for i = 1,len do + ret[len - i + 1] = tbl[i] + end + return ret +end + + +return fn -- cgit v1.2.3-70-g09d2