OpenComputers Von Neumann Machine Programs
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.

106 lines
1.9 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. local event = require("event")
  2. local computer = require("computer")
  3. local string = require("string")
  4. local table = require("table")
  5. local ll = {}
  6. -- Split a string
  7. function ll.split(s, sep)
  8. if sep == nil then
  9. sep = "%s"
  10. end
  11. local t = {}
  12. for str in string.gmatch(s, "([^"..sep.."]+)") do
  13. table.insert(t, str)
  14. end
  15. return t
  16. end
  17. split = ll.split
  18. -- Slice table
  19. function ll.slice(tbl, first, last, step)
  20. local sliced = {}
  21. for i = first or 1, last or #tbl, step or 1 do
  22. sliced[#sliced+1] = tbl[i]
  23. end
  24. return sliced
  25. end
  26. slice = ll.slice
  27. -- Map function
  28. function ll.map(f, ...)
  29. local args = {...}
  30. local ret = {}
  31. for i = 1, #args[1] do
  32. local t = {}
  33. for j = 1, #args do
  34. table.insert(t, #t+1, args[j][i])
  35. end
  36. table.insert(ret, #ret+1, f(table.unpack(t)))
  37. end
  38. return ret
  39. end
  40. map = ll.map
  41. -- Used to apply filters to a regular list
  42. function ll.filter(f, l)
  43. local ret = {}
  44. for _,v in ipairs(l) do
  45. table.insert(ret, #ret+1, f(v) and v or nil)
  46. end
  47. return ret
  48. end
  49. filter = ll.filter
  50. -- Returns true is something is in the sequence
  51. function ll.any(seq)
  52. for i, v in ipairs(seq) do
  53. if v then
  54. return true
  55. end
  56. end
  57. return false
  58. end
  59. any = ll.any
  60. -- TODO finish this
  61. -- Reduce a regular list
  62. -- function ll.reduce(f, l, init)
  63. -- local start = 1
  64. -- if init == nil then
  65. -- init = l[1]
  66. -- start = 2
  67. -- end
  68. -- local agg = nil
  69. -- for i = start, #l-1 do
  70. -- if l[i]
  71. -- end
  72. -- for _,v in ipairs(t) do
  73. -- table.insert(ret, #ret+1, f(v) and v or nil)
  74. -- end
  75. -- return ret
  76. -- end
  77. -- Simple table printing. Does not do recursive tables
  78. function ll.tprint(...)
  79. local t = {...}
  80. for i = 1, #t do
  81. for k,v in pairs(t[i]) do
  82. print(k,v)
  83. end
  84. end
  85. end
  86. function ll.round(num, numDecimalPlaces)
  87. local mult = 10^(numDecimalPlaces or 0)
  88. return math.floor(num * mult + 0.5) / mult
  89. end
  90. return ll
  91. -- wget -f http://mc.bashed.rocks:13699/lib/liblua.lua /usr/lib/liblua.lua