Advertisement
Guest User

lua

a guest
Jul 28th, 2015
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.21 KB | None | 0 0
  1. /*---------------------------------------------------------------------------
  2. Functional library
  3.  
  4. by FPtje Atheos
  5. ---------------------------------------------------------------------------*/
  6.  
  7. /*---------------------------------------------------------------------------
  8. Function currying
  9. Take a function with n parameters.
  10. Currying is the procedure of storing k < n parameters "in the function"
  11. in such a way that the remaining function can be called with n - k parameters
  12.  
  13. Example:
  14. DebugPrint = fp{print, "[DEBUG]"}
  15. DebugPrint("TEST")
  16. > [DEBUG] TEST
  17. ---------------------------------------------------------------------------*/
  18. function fp(tbl)
  19. local func = tbl[1]
  20.  
  21. return function(...)
  22. local fnArgs = {}
  23. local arg = {...}
  24. local tblN = table.maxn(tbl)
  25.  
  26. for i = 2, tblN do fnArgs[i - 1] = tbl[i] end
  27. for i = 1, table.maxn(arg) do fnArgs[tblN + i - 1] = arg[i] end
  28.  
  29. return func(unpack(fnArgs, 1, table.maxn(fnArgs)))
  30. end
  31. end
  32.  
  33. local unpack = unpack
  34. local table = table
  35. local pairs = pairs
  36. local ipairs = ipairs
  37. local error = error
  38. local math = math
  39. local select = select
  40. local _G = _G
  41. local fp = fp
  42.  
  43.  
  44. module("fn")
  45.  
  46. /*---------------------------------------------------------------------------
  47. Parameter manipulation
  48. ---------------------------------------------------------------------------*/
  49. Id = function(...) return ... end
  50.  
  51. Flip = function(f)
  52. if not f then error("not a function") end
  53. return function(b, a, ...)
  54. return f(a, b, ...)
  55. end
  56. end
  57.  
  58. -- Definition from http://lua-users.org/wiki/CurriedLua
  59. ReverseArgs = function(...)
  60.  
  61. --reverse args by building a function to do it, similar to the unpack() example
  62. local function reverse_h(acc, v, ...)
  63. if select('#', ...) == 0 then
  64. return v, acc()
  65. else
  66. return reverse_h(function () return v, acc() end, ...)
  67. end
  68. end
  69.  
  70. -- initial acc is the end of the list
  71. return reverse_h(function () return end, ...)
  72. end
  73.  
  74. /*---------------------------------------------------------------------------
  75. Misc functions
  76. ---------------------------------------------------------------------------*/
  77. -- function composition
  78. Compose = function(funcs)
  79. return function(...)
  80. local res = {...}
  81. for i = #funcs, 1, -1 do
  82. res = {funcs[i](unpack(res))}
  83. end
  84. return unpack(res)
  85. end
  86. end
  87.  
  88. _G.fc = Compose
  89.  
  90. -- Definition from http://lua-users.org/wiki/CurriedLua
  91. Curry = function(func, num_args)
  92. if not num_args then error("Missing argument #2: num_args") end
  93. if not func then error("Function does not exist!", 2) end
  94. -- helper
  95. local function curry_h(argtrace, n)
  96. if n == 0 then
  97. -- reverse argument list and call function
  98. return func(ReverseArgs(argtrace()))
  99. else
  100. -- "push" argument (by building a wrapper function) and decrement n
  101. return function(x)
  102. return curry_h(function() return x, argtrace() end, n - 1)
  103. end
  104. end
  105. end
  106.  
  107. -- no sense currying for 1 arg or less
  108. if num_args > 1 then
  109. return curry_h(function() return end, num_args)
  110. else
  111. return func
  112. end
  113. end
  114.  
  115. -- Thanks Lexic!
  116. Partial = function(func, ...)
  117. local args = {...}
  118. return function(...)
  119. return func(unpack(table.Add( args, {...})))
  120. end
  121. end
  122.  
  123. Apply = function(f, ...) return f(...) end
  124.  
  125. Const = function(a, b) return a end
  126. Until = function(cmp, fn, val)
  127. if cmp(val) then
  128. return val
  129. end
  130. return Until(cmp, fn, fn(val))
  131. end
  132.  
  133. Seq = function(f, x) f(x) return x end
  134.  
  135. GetGlobalVar = function(key) return _G[key] end
  136.  
  137. /*---------------------------------------------------------------------------
  138. Mathematical operators and functions
  139. ---------------------------------------------------------------------------*/
  140. Add = function(a, b) return a + b end
  141. Sub = function(a, b) return a - b end
  142. Mul = function(a, b) return a * b end
  143. Div = function(a, b) return a / b end
  144. Mod = function(a, b) return a % b end
  145. Neg = function(a) return -a end
  146.  
  147. Eq = function(a, b) return a == b end
  148. Neq = function(a, b) return a ~= b end
  149. Gt = function(a, b) return a > b end
  150. Lt = function(a, b) return a < b end
  151. Gte = function(a, b) return a >= b end
  152. Lte = function(a, b) return a <= b end
  153.  
  154. Succ = Compose{Add, 1}
  155. Pred = Compose{Flip(Sub), 1}
  156. Even = Compose{fp{Eq, 0}, fp{Flip(Mod), 2}}
  157. Odd = Compose{Not, Even}
  158.  
  159. /*---------------------------------------------------------------------------
  160. Functional logical operators and conditions
  161. ---------------------------------------------------------------------------*/
  162. FAnd = function(fns)
  163. return function(...)
  164. local val
  165. for _, f in pairs(fns) do
  166. val = {f(...)}
  167. if not val[1] then return unpack(val) end
  168. end
  169. if val then return unpack(val) end
  170. end
  171. end
  172.  
  173. FOr = function(fns)
  174. return function(...)
  175. for _, f in pairs(fns) do
  176. local val = {f(...)}
  177. if val[1] then return unpack(val) end
  178. end
  179. return false
  180. end
  181. end
  182.  
  183. Not = function(x) return not x end
  184.  
  185. If = function(f, Then, Else)
  186. return function(x)
  187. if f(x) then
  188. return Then
  189. else
  190. return Else
  191. end
  192. end
  193. end
  194.  
  195. /*---------------------------------------------------------------------------
  196. List operations
  197. ---------------------------------------------------------------------------*/
  198. Map = function(f, xs)
  199. for k, v in pairs(xs) do
  200. xs[k] = f(v)
  201. end
  202. return xs
  203. end
  204.  
  205. Append = function(xs, ys)
  206. return table.Add(xs, ys)
  207. end
  208.  
  209. Filter = function(f, xs)
  210. local res = {}
  211. for k,v in pairs(xs) do
  212. if f(v) then res[k] = v end
  213. end
  214. return res
  215. end
  216.  
  217. ForEach = function(f, xs)
  218. for k,v in pairs(xs) do
  219. local val = f(k, v)
  220. if val ~= nil then return val end
  221. end
  222. end
  223.  
  224. Head = function(xs)
  225. return table.GetFirstValue(xs)
  226. end
  227.  
  228. Last = function(xs)
  229. return xs[#xs] or table.GetLastValue(xs)
  230. end
  231.  
  232. Tail = function(xs)
  233. table.remove(xs, 1)
  234. return xs
  235. end
  236.  
  237. Init = function(xs)
  238. xs[#xs] = nil
  239. return xs
  240. end
  241.  
  242. GetValue = function(i, xs)
  243. return xs[i]
  244. end
  245.  
  246. Null = function(xs)
  247. for k, v in pairs(xs) do
  248. return false
  249. end
  250. return true
  251. end
  252.  
  253. Length = function(xs)
  254. return #xs
  255. end
  256.  
  257. Index = function(xs, i)
  258. return xs[i]
  259. end
  260.  
  261. Reverse = function(xs)
  262. local res = {}
  263. for i = #xs, 1, -1 do
  264. res[#xs - i + 1] = xs[i]
  265. end
  266. return res
  267. end
  268.  
  269. /*---------------------------------------------------------------------------
  270. Folds
  271. ---------------------------------------------------------------------------*/
  272. Foldr = function(func, val, xs)
  273. for i = #xs, 1, -1 do
  274. val = func(xs[i], val)
  275. end
  276.  
  277. return val
  278. end
  279.  
  280. Foldl = function(func, val, xs)
  281. for k, v in ipairs(xs) do
  282. val = func(val, v)
  283. end
  284.  
  285. return val
  286. end
  287.  
  288. And = function(xs)
  289. for k, v in pairs(xs) do
  290. if v ~= true then return false end
  291. end
  292. return true
  293. end
  294.  
  295. Or = function(xs)
  296. for k, v in pairs(xs) do
  297. if v == true then return true end
  298. end
  299. return false
  300. end
  301.  
  302. Any = function(func, xs)
  303. for k, v in pairs(xs) do
  304. if func(v) == true then return true end
  305. end
  306. return false
  307. end
  308.  
  309. All = function(func, xs)
  310. for k, v in pairs(xs) do
  311. if func(v) ~= true then return false end
  312. end
  313. return true
  314. end
  315.  
  316. Sum = _G.fp{Foldr, Add, 0}
  317.  
  318. Product = _G.fp{Foldr, Mul, 1}
  319.  
  320. Concat = _G.fp{Foldr, Append, {}}
  321.  
  322. Maximum = _G.fp{Foldl, math.Max, -math.huge}
  323.  
  324. Minimum = _G.fp{Foldl, math.Min, math.huge}
  325.  
  326. Snd = _G.fp{select, 2}
  327.  
  328. Thrd = _G.fp{select, 3}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement