summaryrefslogtreecommitdiff
path: root/client/data/fn.lua
diff options
context:
space:
mode:
authorU-DESKTOP-FOJ6TK1\Alex <alex@cogarr.net>2025-02-12 17:25:25 -0600
committerU-DESKTOP-FOJ6TK1\Alex <alex@cogarr.net>2025-02-12 17:25:25 -0600
commitb1c3386ca4032ac5925969d759ae2e5bc015d1f8 (patch)
tree74e7d8797afea9df12222e149e865134ddec9479 /client/data/fn.lua
parent0aae46ecc38005236210f7e243f02cac39ab1dc3 (diff)
downloadhome_text_adventure-b1c3386ca4032ac5925969d759ae2e5bc015d1f8.tar.gz
home_text_adventure-b1c3386ca4032ac5925969d759ae2e5bc015d1f8.tar.bz2
home_text_adventure-b1c3386ca4032ac5925969d759ae2e5bc015d1f8.zip
Final commitHEADmaster
Diffstat (limited to 'client/data/fn.lua')
-rw-r--r--client/data/fn.lua240
1 files changed, 120 insertions, 120 deletions
diff --git a/client/data/fn.lua b/client/data/fn.lua
index e2fa59e..09186b7 100644
--- a/client/data/fn.lua
+++ b/client/data/fn.lua
@@ -1,120 +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
+--[[
+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