Freack100

RandomUtils.min

Dec 7th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.09 KB | None | 0 0
  1. -- Copyright (c) 2016 Paul Kosel
  2. --
  3. -- MIT License
  4. --
  5. -- Permission is hereby granted, free of charge, to any person obtaining
  6. -- a copy of this software and associated documentation files (the
  7. -- "Software"), to deal in the Software without restriction, including
  8. -- without limitation the rights to use, copy, modify, merge, publish,
  9. -- distribute, sublicense, and/or sell copies of the Software, and to
  10. -- permit persons to whom the Software is furnished to do so, subject to
  11. -- the following conditions:
  12. --
  13. -- The above copyright notice and this permission notice shall be
  14. -- included in all copies or substantial portions of the Software.
  15. --
  16. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. -- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. -- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. -- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. -- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. -- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. -- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23.  
  24. local function e(s)
  25. local h=setmetatable({},{__index=_ENV or getfenv()})if setfenv then setfenv(s,h)end;return s(h)or h end
  26. local t=e(function(s,...)local h={}function h.identity(r)return r end
  27. function h.isEqual(r,d)if(type(r)~=type(d))then
  28. return false end
  29. if(type(r)=="table")then for l,r in pairs(r)do
  30. if(d[l]~=r)then return false end end;return true end;return r==d end;function h.toBoolean(r)return r and true or false end
  31. function h.isCallable(r)if(
  32. type(r)=="table")then return
  33. type(getmetatable(r).__call)=="function"end;return
  34. type(r)=="function"end;function h.isTable(r)return type(r)=="table"end
  35. function h.isNil(r)return r==nil end
  36. function h.isArray(r)if not h.isTable(r)then return false end;local d=0;for l,l in pairs(r)do d=d+1;if
  37. h.isNil(r[d])then return false end end;return true end
  38. function h.isIterable(r)return h.toBoolean(pcall(pairs,r))end;function h.isString(r)return type(r)=="string"end
  39. function h.isEmpty(r)if h.isNil(r)then return
  40. true end;if h.isString(r)then return#r==0 end;if h.isTable(r)then return
  41. next(r)==nil end;return true end;function h.isNumber(r)return type(r)=="number"end;function h.isNaN(r)return
  42. h.isNumber(r)and r~=r end;function h.isFinite(r)if
  43. not h.isNumber(r)then return false end
  44. return r>-math.huge and r<math.huge end;function h.isBoolean(r)return
  45. type(r)=="boolean"end
  46. function h.isInteger(r)return type(r)=="number"and
  47. math.floor(r)==r end
  48. function h.once(r)local d=false;local l=false;return
  49. function(...)if(d)then return l end;l=r(unpack({...}))return l end end
  50. function h.functions(r)local d={}for l,u in pairs(r)do if(type(u)=="function")then
  51. table.insert(d,l)end end;return d end
  52. function h.clone(r)if(type(r)~="table")then return r end;local d={}for l,u in pairs(r)do
  53. d[h.clone(l)]=h.clone(u)end;return d end
  54. function h.result(r,d,...)
  55. if(r[d])then
  56. if(h.isCallable(r[d]))then return r[d](r,...)else return r[d]end elseif _.isCallable(d)then return d(r,...)end end
  57. function h.memoize(r,d)local l=d or h.identity;local u={}
  58. return function(...)for m,f in pairs(u)do if(h.isEqual(m,{...}))then
  59. return f end end;local c=r(...)
  60. u[{...}]=c;return c end end;function h.after(r,d)local u,c=0,d
  61. return function(...)u=u+1;if(u>=c)then return r(...)end end end
  62. function h.compose(...)local r={...}return
  63. function(...)
  64. local d=r[1](...)for l,u in pairs(r)do if(l~=1)then d=u(d)end end;return d end end;function h.pipe(r,...)return h.compose(...)(r)end;function h.times(r,d,...)local l={}for u=1,r do
  65. l[u]=d(u,...)end;return l end;function h.bind(r,d)return
  66. function(...)return r(d,...)end end
  67. function h.bindn(r,...)local d={...}return function(...)return
  68. r(unpack(d),...)end end;function h.tap(r,d,...)d(r,...)return r end;return h end)
  69. local a=e(function(s,...)local h={}
  70. function h.each(r,d,...)for l,u in pairs(r)do d(l,u,...)end end
  71. function h.at(r,...)local d={...}local l={}for u,c in pairs(d)do l[#l+1]=r[c]end;return l end
  72. function h.count(r,d)if(d==nil)then return#r end;local l=0;for u,c in pairs(r)do
  73. if(_.isEqual(d,d))then l=l+1 end end;return l end
  74. function h.cycle(r,d)d=d or 1;if d<=0 then return function()end end;local l,u;local c=0
  75. while true do
  76. return function()l=
  77. l and next(r,l)or next(r)u=not u and l or u;if d then c=
  78. (l==u)and c+1 or c;if c>d then return end end
  79. return l,r[l]end end end
  80. function h.map(r,d)local l={}for u,c in pairs(r)do l[u]=d(u,c)end;return l end
  81. function h.reduce(r,d)local l=""for u,c in pairs(r)do l=d(l,c)end;return l end
  82. function h.mapReduce(r,d)local l={}local u=""for c,m in pairs(r)do u=d(u,m)l[#l+1]=u end;return l end
  83. function h.detect(r,d)for l,u in pairs(r)do
  84. if(_.isCallable(d))then
  85. if(_.isEqual(u,d(u)))then return l end else if(_.isEqual(d,u))then return l end end end;return nil end;function h.all(r,d)
  86. for l,u in pairs(r)do if(not d(l,u))then return false end end;return true end;function h.include(r,d)return
  87. h.detect(r,d)~=nil end;return h end)
  88. local o=e(function(s,...)local h={}function h.toArray(...)return{...}end;function h.find(r,d,l)for u=(l or 1),#r do
  89. if(r[u]==d)then return u end end end
  90. function h.reverse(r)
  91. local d={}for l=#r,1,-1 do d[#r-l+1]=r[l]end;return d end
  92. function h.selectWhile(r,d,...)local l={}for u=1,#r do
  93. if(d(u,r[u],...))then l[#l+1]=r[u]else break end end;return l end
  94. function h.dropWhile(r,d,...)local l={}for u=1,#r do
  95. if(not d(u,r[u],...))then select[#l+1]=r[u]else break end end;return l end
  96. function h.indexOf(r,d)for l,u in pairs(r)do if(d==u)then return l end end end
  97. function h.lastIndexOf(r,d)local l;for u,c in pairs(r)do if(d==c)then l=u end end;return l end
  98. function h.push(r,...)for d,l in pairs({...})do r[#r+1]=l end;return r end;function h.pop(r)local d=r[1]table.remove(r,1)return d end
  99. function h.unshift(r)local d=r[
  100. #r]table.remove(r,#r)return d end
  101. function h.pull(r,...)local d={}for u,c in pairs({...})do d[c]=true end;local l={}for u,c in pairs(r)do if
  102. (not d[c])then l[#l+1]=c end end;return l end
  103. function h.removeRange(r,d,l)for u=d,l do table.remove(r,d)end;return r end
  104. function h.slice(r,d,l)local u={}for c=d,l do u[#u+1]=r[c]end;return u end
  105. function h.first(r,d)local l={}for u=1,(d or 1)do l[u]=r[u]end;return l end
  106. function h.initial(r,d)local l={}for u=1,#r- (d or 1)do l[u]=r[u]end;return l end
  107. function h.last(r,d)local l={}for u=#r- (d or 1),#r do l[#l+1]=r[u]end;return l end;function h.nth(r,d)return r[d]end
  108. function h.difference(r,d)local u={}local c={}
  109. for l,m in pairs(d)do c[m]=true end;for l,m in pairs(r)do if(not c[m])then u[#u+1]=m end end;return
  110. u end
  111. function h.union(...)local r={}local d={}
  112. for l,u in pairs({...})do for l,c in pairs(u)do
  113. if(not d[c])then r[#r+1]=c;d[c]=true end end end;return end
  114. function h.unique(r)local d={}local l={}for u,c in pairs(r)do
  115. if(not d[c])then l[#l+1]=c;d[c]=true end end;return l end
  116. function h.isunique(r)local d={}local l={}for u,c in pairs(r)do
  117. if(not d[c])then d[c]=true else return false end end;return true end
  118. function h.append(r,d)local l=r;for u,c in pairs(d)do l[#l+d]=c end;return l end
  119. function h.range(r,d,l)local l=l or 1;local u={}if(not d)then for c=0,r,l do u[#u+1]=c end else
  120. for c=r,d,l do u[#u+1]=c end end;return u end;function h.rep(r,d)local l={}for u=1,d do l[#l+1]=r end;return l end
  121. function h.concat(r,d)
  122. local l=""local d=d or""for u,r in ipairs(r)do l=l..d..r end;return l end
  123. function h.split(r,d)local d=d or" "local l={}for u in r:gmatch("[^"..d.."]+")do
  124. table.insert(l,u)end;return l end;return h end)local i={}_G._=setmetatable({},{__index=i})
  125. local function n(s,h)if(not s)then return end;for r,d in
  126. pairs(s)do if(not h[r])then h[r]=d end end end;n(t,i)n(o,i)n(a,i)n(Helper,i)i._MODULES={}
  127. i._MODULES.Core=true;i._MODULES.Array=i.toBoolean(o)
  128. i._MODULES.Table=i.toBoolean(a)i._MODULES.Computercraft=i.toBoolean(CC)
  129. i._MODULES.Helper=i.toBoolean(Helper)
  130. function i.new(s)local h={_value=s,_wrapped=true}
  131. local function r(d,l)if(l=="value")then
  132. return function()return h._value end end
  133. if(i.isCallable(i[l]))then return function(...)return
  134. i.new(i[l](h._value,...))end end end;return setmetatable(h,{__index=r})end;function i.chain(s)return i.new(s)end
Add Comment
Please, Sign In to add comment