SHARE
TWEET

Untitled

a guest Aug 24th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local func = {}
  2.  
  3. function func.filter(filterer, f, s, var)
  4.     if f == nil then return f, s, v end
  5.  
  6.     local function stepper(...)
  7.         var = ...
  8.         if var == nil then
  9.             return nil
  10.         elseif filterer(...) then
  11.             return ...
  12.         else
  13.             return stepper(f(s, var))
  14.         end
  15.     end
  16.  
  17.     return function (s, var)
  18.         return stepper(f(s, var))
  19.     end, s, var
  20. end
  21.  
  22. function func.map(mapper, f, s, var)
  23.     if f == nil then return f, s, v end
  24.  
  25.     local function stepper(...)
  26.         var = ...
  27.         if var == nil then return nil
  28.         else return mapper(...)
  29.         end
  30.     end
  31.  
  32.     return function (s, _var)
  33.         return stepper(f(s, var))
  34.     end, s, var
  35. end
  36.  
  37. function func.reduce(init, reducer, f, s, var)
  38.     if f == nil then return f, s, v end
  39.  
  40.     local total = init
  41.  
  42.     local function stopper(total) return total end
  43.  
  44.     local function breaker(...)
  45.         var = ...
  46.         if var == nil then reducer = stopper end
  47.         return ...
  48.     end
  49.  
  50.     local function stepper(...)
  51.         var = ...
  52.         if var == nil then return total
  53.         else return reducer(total, ...)
  54.         end
  55.     end
  56.  
  57.     while true do
  58.         total = stepper(f(s, var))
  59.         if var == nil then break end
  60.     end
  61.  
  62.     return total
  63. end
  64.  
  65. function func.collect(f, s, var)
  66.     if f == nil then return f, s, v end
  67.  
  68.     local function reducer(rs, rv)
  69.         table.insert(rs, rv)
  70.         return rs
  71.     end
  72.  
  73.     return func.reduce({}, reducer, f, s, v)
  74. end
  75.  
  76. function func.join(joiner, f, s, var)
  77.     if f == nil then return f, s, v end
  78.  
  79.     local function reducer(rs, rv)
  80.         if rs == nil then
  81.             return tostring(rv)
  82.         else
  83.             return rs .. joiner .. rv
  84.         end
  85.     end
  86.  
  87.     return func.reduce(nil, reducer, f, s, var)
  88. end
  89.  
  90. function func.split(by, value)
  91.     if by == nil then return nil, 'split by is nil' end
  92.     if value == nil then return nil, 'split value is nil' end
  93.  
  94.     return function (s, var)
  95.         local i = s.i
  96.         local p
  97.         if i == nil then return nil end
  98.         local st, ed = string.find(value, by, s.i, false)
  99.         if st == nil then
  100.             s.i = nil
  101.             return string.sub(value, i)
  102.         else
  103.             s.i = ed + 1
  104.             return string.sub(value, i, st - 1)
  105.         end
  106.     end, { i = 1 }, nil
  107. end
  108.  
  109. return func
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top