Advertisement
Guest User

Untitled

a guest
Jul 20th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. -- Functional Library
  2. --
  3. -- @file functional.lua
  4. -- @author Shimomura Ikkei
  5. -- @date 2005/05/18
  6. --
  7. -- @brief porting several convenience functional utilities form Haskell,Python etc..
  8. -- map(function, table)
  9. -- e.g: map(double, {1,2,3}) -> {2,4,6}
  10. function map(func, tbl)
  11. local newtbl = {}
  12. for i,v in pairs(tbl) do
  13. newtbl[i] = func(v)
  14. end
  15. return newtbl
  16. end
  17. -- filter(function, table)
  18. -- e.g: filter(is_even, {1,2,3,4}) -> {2,4}
  19. function filter(func, tbl)
  20. local newtbl= {}
  21. for i,v in pairs(tbl) do
  22. if func(v) then
  23. newtbl[i]=v
  24. end
  25. end
  26. return newtbl
  27. end
  28. -- head(table)
  29. -- e.g: head({1,2,3}) -> 1
  30. function head(tbl)
  31. return tbl[1]
  32. end
  33.  
  34. -- tail(table)
  35. -- e.g: tail({1,2,3}) -> {2,3}
  36. --
  37. -- XXX This is a BAD and ugly implementation.
  38. -- should return the address to next porinter, like in C (arr+1)
  39. function tail(tbl)
  40. if table.getn(tbl) < 1 then
  41. return nil
  42. else
  43. local newtbl = {}
  44. local tblsize = table.getn(tbl)
  45. local i = 2
  46. while (i <= tblsize) do
  47. table.insert(newtbl, i-1, tbl[i])
  48. i = i + 1
  49. end
  50. return newtbl
  51. end
  52. end
  53. -- foldr(function, default_value, table)
  54. -- e.g: foldr(operator.mul, 1, {1,2,3,4,5}) -> 120
  55. function foldr(func, val, tbl)
  56. for i,v in pairs(tbl) do
  57. val = func(val, v)
  58. end
  59. return val
  60. end
  61.  
  62. -- reduce(function, table)
  63. -- e.g: reduce(operator.add, {1,2,3,4}) -> 10
  64. function reduce(func, tbl)
  65. return foldr(func, head(tbl), tail(tbl))
  66. end
  67. -- curry(f,g)
  68. -- e.g: printf = curry(io.write, string.format)
  69. -- -> function(...) return io.write(string.format(unpack(arg))) end
  70. function curry(f,g)
  71. return function (...)
  72. return f(g(unpack(arg)))
  73. end
  74. end
  75. -- bind1(func, binding_value_for_1st)
  76. -- bind2(func, binding_value_for_2nd)
  77. -- @brief
  78. -- Binding argument(s) and generate new function.
  79. -- @see also STL's functional, Boost's Lambda, Combine, Bind.
  80. -- @examples
  81. -- local mul5 = bind1(operator.mul, 5) -- mul5(10) is 5 * 10
  82. -- local sub2 = bind2(operator.sub, 2) -- sub2(5) is 5 -2
  83. function bind1(func, val1)
  84. return function (val2)
  85. return func(val1, val2)
  86. end
  87. end
  88. function bind2(func, val2) -- bind second argument.
  89. return function (val1)
  90. return func(val1, val2)
  91. end
  92. end
  93. -- is(checker_function, expected_value)
  94. -- @brief
  95. -- check function generator. return the function to return boolean,
  96. -- if the condition was expected then true, else false.
  97. -- @example
  98. -- local is_table = is(type, "table")
  99. -- local is_even = is(bind2(math.mod, 2), 1)
  100. -- local is_odd = is(bind2(math.mod, 2), 0)
  101. is = function(check, expected)
  102. return function (...)
  103. if (check(unpack(arg)) == expected) then
  104. return true
  105. else
  106. return false
  107. end
  108. end
  109. end
  110. -- operator table.
  111. -- @see also python's operator module.
  112. operator = {
  113. mod = math.mod;
  114. pow = math.pow;
  115. add = function(n,m) return n + m end;
  116. sub = function(n,m) return n - m end;
  117. mul = function(n,m) return n * m end;
  118. div = function(n,m) return n / m end;
  119. gt = function(n,m) return n > m end;
  120. lt = function(n,m) return n < m end;
  121. eq = function(n,m) return n == m end;
  122. le = function(n,m) return n <= m end;
  123. ge = function(n,m) return n >= m end;
  124. ne = function(n,m) return n ~= m end;
  125.  
  126. }
  127. -- enumFromTo(from, to)
  128. -- e.g: enumFromTo(1, 10) -> {1,2,3,4,5,6,7,8,9}
  129. -- TODO How to lazy evaluate in Lua? (thinking with coroutine)
  130. enumFromTo = function (from,to)
  131. local newtbl = {}
  132. local step = bind2(operator[(from < to) and "add" or "sub"], 1)
  133. local val = from
  134. while val <= to do
  135. table.insert(newtbl, table.getn(newtbl)+1, val)
  136. val = step(val)
  137. end
  138. return newtbl
  139. end
  140. -- make function to take variant arguments, replace of a table.
  141. -- this does not mean expand the arguments of function took,
  142. -- it expand the function's spec: function(tbl) -> function(...)
  143. function expand_args(func)
  144. return function(...) return func(arg) end
  145. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement