| 1 |
--- |
| 2 |
--- Lua Fun - a high-performance functional programming library for LuaJIT |
| 3 |
--- |
| 4 |
--- Copyright (c) 2013-2017 Roman Tsisyk <[email protected]> |
| 5 |
--- |
| 6 |
--- Distributed under the MIT/X11 License. See COPYING.md for more details. |
| 7 |
--- |
| 8 |
|
| 9 |
local exports = {} |
| 10 |
local methods = {} |
| 11 |
|
| 12 |
-- Rspamd specific |
| 13 |
local rspamd_text = require "rspamd_text" |
| 14 |
local text_cookie = rspamd_text.cookie |
| 15 |
|
| 16 |
-- compatibility with Lua 5.1/5.2 |
| 17 |
local unpack = rawget(table, "unpack") or unpack |
| 18 |
|
| 19 |
-------------------------------------------------------------------------------- |
| 20 |
-- Tools |
| 21 |
-------------------------------------------------------------------------------- |
| 22 |
|
| 23 |
local return_if_not_empty = function(state_x, ...) |
| 24 |
if state_x == nil then |
| 25 |
return nil |
| 26 |
end |
| 27 |
return ... |
| 28 |
end |
| 29 |
|
| 30 |
local call_if_not_empty = function(fun, state_x, ...) |
| 31 |
if state_x == nil then |
| 32 |
return nil |
| 33 |
end |
| 34 |
return state_x, fun(...) |
| 35 |
end |
| 36 |
|
| 37 |
local function deepcopy(orig) -- used by cycle() |
| 38 |
local orig_type = type(orig) |
| 39 |
local copy |
| 40 |
if orig_type == 'table' then |
| 41 |
copy = {} |
| 42 |
for orig_key, orig_value in next, orig, nil do |
| 43 |
copy[deepcopy(orig_key)] = deepcopy(orig_value) |
| 44 |
end |
| 45 |
else |
| 46 |
copy = orig |
| 47 |
end |
| 48 |
return copy |
| 49 |
end |
| 50 |
|
| 51 |
local iterator_mt = { |
| 52 |
-- usually called by for-in loop |
| 53 |
__call = function(self, param, state) |
| 54 |
return self.gen(param, state) |
| 55 |
end; |
| 56 |
__tostring = function(self) |
| 57 |
return '<generator>' |
| 58 |
end; |
| 59 |
-- add all exported methods |
| 60 |
__index = methods; |
| 61 |
} |
| 62 |
|
| 63 |
local wrap = function(gen, param, state) |
| 64 |
return setmetatable({ |
| 65 |
gen = gen, |
| 66 |
param = param, |
| 67 |
state = state |
| 68 |
}, iterator_mt), param, state |
| 69 |
end |
| 70 |
exports.wrap = wrap |
| 71 |
|
| 72 |
local unwrap = function(self) |
| 73 |
return self.gen, self.param, self.state |
| 74 |
end |
| 75 |
methods.unwrap = unwrap |
| 76 |
|
| 77 |
-------------------------------------------------------------------------------- |
| 78 |
-- Basic Functions |
| 79 |
-------------------------------------------------------------------------------- |
| 80 |
|
| 81 |
local nil_gen = function(_param, _state) |
| 82 |
return nil |
| 83 |
end |
| 84 |
|
| 85 |
local string_gen = function(param, state) |
| 86 |
local state = state + 1 |
| 87 |
if state > #param then |
| 88 |
return nil |
| 89 |
end |
| 90 |
local r = string.sub(param, state, state) |
| 91 |
return state, r |
| 92 |
end |
| 93 |
|
| 94 |
local text_gen = function(param, state) |
| 95 |
local state = state + 1 |
| 96 |
if state > #param then |
| 97 |
return nil |
| 98 |
end |
| 99 |
local r = string.char(param:byte(state)) |
| 100 |
return state, r |
| 101 |
end |
| 102 |
|
| 103 |
local ipairs_gen = ipairs({}) -- get the generating function from ipairs |
| 104 |
|
| 105 |
local pairs_gen = pairs({ a = 0 }) -- get the generating function from pairs |
| 106 |
local map_gen = function(tab, key) |
| 107 |
local value |
| 108 |
local key, value = pairs_gen(tab, key) |
| 109 |
return key, key, value |
| 110 |
end |
| 111 |
|
| 112 |
local rawiter = function(obj, param, state) |
| 113 |
assert(obj ~= nil, "invalid iterator") |
| 114 |
if type(obj) == "table" then |
| 115 |
local mt = getmetatable(obj); |
| 116 |
if mt ~= nil then |
| 117 |
if mt == iterator_mt then |
| 118 |
return obj.gen, obj.param, obj.state |
| 119 |
elseif mt.__ipairs ~= nil then |
| 120 |
return mt.__ipairs(obj) |
| 121 |
elseif mt.__pairs ~= nil then |
| 122 |
return mt.__pairs(obj) |
| 123 |
end |
| 124 |
end |
| 125 |
if #obj > 0 then |
| 126 |
-- array |
| 127 |
return ipairs(obj) |
| 128 |
else |
| 129 |
-- hash |
| 130 |
return map_gen, obj, nil |
| 131 |
end |
| 132 |
elseif (type(obj) == "function") then |
| 133 |
return obj, param, state |
| 134 |
elseif (type(obj) == "string") then |
| 135 |
if #obj == 0 then |
| 136 |
return nil_gen, nil, nil |
| 137 |
end |
| 138 |
return string_gen, obj, 0 |
| 139 |
elseif (type(obj) == "userdata" and obj.cookie == text_cookie) then |
| 140 |
if #obj == 0 then |
| 141 |
return nil_gen, nil, nil |
| 142 |
end |
| 143 |
return text_gen, obj, 0 |
| 144 |
end |
| 145 |
error(string.format('object %s of type "%s" is not iterable', |
| 146 |
obj, type(obj))) |
| 147 |
end |
| 148 |
|
| 149 |
local iter = function(obj, param, state) |
| 150 |
return wrap(rawiter(obj, param, state)) |
| 151 |
end |
| 152 |
exports.iter = iter |
| 153 |
|
| 154 |
local method0 = function(fun) |
| 155 |
return function(self) |
| 156 |
return fun(self.gen, self.param, self.state) |
| 157 |
end |
| 158 |
end |
| 159 |
|
| 160 |
local method1 = function(fun) |
| 161 |
return function(self, arg1) |
| 162 |
return fun(arg1, self.gen, self.param, self.state) |
| 163 |
end |
| 164 |
end |
| 165 |
|
| 166 |
local method2 = function(fun) |
| 167 |
return function(self, arg1, arg2) |
| 168 |
return fun(arg1, arg2, self.gen, self.param, self.state) |
| 169 |
end |
| 170 |
end |
| 171 |
|
| 172 |
local export0 = function(fun) |
| 173 |
return function(gen, param, state) |
| 174 |
return fun(rawiter(gen, param, state)) |
| 175 |
end |
| 176 |
end |
| 177 |
|
| 178 |
local export1 = function(fun) |
| 179 |
return function(arg1, gen, param, state) |
| 180 |
return fun(arg1, rawiter(gen, param, state)) |
| 181 |
end |
| 182 |
end |
| 183 |
|
| 184 |
local export2 = function(fun) |
| 185 |
return function(arg1, arg2, gen, param, state) |
| 186 |
return fun(arg1, arg2, rawiter(gen, param, state)) |
| 187 |
end |
| 188 |
end |
| 189 |
|
| 190 |
local each = function(fun, gen, param, state) |
| 191 |
repeat |
| 192 |
state = call_if_not_empty(fun, gen(param, state)) |
| 193 |
until state == nil |
| 194 |
end |
| 195 |
methods.each = method1(each) |
| 196 |
exports.each = export1(each) |
| 197 |
methods.for_each = methods.each |
| 198 |
exports.for_each = exports.each |
| 199 |
methods.foreach = methods.each |
| 200 |
exports.foreach = exports.each |
| 201 |
|
| 202 |
-------------------------------------------------------------------------------- |
| 203 |
-- Generators |
| 204 |
-------------------------------------------------------------------------------- |
| 205 |
|
| 206 |
local range_gen = function(param, state) |
| 207 |
local stop, step = param[1], param[2] |
| 208 |
local state = state + step |
| 209 |
if state > stop then |
| 210 |
return nil |
| 211 |
end |
| 212 |
return state, state |
| 213 |
end |
| 214 |
|
| 215 |
local range_rev_gen = function(param, state) |
| 216 |
local stop, step = param[1], param[2] |
| 217 |
local state = state + step |
| 218 |
if state < stop then |
| 219 |
return nil |
| 220 |
end |
| 221 |
return state, state |
| 222 |
end |
| 223 |
|
| 224 |
local range = function(start, stop, step) |
| 225 |
if step == nil then |
| 226 |
if stop == nil then |
| 227 |
if start == 0 then |
| 228 |
return nil_gen, nil, nil |
| 229 |
end |
| 230 |
stop = start |
| 231 |
start = stop > 0 and 1 or -1 |
| 232 |
end |
| 233 |
step = start <= stop and 1 or -1 |
| 234 |
end |
| 235 |
|
| 236 |
assert(type(start) == "number", "start must be a number") |
| 237 |
assert(type(stop) == "number", "stop must be a number") |
| 238 |
assert(type(step) == "number", "step must be a number") |
| 239 |
assert(step ~= 0, "step must not be zero") |
| 240 |
|
| 241 |
if (step > 0) then |
| 242 |
return wrap(range_gen, {stop, step}, start - step) |
| 243 |
elseif (step < 0) then |
| 244 |
return wrap(range_rev_gen, {stop, step}, start - step) |
| 245 |
end |
| 246 |
end |
| 247 |
exports.range = range |
| 248 |
|
| 249 |
local duplicate_table_gen = function(param_x, state_x) |
| 250 |
return state_x + 1, unpack(param_x) |
| 251 |
end |
| 252 |
|
| 253 |
local duplicate_fun_gen = function(param_x, state_x) |
| 254 |
return state_x + 1, param_x(state_x) |
| 255 |
end |
| 256 |
|
| 257 |
local duplicate_gen = function(param_x, state_x) |
| 258 |
return state_x + 1, param_x |
| 259 |
end |
| 260 |
|
| 261 |
local duplicate = function(...) |
| 262 |
if select('#', ...) <= 1 then |
| 263 |
return wrap(duplicate_gen, select(1, ...), 0) |
| 264 |
else |
| 265 |
return wrap(duplicate_table_gen, {...}, 0) |
| 266 |
end |
| 267 |
end |
| 268 |
exports.duplicate = duplicate |
| 269 |
exports.replicate = duplicate |
| 270 |
exports.xrepeat = duplicate |
| 271 |
|
| 272 |
local tabulate = function(fun) |
| 273 |
assert(type(fun) == "function") |
| 274 |
return wrap(duplicate_fun_gen, fun, 0) |
| 275 |
end |
| 276 |
exports.tabulate = tabulate |
| 277 |
|
| 278 |
local zeros = function() |
| 279 |
return wrap(duplicate_gen, 0, 0) |
| 280 |
end |
| 281 |
exports.zeros = zeros |
| 282 |
|
| 283 |
local ones = function() |
| 284 |
return wrap(duplicate_gen, 1, 0) |
| 285 |
end |
| 286 |
exports.ones = ones |
| 287 |
|
| 288 |
local rands_gen = function(param_x, _state_x) |
| 289 |
return 0, math.random(param_x[1], param_x[2]) |
| 290 |
end |
| 291 |
|
| 292 |
local rands_nil_gen = function(_param_x, _state_x) |
| 293 |
return 0, math.random() |
| 294 |
end |
| 295 |
|
| 296 |
local rands = function(n, m) |
| 297 |
if n == nil and m == nil then |
| 298 |
return wrap(rands_nil_gen, 0, 0) |
| 299 |
end |
| 300 |
assert(type(n) == "number", "invalid first arg to rands") |
| 301 |
if m == nil then |
| 302 |
m = n |
| 303 |
n = 0 |
| 304 |
else |
| 305 |
assert(type(m) == "number", "invalid second arg to rands") |
| 306 |
end |
| 307 |
assert(n < m, "empty interval") |
| 308 |
return wrap(rands_gen, {n, m - 1}, 0) |
| 309 |
end |
| 310 |
exports.rands = rands |
| 311 |
|
| 312 |
-------------------------------------------------------------------------------- |
| 313 |
-- Slicing |
| 314 |
-------------------------------------------------------------------------------- |
| 315 |
|
| 316 |
local nth = function(n, gen_x, param_x, state_x) |
| 317 |
assert(n > 0, "invalid first argument to nth") |
| 318 |
-- An optimization for arrays and strings |
| 319 |
if gen_x == ipairs_gen then |
| 320 |
return param_x[n] |
| 321 |
elseif gen_x == string_gen then |
| 322 |
if n <= #param_x then |
| 323 |
return string.sub(param_x, n, n) |
| 324 |
else |
| 325 |
return nil |
| 326 |
end |
| 327 |
end |
| 328 |
for i=1,n-1,1 do |
| 329 |
state_x = gen_x(param_x, state_x) |
| 330 |
if state_x == nil then |
| 331 |
return nil |
| 332 |
end |
| 333 |
end |
| 334 |
return return_if_not_empty(gen_x(param_x, state_x)) |
| 335 |
end |
| 336 |
methods.nth = method1(nth) |
| 337 |
exports.nth = export1(nth) |
| 338 |
|
| 339 |
local head_call = function(state, ...) |
| 340 |
if state == nil then |
| 341 |
error("head: iterator is empty") |
| 342 |
end |
| 343 |
return ... |
| 344 |
end |
| 345 |
|
| 346 |
local head = function(gen, param, state) |
| 347 |
return head_call(gen(param, state)) |
| 348 |
end |
| 349 |
methods.head = method0(head) |
| 350 |
exports.head = export0(head) |
| 351 |
exports.car = exports.head |
| 352 |
methods.car = methods.head |
| 353 |
|
| 354 |
local tail = function(gen, param, state) |
| 355 |
state = gen(param, state) |
| 356 |
if state == nil then |
| 357 |
return wrap(nil_gen, nil, nil) |
| 358 |
end |
| 359 |
return wrap(gen, param, state) |
| 360 |
end |
| 361 |
methods.tail = method0(tail) |
| 362 |
exports.tail = export0(tail) |
| 363 |
exports.cdr = exports.tail |
| 364 |
methods.cdr = methods.tail |
| 365 |
|
| 366 |
local take_n_gen_x = function(i, state_x, ...) |
| 367 |
if state_x == nil then |
| 368 |
return nil |
| 369 |
end |
| 370 |
return {i, state_x}, ... |
| 371 |
end |
| 372 |
|
| 373 |
local take_n_gen = function(param, state) |
| 374 |
local n, gen_x, param_x = param[1], param[2], param[3] |
| 375 |
local i, state_x = state[1], state[2] |
| 376 |
if i >= n then |
| 377 |
return nil |
| 378 |
end |
| 379 |
return take_n_gen_x(i + 1, gen_x(param_x, state_x)) |
| 380 |
end |
| 381 |
|
| 382 |
local take_n = function(n, gen, param, state) |
| 383 |
assert(n >= 0, "invalid first argument to take_n") |
| 384 |
return wrap(take_n_gen, {n, gen, param}, {0, state}) |
| 385 |
end |
| 386 |
methods.take_n = method1(take_n) |
| 387 |
exports.take_n = export1(take_n) |
| 388 |
|
| 389 |
local take_while_gen_x = function(fun, state_x, ...) |
| 390 |
if state_x == nil or not fun(...) then |
| 391 |
return nil |
| 392 |
end |
| 393 |
return state_x, ... |
| 394 |
end |
| 395 |
|
| 396 |
local take_while_gen = function(param, state_x) |
| 397 |
local fun, gen_x, param_x = param[1], param[2], param[3] |
| 398 |
return take_while_gen_x(fun, gen_x(param_x, state_x)) |
| 399 |
end |
| 400 |
|
| 401 |
local take_while = function(fun, gen, param, state) |
| 402 |
assert(type(fun) == "function", "invalid first argument to take_while") |
| 403 |
return wrap(take_while_gen, {fun, gen, param}, state) |
| 404 |
end |
| 405 |
methods.take_while = method1(take_while) |
| 406 |
exports.take_while = export1(take_while) |
| 407 |
|
| 408 |
local take = function(n_or_fun, gen, param, state) |
| 409 |
if type(n_or_fun) == "number" then |
| 410 |
return take_n(n_or_fun, gen, param, state) |
| 411 |
else |
| 412 |
return take_while(n_or_fun, gen, param, state) |
| 413 |
end |
| 414 |
end |
| 415 |
methods.take = method1(take) |
| 416 |
exports.take = export1(take) |
| 417 |
|
| 418 |
local drop_n = function(n, gen, param, state) |
| 419 |
assert(n >= 0, "invalid first argument to drop_n") |
| 420 |
local i |
| 421 |
for i=1,n,1 do |
| 422 |
state = gen(param, state) |
| 423 |
if state == nil then |
| 424 |
return wrap(nil_gen, nil, nil) |
| 425 |
end |
| 426 |
end |
| 427 |
return wrap(gen, param, state) |
| 428 |
end |
| 429 |
methods.drop_n = method1(drop_n) |
| 430 |
exports.drop_n = export1(drop_n) |
| 431 |
|
| 432 |
local drop_while_x = function(fun, state_x, ...) |
| 433 |
if state_x == nil or not fun(...) then |
| 434 |
return state_x, false |
| 435 |
end |
| 436 |
return state_x, true, ... |
| 437 |
end |
| 438 |
|
| 439 |
local drop_while = function(fun, gen_x, param_x, state_x) |
| 440 |
assert(type(fun) == "function", "invalid first argument to drop_while") |
| 441 |
local cont, state_x_prev |
| 442 |
repeat |
| 443 |
state_x_prev = deepcopy(state_x) |
| 444 |
state_x, cont = drop_while_x(fun, gen_x(param_x, state_x)) |
| 445 |
until not cont |
| 446 |
if state_x == nil then |
| 447 |
return wrap(nil_gen, nil, nil) |
| 448 |
end |
| 449 |
return wrap(gen_x, param_x, state_x_prev) |
| 450 |
end |
| 451 |
methods.drop_while = method1(drop_while) |
| 452 |
exports.drop_while = export1(drop_while) |
| 453 |
|
| 454 |
local drop = function(n_or_fun, gen_x, param_x, state_x) |
| 455 |
if type(n_or_fun) == "number" then |
| 456 |
return drop_n(n_or_fun, gen_x, param_x, state_x) |
| 457 |
else |
| 458 |
return drop_while(n_or_fun, gen_x, param_x, state_x) |
| 459 |
end |
| 460 |
end |
| 461 |
methods.drop = method1(drop) |
| 462 |
exports.drop = export1(drop) |
| 463 |
|
| 464 |
local split = function(n_or_fun, gen_x, param_x, state_x) |
| 465 |
return take(n_or_fun, gen_x, param_x, state_x), |
| 466 |
drop(n_or_fun, gen_x, param_x, state_x) |
| 467 |
end |
| 468 |
methods.split = method1(split) |
| 469 |
exports.split = export1(split) |
| 470 |
methods.split_at = methods.split |
| 471 |
exports.split_at = exports.split |
| 472 |
methods.span = methods.split |
| 473 |
exports.span = exports.split |
| 474 |
|
| 475 |
-------------------------------------------------------------------------------- |
| 476 |
-- Indexing |
| 477 |
-------------------------------------------------------------------------------- |
| 478 |
|
| 479 |
local index = function(x, gen, param, state) |
| 480 |
local i = 1 |
| 481 |
for _k, r in gen, param, state do |
| 482 |
if r == x then |
| 483 |
return i |
| 484 |
end |
| 485 |
i = i + 1 |
| 486 |
end |
| 487 |
return nil |
| 488 |
end |
| 489 |
methods.index = method1(index) |
| 490 |
exports.index = export1(index) |
| 491 |
methods.index_of = methods.index |
| 492 |
exports.index_of = exports.index |
| 493 |
methods.elem_index = methods.index |
| 494 |
exports.elem_index = exports.index |
| 495 |
|
| 496 |
local indexes_gen = function(param, state) |
| 497 |
local x, gen_x, param_x = param[1], param[2], param[3] |
| 498 |
local i, state_x = state[1], state[2] |
| 499 |
local r |
| 500 |
while true do |
| 501 |
state_x, r = gen_x(param_x, state_x) |
| 502 |
if state_x == nil then |
| 503 |
return nil |
| 504 |
end |
| 505 |
i = i + 1 |
| 506 |
if r == x then |
| 507 |
return {i, state_x}, i |
| 508 |
end |
| 509 |
end |
| 510 |
end |
| 511 |
|
| 512 |
local indexes = function(x, gen, param, state) |
| 513 |
return wrap(indexes_gen, {x, gen, param}, {0, state}) |
| 514 |
end |
| 515 |
methods.indexes = method1(indexes) |
| 516 |
exports.indexes = export1(indexes) |
| 517 |
methods.elem_indexes = methods.indexes |
| 518 |
exports.elem_indexes = exports.indexes |
| 519 |
methods.indices = methods.indexes |
| 520 |
exports.indices = exports.indexes |
| 521 |
methods.elem_indices = methods.indexes |
| 522 |
exports.elem_indices = exports.indexes |
| 523 |
|
| 524 |
-------------------------------------------------------------------------------- |
| 525 |
-- Filtering |
| 526 |
-------------------------------------------------------------------------------- |
| 527 |
|
| 528 |
local filter1_gen = function(fun, gen_x, param_x, state_x, a) |
| 529 |
while true do |
| 530 |
if state_x == nil or fun(a) then break; end |
| 531 |
state_x, a = gen_x(param_x, state_x) |
| 532 |
end |
| 533 |
return state_x, a |
| 534 |
end |
| 535 |
|
| 536 |
-- call each other |
| 537 |
local filterm_gen |
| 538 |
local filterm_gen_shrink = function(fun, gen_x, param_x, state_x) |
| 539 |
return filterm_gen(fun, gen_x, param_x, gen_x(param_x, state_x)) |
| 540 |
end |
| 541 |
|
| 542 |
filterm_gen = function(fun, gen_x, param_x, state_x, ...) |
| 543 |
if state_x == nil then |
| 544 |
return nil |
| 545 |
end |
| 546 |
if fun(...) then |
| 547 |
return state_x, ... |
| 548 |
end |
| 549 |
return filterm_gen_shrink(fun, gen_x, param_x, state_x) |
| 550 |
end |
| 551 |
|
| 552 |
local filter_detect = function(fun, gen_x, param_x, state_x, ...) |
| 553 |
if select('#', ...) < 2 then |
| 554 |
return filter1_gen(fun, gen_x, param_x, state_x, ...) |
| 555 |
else |
| 556 |
return filterm_gen(fun, gen_x, param_x, state_x, ...) |
| 557 |
end |
| 558 |
end |
| 559 |
|
| 560 |
local filter_gen = function(param, state_x) |
| 561 |
local fun, gen_x, param_x = param[1], param[2], param[3] |
| 562 |
return filter_detect(fun, gen_x, param_x, gen_x(param_x, state_x)) |
| 563 |
end |
| 564 |
|
| 565 |
local filter = function(fun, gen, param, state) |
| 566 |
return wrap(filter_gen, {fun, gen, param}, state) |
| 567 |
end |
| 568 |
methods.filter = method1(filter) |
| 569 |
exports.filter = export1(filter) |
| 570 |
methods.remove_if = methods.filter |
| 571 |
exports.remove_if = exports.filter |
| 572 |
|
| 573 |
local grep = function(fun_or_regexp, gen, param, state) |
| 574 |
local fun = fun_or_regexp |
| 575 |
if type(fun_or_regexp) == "string" then |
| 576 |
fun = function(x) return string.find(x, fun_or_regexp) ~= nil end |
| 577 |
end |
| 578 |
return filter(fun, gen, param, state) |
| 579 |
end |
| 580 |
methods.grep = method1(grep) |
| 581 |
exports.grep = export1(grep) |
| 582 |
|
| 583 |
local partition = function(fun, gen, param, state) |
| 584 |
local neg_fun = function(...) |
| 585 |
return not fun(...) |
| 586 |
end |
| 587 |
return filter(fun, gen, param, state), |
| 588 |
filter(neg_fun, gen, param, state) |
| 589 |
end |
| 590 |
methods.partition = method1(partition) |
| 591 |
exports.partition = export1(partition) |
| 592 |
|
| 593 |
-------------------------------------------------------------------------------- |
| 594 |
-- Reducing |
| 595 |
-------------------------------------------------------------------------------- |
| 596 |
|
| 597 |
local foldl_call = function(fun, start, state, ...) |
| 598 |
if state == nil then |
| 599 |
return nil, start |
| 600 |
end |
| 601 |
return state, fun(start, ...) |
| 602 |
end |
| 603 |
|
| 604 |
local foldl = function(fun, start, gen_x, param_x, state_x) |
| 605 |
while true do |
| 606 |
state_x, start = foldl_call(fun, start, gen_x(param_x, state_x)) |
| 607 |
if state_x == nil then |
| 608 |
break; |
| 609 |
end |
| 610 |
end |
| 611 |
return start |
| 612 |
end |
| 613 |
methods.foldl = method2(foldl) |
| 614 |
exports.foldl = export2(foldl) |
| 615 |
methods.reduce = methods.foldl |
| 616 |
exports.reduce = exports.foldl |
| 617 |
|
| 618 |
local length = function(gen, param, state) |
| 619 |
if gen == ipairs_gen or gen == string_gen then |
| 620 |
return #param |
| 621 |
end |
| 622 |
local len = 0 |
| 623 |
repeat |
| 624 |
state = gen(param, state) |
| 625 |
len = len + 1 |
| 626 |
until state == nil |
| 627 |
return len - 1 |
| 628 |
end |
| 629 |
methods.length = method0(length) |
| 630 |
exports.length = export0(length) |
| 631 |
|
| 632 |
local is_null = function(gen, param, state) |
| 633 |
return gen(param, deepcopy(state)) == nil |
| 634 |
end |
| 635 |
methods.is_null = method0(is_null) |
| 636 |
exports.is_null = export0(is_null) |
| 637 |
|
| 638 |
local is_prefix_of = function(iter_x, iter_y) |
| 639 |
local gen_x, param_x, state_x = iter(iter_x) |
| 640 |
local gen_y, param_y, state_y = iter(iter_y) |
| 641 |
|
| 642 |
local r_x, r_y |
| 643 |
for i=1,10,1 do |
| 644 |
state_x, r_x = gen_x(param_x, state_x) |
| 645 |
state_y, r_y = gen_y(param_y, state_y) |
| 646 |
if state_x == nil then |
| 647 |
return true |
| 648 |
end |
| 649 |
if state_y == nil or r_x ~= r_y then |
| 650 |
return false |
| 651 |
end |
| 652 |
end |
| 653 |
end |
| 654 |
methods.is_prefix_of = is_prefix_of |
| 655 |
exports.is_prefix_of = is_prefix_of |
| 656 |
|
| 657 |
local all = function(fun, gen_x, param_x, state_x) |
| 658 |
local r |
| 659 |
repeat |
| 660 |
state_x, r = call_if_not_empty(fun, gen_x(param_x, state_x)) |
| 661 |
until state_x == nil or not r |
| 662 |
return state_x == nil |
| 663 |
end |
| 664 |
methods.all = method1(all) |
| 665 |
exports.all = export1(all) |
| 666 |
methods.every = methods.all |
| 667 |
exports.every = exports.all |
| 668 |
|
| 669 |
local any = function(fun, gen_x, param_x, state_x) |
| 670 |
local r |
| 671 |
repeat |
| 672 |
state_x, r = call_if_not_empty(fun, gen_x(param_x, state_x)) |
| 673 |
until state_x == nil or r |
| 674 |
return not not r |
| 675 |
end |
| 676 |
methods.any = method1(any) |
| 677 |
exports.any = export1(any) |
| 678 |
methods.some = methods.any |
| 679 |
exports.some = exports.any |
| 680 |
|
| 681 |
local sum = function(gen, param, state) |
| 682 |
local s = 0 |
| 683 |
local r = 0 |
| 684 |
repeat |
| 685 |
s = s + r |
| 686 |
state, r = gen(param, state) |
| 687 |
until state == nil |
| 688 |
return s |
| 689 |
end |
| 690 |
methods.sum = method0(sum) |
| 691 |
exports.sum = export0(sum) |
| 692 |
|
| 693 |
local product = function(gen, param, state) |
| 694 |
local p = 1 |
| 695 |
local r = 1 |
| 696 |
repeat |
| 697 |
p = p * r |
| 698 |
state, r = gen(param, state) |
| 699 |
until state == nil |
| 700 |
return p |
| 701 |
end |
| 702 |
methods.product = method0(product) |
| 703 |
exports.product = export0(product) |
| 704 |
|
| 705 |
local min_cmp = function(m, n) |
| 706 |
if n < m then return n else return m end |
| 707 |
end |
| 708 |
|
| 709 |
local max_cmp = function(m, n) |
| 710 |
if n > m then return n else return m end |
| 711 |
end |
| 712 |
|
| 713 |
local min = function(gen, param, state) |
| 714 |
local state, m = gen(param, state) |
| 715 |
if state == nil then |
| 716 |
error("min: iterator is empty") |
| 717 |
end |
| 718 |
|
| 719 |
local cmp |
| 720 |
if type(m) == "number" then |
| 721 |
-- An optimization: use math.min for numbers |
| 722 |
cmp = math.min |
| 723 |
else |
| 724 |
cmp = min_cmp |
| 725 |
end |
| 726 |
|
| 727 |
for _, r in gen, param, state do |
| 728 |
m = cmp(m, r) |
| 729 |
end |
| 730 |
return m |
| 731 |
end |
| 732 |
methods.min = method0(min) |
| 733 |
exports.min = export0(min) |
| 734 |
methods.minimum = methods.min |
| 735 |
exports.minimum = exports.min |
| 736 |
|
| 737 |
local min_by = function(cmp, gen_x, param_x, state_x) |
| 738 |
local state_x, m = gen_x(param_x, state_x) |
| 739 |
if state_x == nil then |
| 740 |
error("min: iterator is empty") |
| 741 |
end |
| 742 |
|
| 743 |
for _, r in gen_x, param_x, state_x do |
| 744 |
m = cmp(m, r) |
| 745 |
end |
| 746 |
return m |
| 747 |
end |
| 748 |
methods.min_by = method1(min_by) |
| 749 |
exports.min_by = export1(min_by) |
| 750 |
methods.minimum_by = methods.min_by |
| 751 |
exports.minimum_by = exports.min_by |
| 752 |
|
| 753 |
local max = function(gen_x, param_x, state_x) |
| 754 |
local state_x, m = gen_x(param_x, state_x) |
| 755 |
if state_x == nil then |
| 756 |
error("max: iterator is empty") |
| 757 |
end |
| 758 |
|
| 759 |
local cmp |
| 760 |
if type(m) == "number" then |
| 761 |
-- An optimization: use math.max for numbers |
| 762 |
cmp = math.max |
| 763 |
else |
| 764 |
cmp = max_cmp |
| 765 |
end |
| 766 |
|
| 767 |
for _, r in gen_x, param_x, state_x do |
| 768 |
m = cmp(m, r) |
| 769 |
end |
| 770 |
return m |
| 771 |
end |
| 772 |
methods.max = method0(max) |
| 773 |
exports.max = export0(max) |
| 774 |
methods.maximum = methods.max |
| 775 |
exports.maximum = exports.max |
| 776 |
|
| 777 |
local max_by = function(cmp, gen_x, param_x, state_x) |
| 778 |
local state_x, m = gen_x(param_x, state_x) |
| 779 |
if state_x == nil then |
| 780 |
error("max: iterator is empty") |
| 781 |
end |
| 782 |
|
| 783 |
for _, r in gen_x, param_x, state_x do |
| 784 |
m = cmp(m, r) |
| 785 |
end |
| 786 |
return m |
| 787 |
end |
| 788 |
methods.max_by = method1(max_by) |
| 789 |
exports.max_by = export1(max_by) |
| 790 |
methods.maximum_by = methods.maximum_by |
| 791 |
exports.maximum_by = exports.maximum_by |
| 792 |
|
| 793 |
local totable = function(gen_x, param_x, state_x) |
| 794 |
local tab, key, val = {} |
| 795 |
while true do |
| 796 |
state_x, val = gen_x(param_x, state_x) |
| 797 |
if state_x == nil then |
| 798 |
break |
| 799 |
end |
| 800 |
table.insert(tab, val) |
| 801 |
end |
| 802 |
return tab |
| 803 |
end |
| 804 |
methods.totable = method0(totable) |
| 805 |
exports.totable = export0(totable) |
| 806 |
|
| 807 |
local tomap = function(gen_x, param_x, state_x) |
| 808 |
local tab, key, val = {} |
| 809 |
while true do |
| 810 |
state_x, key, val = gen_x(param_x, state_x) |
| 811 |
if state_x == nil then |
| 812 |
break |
| 813 |
end |
| 814 |
tab[key] = val |
| 815 |
end |
| 816 |
return tab |
| 817 |
end |
| 818 |
methods.tomap = method0(tomap) |
| 819 |
exports.tomap = export0(tomap) |
| 820 |
|
| 821 |
-------------------------------------------------------------------------------- |
| 822 |
-- Transformations |
| 823 |
-------------------------------------------------------------------------------- |
| 824 |
|
| 825 |
local map_gen = function(param, state) |
| 826 |
local gen_x, param_x, fun = param[1], param[2], param[3] |
| 827 |
return call_if_not_empty(fun, gen_x(param_x, state)) |
| 828 |
end |
| 829 |
|
| 830 |
local map = function(fun, gen, param, state) |
| 831 |
return wrap(map_gen, {gen, param, fun}, state) |
| 832 |
end |
| 833 |
methods.map = method1(map) |
| 834 |
exports.map = export1(map) |
| 835 |
|
| 836 |
local enumerate_gen_call = function(state, i, state_x, ...) |
| 837 |
if state_x == nil then |
| 838 |
return nil |
| 839 |
end |
| 840 |
return {i + 1, state_x}, i, ... |
| 841 |
end |
| 842 |
|
| 843 |
local enumerate_gen = function(param, state) |
| 844 |
local gen_x, param_x = param[1], param[2] |
| 845 |
local i, state_x = state[1], state[2] |
| 846 |
return enumerate_gen_call(state, i, gen_x(param_x, state_x)) |
| 847 |
end |
| 848 |
|
| 849 |
local enumerate = function(gen, param, state) |
| 850 |
return wrap(enumerate_gen, {gen, param}, {1, state}) |
| 851 |
end |
| 852 |
methods.enumerate = method0(enumerate) |
| 853 |
exports.enumerate = export0(enumerate) |
| 854 |
|
| 855 |
local intersperse_call = function(i, state_x, ...) |
| 856 |
if state_x == nil then |
| 857 |
return nil |
| 858 |
end |
| 859 |
return {i + 1, state_x}, ... |
| 860 |
end |
| 861 |
|
| 862 |
local intersperse_gen = function(param, state) |
| 863 |
local x, gen_x, param_x = param[1], param[2], param[3] |
| 864 |
local i, state_x = state[1], state[2] |
| 865 |
if i % 2 == 1 then |
| 866 |
return {i + 1, state_x}, x |
| 867 |
else |
| 868 |
return intersperse_call(i, gen_x(param_x, state_x)) |
| 869 |
end |
| 870 |
end |
| 871 |
|
| 872 |
-- TODO: interperse must not add x to the tail |
| 873 |
local intersperse = function(x, gen, param, state) |
| 874 |
return wrap(intersperse_gen, {x, gen, param}, {0, state}) |
| 875 |
end |
| 876 |
methods.intersperse = method1(intersperse) |
| 877 |
exports.intersperse = export1(intersperse) |
| 878 |
|
| 879 |
-------------------------------------------------------------------------------- |
| 880 |
-- Compositions |
| 881 |
-------------------------------------------------------------------------------- |
| 882 |
|
| 883 |
local function zip_gen_r(param, state, state_new, ...) |
| 884 |
if #state_new == #param / 2 then |
| 885 |
return state_new, ... |
| 886 |
end |
| 887 |
|
| 888 |
local i = #state_new + 1 |
| 889 |
local gen_x, param_x = param[2 * i - 1], param[2 * i] |
| 890 |
local state_x, r = gen_x(param_x, state[i]) |
| 891 |
if state_x == nil then |
| 892 |
return nil |
| 893 |
end |
| 894 |
table.insert(state_new, state_x) |
| 895 |
return zip_gen_r(param, state, state_new, r, ...) |
| 896 |
end |
| 897 |
|
| 898 |
local zip_gen = function(param, state) |
| 899 |
return zip_gen_r(param, state, {}) |
| 900 |
end |
| 901 |
|
| 902 |
-- A special hack for zip/chain to skip last two state, if a wrapped iterator |
| 903 |
-- has been passed |
| 904 |
local numargs = function(...) |
| 905 |
local n = select('#', ...) |
| 906 |
if n >= 3 then |
| 907 |
-- Fix last argument |
| 908 |
local it = select(n - 2, ...) |
| 909 |
if type(it) == 'table' and getmetatable(it) == iterator_mt and |
| 910 |
it.param == select(n - 1, ...) and it.state == select(n, ...) then |
| 911 |
return n - 2 |
| 912 |
end |
| 913 |
end |
| 914 |
return n |
| 915 |
end |
| 916 |
|
| 917 |
local zip = function(...) |
| 918 |
local n = numargs(...) |
| 919 |
if n == 0 then |
| 920 |
return wrap(nil_gen, nil, nil) |
| 921 |
end |
| 922 |
local param = { [2 * n] = 0 } |
| 923 |
local state = { [n] = 0 } |
| 924 |
|
| 925 |
local i, gen_x, param_x, state_x |
| 926 |
for i=1,n,1 do |
| 927 |
local it = select(n - i + 1, ...) |
| 928 |
gen_x, param_x, state_x = rawiter(it) |
| 929 |
param[2 * i - 1] = gen_x |
| 930 |
param[2 * i] = param_x |
| 931 |
state[i] = state_x |
| 932 |
end |
| 933 |
|
| 934 |
return wrap(zip_gen, param, state) |
| 935 |
end |
| 936 |
methods.zip = zip |
| 937 |
exports.zip = zip |
| 938 |
|
| 939 |
local cycle_gen_call = function(param, state_x, ...) |
| 940 |
if state_x == nil then |
| 941 |
local gen_x, param_x, state_x0 = param[1], param[2], param[3] |
| 942 |
return gen_x(param_x, deepcopy(state_x0)) |
| 943 |
end |
| 944 |
return state_x, ... |
| 945 |
end |
| 946 |
|
| 947 |
local cycle_gen = function(param, state_x) |
| 948 |
local gen_x, param_x, state_x0 = param[1], param[2], param[3] |
| 949 |
return cycle_gen_call(param, gen_x(param_x, state_x)) |
| 950 |
end |
| 951 |
|
| 952 |
local cycle = function(gen, param, state) |
| 953 |
return wrap(cycle_gen, {gen, param, state}, deepcopy(state)) |
| 954 |
end |
| 955 |
methods.cycle = method0(cycle) |
| 956 |
exports.cycle = export0(cycle) |
| 957 |
|
| 958 |
-- call each other |
| 959 |
local chain_gen_r1 |
| 960 |
local chain_gen_r2 = function(param, state, state_x, ...) |
| 961 |
if state_x == nil then |
| 962 |
local i = state[1] |
| 963 |
i = i + 1 |
| 964 |
if param[3 * i - 1] == nil then |
| 965 |
return nil |
| 966 |
end |
| 967 |
local state_x = param[3 * i] |
| 968 |
return chain_gen_r1(param, {i, state_x}) |
| 969 |
end |
| 970 |
return {state[1], state_x}, ... |
| 971 |
end |
| 972 |
|
| 973 |
chain_gen_r1 = function(param, state) |
| 974 |
local i, state_x = state[1], state[2] |
| 975 |
local gen_x, param_x = param[3 * i - 2], param[3 * i - 1] |
| 976 |
return chain_gen_r2(param, state, gen_x(param_x, state[2])) |
| 977 |
end |
| 978 |
|
| 979 |
local chain = function(...) |
| 980 |
local n = numargs(...) |
| 981 |
if n == 0 then |
| 982 |
return wrap(nil_gen, nil, nil) |
| 983 |
end |
| 984 |
|
| 985 |
local param = { [3 * n] = 0 } |
| 986 |
local i, gen_x, param_x, state_x |
| 987 |
for i=1,n,1 do |
| 988 |
local elem = select(i, ...) |
| 989 |
gen_x, param_x, state_x = iter(elem) |
| 990 |
param[3 * i - 2] = gen_x |
| 991 |
param[3 * i - 1] = param_x |
| 992 |
param[3 * i] = state_x |
| 993 |
end |
| 994 |
|
| 995 |
return wrap(chain_gen_r1, param, {1, param[3]}) |
| 996 |
end |
| 997 |
methods.chain = chain |
| 998 |
exports.chain = chain |
| 999 |
|
| 1000 |
-------------------------------------------------------------------------------- |
| 1001 |
-- Operators |
| 1002 |
-------------------------------------------------------------------------------- |
| 1003 |
|
| 1004 |
local operator = { |
| 1005 |
---------------------------------------------------------------------------- |
| 1006 |
-- Comparison operators |
| 1007 |
---------------------------------------------------------------------------- |
| 1008 |
lt = function(a, b) return a < b end, |
| 1009 |
le = function(a, b) return a <= b end, |
| 1010 |
eq = function(a, b) return a == b end, |
| 1011 |
ne = function(a, b) return a ~= b end, |
| 1012 |
ge = function(a, b) return a >= b end, |
| 1013 |
gt = function(a, b) return a > b end, |
| 1014 |
|
| 1015 |
---------------------------------------------------------------------------- |
| 1016 |
-- Arithmetic operators |
| 1017 |
---------------------------------------------------------------------------- |
| 1018 |
add = function(a, b) return a + b end, |
| 1019 |
div = function(a, b) return a / b end, |
| 1020 |
floordiv = function(a, b) return math.floor(a/b) end, |
| 1021 |
intdiv = function(a, b) |
| 1022 |
local q = a / b |
| 1023 |
if a >= 0 then return math.floor(q) else return math.ceil(q) end |
| 1024 |
end, |
| 1025 |
mod = function(a, b) return a % b end, |
| 1026 |
mul = function(a, b) return a * b end, |
| 1027 |
neq = function(a) return -a end, |
| 1028 |
unm = function(a) return -a end, -- an alias |
| 1029 |
pow = function(a, b) return a ^ b end, |
| 1030 |
sub = function(a, b) return a - b end, |
| 1031 |
truediv = function(a, b) return a / b end, |
| 1032 |
|
| 1033 |
---------------------------------------------------------------------------- |
| 1034 |
-- String operators |
| 1035 |
---------------------------------------------------------------------------- |
| 1036 |
concat = function(a, b) return a..b end, |
| 1037 |
len = function(a) return #a end, |
| 1038 |
length = function(a) return #a end, -- an alias |
| 1039 |
|
| 1040 |
---------------------------------------------------------------------------- |
| 1041 |
-- Logical operators |
| 1042 |
---------------------------------------------------------------------------- |
| 1043 |
land = function(a, b) return a and b end, |
| 1044 |
lor = function(a, b) return a or b end, |
| 1045 |
lnot = function(a) return not a end, |
| 1046 |
truth = function(a) return not not a end, |
| 1047 |
} |
| 1048 |
exports.operator = operator |
| 1049 |
methods.operator = operator |
| 1050 |
exports.op = operator |
| 1051 |
methods.op = operator |
| 1052 |
|
| 1053 |
-------------------------------------------------------------------------------- |
| 1054 |
-- module definitions |
| 1055 |
-------------------------------------------------------------------------------- |
| 1056 |
|
| 1057 |
-- a special syntax sugar to export all functions to the global table |
| 1058 |
setmetatable(exports, { |
| 1059 |
__call = function(t, override) |
| 1060 |
for k, v in pairs(t) do |
| 1061 |
if _G[k] ~= nil then |
| 1062 |
local msg = 'function ' .. k .. ' already exists in global scope.' |
| 1063 |
if override then |
| 1064 |
_G[k] = v |
| 1065 |
print('WARNING: ' .. msg .. ' Overwritten.') |
| 1066 |
else |
| 1067 |
print('NOTICE: ' .. msg .. ' Skipped.') |
| 1068 |
end |
| 1069 |
else |
| 1070 |
_G[k] = v |
| 1071 |
end |
| 1072 |
end |
| 1073 |
end, |
| 1074 |
}) |
| 1075 |
|
| 1076 |
return exports |