You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
3.2KB

  1. local doc = require "wslua.doc"
  2. local fndef = require "wslua.fndef"
  3. local ty = require "wslua.ty"
  4. local list = require "wslua.list"
  5. local table = setmetatable({}, {__index = table})
  6. doc.module{
  7. name = "wslua.table",
  8. what = "Additional table functionality",
  9. descr = "This module also contains all functions from the default `table` library, and can be used as a drop-in replacement.",
  10. see = {
  11. {module = "wslua.list"},
  12. }
  13. }
  14. table.count = fndef({
  15. name = "table.cont",
  16. what = "Get number of key-value pairs in the table.",
  17. descr = "O(n)",
  18. args = {
  19. {name = "tbl", type = ty.table},
  20. },
  21. ret = {
  22. {type = ty.number},
  23. },
  24. tests = {
  25. function() return 0, table.count{} end,
  26. function() return 1, table.count{1} end,
  27. function() return 1, table.count{a = 1} end,
  28. function() return 2, table.count{a = 1, 1} end,
  29. },
  30. },
  31. function(tbl)
  32. local ret = 0
  33. for _, __ in pairs(tbl) do ret = ret + 1 end
  34. return ret
  35. end)
  36. table.deepcpy = fndef({
  37. name = "table.deepcpy",
  38. what = "Returns a recursive copy of `t`.",
  39. descr = "Also assigns the metatable of `t` to the result, if it exists.",
  40. args = {
  41. {name = "t", type = ty.table},
  42. },
  43. ret = {
  44. {type = ty.table},
  45. },
  46. },
  47. function(t)
  48. local ret = {}
  49. for k, v in pairs(t) do
  50. ret[k] = type(v) == "table" and table.deepcpy(v) or v
  51. end
  52. return setmetatable(ret, getmetatable(t))
  53. end)
  54. table.flatcpy = fndef({
  55. name = "table.flatcpy",
  56. what = "Returns a flat copy of `t`.",
  57. descr = "Also assigns the metatable of `t` to the result, if it exists.",
  58. args = {
  59. {name = "t", type = ty.table},
  60. },
  61. ret = {
  62. {type = ty.table},
  63. },
  64. },
  65. function(t)
  66. local ret = {}
  67. for k, v in pairs(t) do ret[k] = v end
  68. return setmetatable(ret, getmetatable(t))
  69. end)
  70. doc.todo "tests for flatcpy and deepcpy"
  71. table.keys = fndef({
  72. name = "table.keys",
  73. what = "Get a list of all keys in the table.",
  74. args = {
  75. {name = "tbl", type = ty.table},
  76. },
  77. ret = {
  78. {type = ty.class(list)},
  79. },
  80. tests = {
  81. function() return "{}", table.keys{}:sort():__tostring() end,
  82. function() return "{a, b}", table.keys{a = 1, b = 2}:sort():__tostring() end,
  83. function() return "{1, 2}", table.keys{3, 4}:sort():__tostring() end,
  84. },
  85. },
  86. function(tbl)
  87. local ret = list()
  88. for k, _ in pairs(tbl) do ret:insert(k) end
  89. return ret
  90. end)
  91. table.update = fndef({
  92. name = "table.update",
  93. what = "Update entries in a table from another table.",
  94. args = {
  95. {name = "t1", type = ty.table, what = "Table to be updated."},
  96. {name = "t2", type = ty.table, what = "Table to take updated key-value pairs from."},
  97. },
  98. ret = {
  99. {type = ty.table, what = "Updated `t1`."},
  100. },
  101. },
  102. function(t1, t2)
  103. for k, v in pairs(t2) do t1[k] = v end
  104. return t1
  105. end)
  106. table.values = fndef({
  107. name = "table.values",
  108. what = "Get a list of all values in the table.",
  109. args = {
  110. {name = "tbl", type = ty.table},
  111. },
  112. ret = {
  113. {type = ty.class(list)},
  114. },
  115. tests = {
  116. function() return "{}", table.values{}:sort():__tostring() end,
  117. function() return "{1, 2}", table.values{a = 1, b = 2}:sort():__tostring() end,
  118. function() return "{3, 4}", table.values{3, 4}:sort():__tostring() end,
  119. },
  120. },
  121. function(tbl)
  122. local ret = list()
  123. for _, v in pairs(tbl) do ret:insert(v) end
  124. return ret
  125. end)
  126. return table