• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lunarmodules / Penlight / 16815657215

07 Aug 2025 08:56PM UTC coverage: 88.858% (-0.01%) from 88.871%
16815657215

push

github

Tieske
fix(*): some Lua 5.5 fixes for constant loop variables

see #497

12 of 13 new or added lines in 4 files covered. (92.31%)

9 existing lines in 3 files now uncovered.

5455 of 6139 relevant lines covered (88.86%)

256.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.65
/lua/pl/seq.lua
1
--- Manipulating iterators as sequences.
2
-- See @{07-functional.md.Sequences|The Guide}
3
--
4
-- Dependencies: `pl.utils`, `pl.types`, `debug`
5
-- @module pl.seq
6

7
local next,assert,pairs,tonumber,type,setmetatable = next,assert,pairs,tonumber,type,setmetatable
18✔
8
local strfind,format = string.find,string.format
18✔
9
local mrandom = math.random
18✔
10
local tsort,tappend = table.sort,table.insert
18✔
11
local io = io
18✔
12
local utils = require 'pl.utils'
18✔
13
local callable = require 'pl.types'.is_callable
18✔
14
local function_arg = utils.function_arg
18✔
15
local assert_arg = utils.assert_arg
18✔
16
local debug = require 'debug'
18✔
17

18
local seq = {}
18✔
19

20
-- given a number, return a function(y) which returns true if y > x
21
-- @param x a number
22
function seq.greater_than(x)
18✔
23
  return function(v)
24
    return tonumber(v) > x
150✔
25
  end
26
end
27

28
-- given a number, returns a function(y) which returns true if y < x
29
-- @param x a number
30
function seq.less_than(x)
18✔
31
  return function(v)
32
    return tonumber(v) < x
150✔
33
  end
34
end
35

36
-- given any value, return a function(y) which returns true if y == x
37
-- @param x a value
38
function seq.equal_to(x)
18✔
39
  if type(x) == "number" then
12✔
40
    return function(v)
41
      return tonumber(v) == x
30✔
42
    end
43
  else
44
    return function(v)
45
      return v == x
30✔
46
    end
47
  end
48
end
49

50
--- given a string, return a function(y) which matches y against the string.
51
-- @param s a string
52
function seq.matching(s)
18✔
53
  return function(v)
54
     return strfind(v,s)
18✔
55
  end
56
end
57

58
local nexti
59

60
--- sequence adaptor for a table.   Note that if any generic function is
61
-- passed a table, it will automatically use seq.list()
62
-- @param t a list-like table
63
-- @usage sum(list(t)) is the sum of all elements of t
64
-- @usage for x in list(t) do...end
65
function seq.list(t)
18✔
66
  assert_arg(1,t,'table')
234✔
67
  if not nexti then
234✔
68
    nexti = ipairs{}
6✔
69
  end
70
  local key,value = 0
234✔
71
  return function()
72
    key,value = nexti(t,key)
906✔
73
    return value
906✔
74
  end
75
end
76

77
--- return the keys of the table.
78
-- @param t an arbitrary table
79
-- @return iterator over keys
80
function seq.keys(t)
18✔
81
  assert_arg(1,t,'table')
6✔
82
  local key
83
  return function()
84
    key = next(t,key)
24✔
85
    return key
24✔
86
  end
87
end
88

89
local list = seq.list
18✔
90
local function default_iter(iter)
91
  if type(iter) == 'table' then return list(iter)
342✔
92
  else return iter end
282✔
93
end
94

95
seq.iter = default_iter
18✔
96

97
--- create an iterator over a numerical range. Like the standard Python function xrange.
98
-- @param start a number
99
-- @param finish a number greater than start
100
function seq.range(start,finish)
18✔
101
  local i = start - 1
6✔
102
  return function()
103
      i = i + 1
30✔
104
      if i > finish then return nil
30✔
105
      else return i end
24✔
106
  end
107
end
108

109
-- count the number of elements in the sequence which satisfy the predicate
110
-- @param iter a sequence
111
-- @param condn a predicate function (must return either true or false)
112
-- @param optional argument to be passed to predicate as second argument.
113
-- @return count
114
function seq.count(iter,condn,arg)
18✔
115
  local i = 0
6✔
116
  seq.foreach(iter,function(val)
12✔
117
        if condn(val,arg) then i = i + 1 end
32✔
118
  end)
119
  return i
6✔
120
end
121

122
--- return the minimum and the maximum value of the sequence.
123
-- @param iter a sequence
124
-- @return minimum value
125
-- @return maximum value
126
function seq.minmax(iter)
18✔
127
  local vmin,vmax = 1e70,-1e70
6✔
128
  for val in default_iter(iter) do
90✔
129
    local v = tonumber(val)
60✔
130
    if v < vmin then vmin = v end
60✔
131
    if v > vmax then vmax = v end
60✔
132
  end
133
  return vmin,vmax
6✔
134
end
135

136
--- return the sum and element count of the sequence.
137
-- @param iter a sequence
138
-- @param fn an optional function to apply to the values
139
function seq.sum(iter,fn)
18✔
140
  local s = 0
6✔
141
  local i = 0
6✔
142
  for val in default_iter(iter) do
50✔
143
    local v
144
    if fn then
30✔
NEW
145
      v = fn(val)
×
146
    else
147
      v = val
30✔
148
    end
149
    s = s + v
30✔
150
    i = i + 1
30✔
151
  end
152
  return s,i
6✔
153
end
154

155
--- create a table from the sequence. (This will make the result a List.)
156
-- @param iter a sequence
157
-- @return a List
158
-- @usage copy(list(ls)) is equal to ls
159
-- @usage copy(list {1,2,3}) == List{1,2,3}
160
function seq.copy(iter)
18✔
161
    local res,k = {},1
168✔
162
    for v in default_iter(iter) do
920✔
163
        res[k] = v
480✔
164
        k = k + 1
480✔
165
    end
166
    setmetatable(res, require('pl.List'))
168✔
167
    return res
168✔
168
end
169

170
--- create a table of pairs from the double-valued sequence.
171
-- @param iter a double-valued sequence
172
-- @param i1 used to capture extra iterator values
173
-- @param i2 as with pairs & ipairs
174
-- @usage copy2(ipairs{10,20,30}) == {{1,10},{2,20},{3,30}}
175
-- @return a list-like table
176
function seq.copy2 (iter,i1,i2)
18✔
177
    local res,k = {},1
240✔
178
    for v1,v2 in iter,i1,i2 do
1,104✔
179
        res[k] = {v1,v2}
594✔
180
        k = k + 1
594✔
181
    end
182
    return res
240✔
183
end
184

185
--- create a table of 'tuples' from a multi-valued sequence.
186
-- A generalization of copy2 above
187
-- @param iter a multiple-valued sequence
188
-- @return a list-like table
189
function seq.copy_tuples (iter)
18✔
190
    iter = default_iter(iter)
8✔
191
    local res = {}
6✔
192
    local row = {iter()}
8✔
193
    while #row > 0 do
36✔
194
        tappend(res,row)
30✔
195
        row = {iter()}
40✔
196
    end
197
    return res
6✔
198
end
199

200
--- return an iterator of random numbers.
201
-- @param n the length of the sequence
202
-- @param l same as the first optional argument to math.random
203
-- @param u same as the second optional argument to math.random
204
-- @return a sequence
205
function seq.random(n,l,u)
18✔
206
  local rand
207
  assert(type(n) == 'number')
18✔
208
  if u then
18✔
209
     rand = function() return mrandom(l,u) end
48✔
210
  elseif l then
12✔
211
     rand = function() return mrandom(l) end
48✔
212
  else
213
     rand = mrandom
6✔
214
  end
215

216
  return function()
217
     if n == 0 then return nil
144✔
218
     else
219
       n = n - 1
126✔
220
       return rand()
126✔
221
     end
222
  end
223
end
224

225
--- return an iterator to the sorted elements of a sequence.
226
-- @param iter a sequence
227
-- @param comp an optional comparison function (comp(x,y) is true if x < y)
228
function seq.sort(iter,comp)
18✔
229
    local t = seq.copy(iter)
12✔
230
    tsort(t,comp)
12✔
231
    return list(t)
12✔
232
end
233

234
--- return an iterator which returns elements of two sequences.
235
-- @param iter1 a sequence
236
-- @param iter2 a sequence
237
-- @usage for x,y in seq.zip(ls1,ls2) do....end
238
function seq.zip(iter1,iter2)
18✔
239
    iter1 = default_iter(iter1)
8✔
240
    iter2 = default_iter(iter2)
8✔
241
    return function()
242
        return iter1(),iter2()
40✔
243
    end
244
end
245

246
--- Makes a table where the key/values are the values and value counts of the sequence.
247
-- This version works with 'hashable' values like strings and numbers.
248
-- `pl.tablex.count_map` is more general.
249
-- @param iter a sequence
250
-- @return a map-like table
251
-- @return a table
252
-- @see pl.tablex.count_map
253
function seq.count_map(iter)
18✔
254
    local t = {}
6✔
255
    local v
256
    for s in default_iter(iter) do
50✔
257
        v = t[s]
30✔
258
        if v then t[s] = v + 1
30✔
259
        else t[s] = 1 end
18✔
260
    end
261
    return setmetatable(t, require('pl.Map'))
6✔
262
end
263

264
-- given a sequence, return all the unique values in that sequence.
265
-- @param iter a sequence
266
-- @param returns_table true if we return a table, not a sequence
267
-- @return a sequence or a table; defaults to a sequence.
268
function seq.unique(iter,returns_table)
18✔
269
    local t = seq.count_map(iter)
6✔
270
    local res,k = {},1
6✔
271
    for key in pairs(t) do res[k] = key; k = k + 1 end
24✔
272
    table.sort(res)
6✔
273
    if returns_table then
6✔
274
        return res
×
275
    else
276
        return list(res)
6✔
277
    end
278
end
279

280
--- print out a sequence iter with a separator.
281
-- @param iter a sequence
282
-- @param sep the separator (default space)
283
-- @param nfields maximum number of values per line (default 7)
284
-- @param fmt optional format function for each value
285
function seq.printall(iter,sep,nfields,fmt)
18✔
286
  local write = io.write
6✔
287
  if not sep then sep = ' ' end
6✔
288
  if not nfields then
6✔
289
      if sep == '\n' then nfields = 1e30
6✔
290
      else nfields = 7 end
6✔
291
  end
292
  if fmt then
6✔
293
    local fstr = fmt
6✔
294
    fmt = function(v) return format(fstr,v) end
66✔
295
  end
296
  local k = 1
6✔
297
  for v in default_iter(iter) do
90✔
298
     if fmt then v = fmt(v) end
80✔
299
     if k < nfields then
60✔
300
       write(v,sep)
54✔
301
       k = k + 1
54✔
302
    else
303
       write(v,'\n')
6✔
304
       k = 1
6✔
305
    end
306
  end
307
  write '\n'
6✔
308
end
309

310
-- return an iterator running over every element of two sequences (concatenation).
311
-- @param iter1 a sequence
312
-- @param iter2 a sequence
313
function seq.splice(iter1,iter2)
18✔
314
  iter1 = default_iter(iter1)
8✔
315
  iter2 = default_iter(iter2)
8✔
316
  local iter = iter1
6✔
317
  return function()
318
    local ret = iter()
30✔
319
    if ret == nil then
30✔
320
      if iter == iter1 then
12✔
321
        iter = iter2
6✔
322
        return iter()
6✔
323
      else return nil end
6✔
324
   else
325
       return  ret
18✔
326
   end
327
 end
328
end
329

330
--- return a sequence where every element of a sequence has been transformed
331
-- by a function. If you don't supply an argument, then the function will
332
-- receive both values of a double-valued sequence, otherwise behaves rather like
333
-- tablex.map.
334
-- @param fn a function to apply to elements; may take two arguments
335
-- @param iter a sequence of one or two values
336
-- @param arg optional argument to pass to function.
337
function seq.map(fn,iter,arg)
18✔
338
    fn = function_arg(1,fn)
16✔
339
    iter = default_iter(iter)
16✔
340
    return function()
341
        local v1,v2 = iter()
42✔
342
        if v1 == nil then return nil end
42✔
343
        return fn(v1,arg or v2) or false
40✔
344
    end
345
end
346

347
--- filter a sequence using a predicate function.
348
-- @param iter a sequence of one or two values
349
-- @param pred a boolean function; may take two arguments
350
-- @param arg optional argument to pass to function.
351
function seq.filter (iter,pred,arg)
18✔
352
    pred = function_arg(2,pred)
88✔
353
    return function ()
354
        local v1,v2
355
        while true do
356
            v1,v2 = iter()
592✔
357
            if v1 == nil then return nil end
444✔
358
            if pred(v1,arg or v2) then return v1,v2 end
504✔
359
        end
360
    end
361
end
362

363
--- 'reduce' a sequence using a binary function.
364
-- @func fn a function of two arguments
365
-- @param iter a sequence
366
-- @param initval optional initial value
367
-- @usage seq.reduce(operator.add,seq.list{1,2,3,4}) == 10
368
-- @usage seq.reduce('-',{1,2,3,4,5}) == -13
369
function seq.reduce (fn,iter,initval)
18✔
370
   fn = function_arg(1,fn)
56✔
371
   iter = default_iter(iter)
56✔
372
   local val = initval or iter()
42✔
373
   if val == nil then return nil end
42✔
374
   for v in iter do
160✔
375
       val = fn(val,v)
112✔
376
   end
377
   return val
36✔
378
end
379

380
--- take the first n values from the sequence.
381
-- @param iter a sequence of one or two values
382
-- @param n number of items to take
383
-- @return a sequence of at most n items
384
function seq.take (iter,n)
18✔
385
    iter = default_iter(iter)
40✔
386
    return function()
387
        if n < 1 then return end
102✔
388
        local val1,val2 = iter()
78✔
389
        if not val1 then return end
78✔
390
        n = n - 1
72✔
391
        return val1,val2
72✔
392
    end
393
end
394

395
--- skip the first n values of a sequence
396
-- @param iter a sequence of one or more values
397
-- @param n number of items to skip
398
function seq.skip (iter,n)
18✔
399
    n = n or 1
12✔
400
    for i = 1,n do
36✔
401
        if iter() == nil then return list{} end
40✔
402
    end
403
    return iter
6✔
404
end
405

406
--- a sequence with a sequence count and the original value.
407
-- enum(copy(ls)) is a roundabout way of saying ipairs(ls).
408
-- @param iter a single or double valued sequence
409
-- @return sequence of (i,v), i = 1..n and v is from iter.
410
function seq.enum (iter)
18✔
411
    local i = 0
6✔
412
    iter = default_iter(iter)
8✔
413
    return function  ()
414
        local val1,val2 = iter()
36✔
415
        if not val1 then return end
36✔
416
        i = i + 1
30✔
417
        return i,val1,val2
30✔
418
    end
419
end
420

421
--- map using a named method over a sequence.
422
-- @param iter a sequence
423
-- @param name the method name
424
-- @param arg1 optional first extra argument
425
-- @param arg2 optional second extra argument
426
function seq.mapmethod (iter,name,arg1,arg2)
18✔
427
    iter = default_iter(iter)
8✔
428
    return function()
429
        local val = iter()
18✔
430
        if not val then return end
18✔
431
        local fn = val[name]
12✔
432
        if not fn then error(type(val).." does not have method "..name) end
12✔
433
        return fn(val,arg1,arg2)
12✔
434
    end
435
end
436

437
--- a sequence of (last,current) values from another sequence.
438
--  This will return S(i-1),S(i) if given S(i)
439
-- @param iter a sequence
440
function seq.last (iter)
18✔
441
    iter = default_iter(iter)
24✔
442
    local val, l = iter(), nil
24✔
443
    if val == nil then return list{} end
18✔
444
    return function ()
445
        val,l = iter(),val
32✔
446
        if val == nil then return nil end
24✔
447
        return val,l
12✔
448
    end
449
end
450

451
--- call the function on each element of the sequence.
452
-- @param iter a sequence with up to 3 values
453
-- @param fn a function
454
function seq.foreach(iter,fn)
18✔
455
    fn = function_arg(2,fn)
8✔
456
    for i1,i2,i3 in default_iter(iter) do fn(i1,i2,i3) end
50✔
457
end
458

459
---------------------- Sequence Adapters ---------------------
460

461
local SMT
462

463
local function SW (iter,...)
464
    if callable(iter) then
352✔
465
        return setmetatable({iter=iter},SMT)
114✔
466
    else
467
        return iter,...
150✔
468
    end
469
end
470

471

472
-- can't directly look these up in seq because of the wrong argument order...
473
local map,reduce,mapmethod = seq.map, seq.reduce, seq.mapmethod
18✔
474
local overrides = {
18✔
475
    map = function(self,fun,arg)
476
        return map(fun,self,arg)
6✔
477
    end,
478
    reduce = function(self,fun,initval)
479
        return reduce(fun,self,initval)
36✔
480
    end
481
}
482

483
SMT = {
18✔
484
    __index = function (tbl,key)
485
        local fn = overrides[key] or seq[key]
258✔
486
        if fn then
258✔
487
            return function(sw,...) return SW(fn(sw.iter,...)) end
588✔
488
        else
489
            return function(sw,...) return SW(mapmethod(sw.iter,key,...)) end
14✔
490
        end
491
    end,
492
    __call = function (sw)
493
        return sw.iter()
×
494
    end,
495
}
18✔
496

497
setmetatable(seq,{
36✔
498
    __call = function(tbl,iter,extra)
499
        if not callable(iter) then
192✔
500
            if type(iter) == 'table' then iter = seq.list(iter)
152✔
501
            else return iter
×
502
            end
503
        end
504
        if extra then
144✔
505
            return setmetatable({iter=function()
10✔
506
                return iter(extra)
24✔
507
            end},SMT)
9✔
508
        else
509
            return setmetatable({iter=iter},SMT)
138✔
510
        end
511
    end
512
})
513

514
--- create a wrapped iterator over all lines in the file.
515
-- @param f either a filename, file-like object, or 'STDIN' (for standard input)
516
-- @param ... for Lua 5.2 only, optional format specifiers, as in `io.read`.
517
-- @return a sequence wrapper
518
function seq.lines (f,...)
18✔
519
    local iter,obj
520
    if f == 'STDIN' then
6✔
521
        f = io.stdin
×
522
    elseif type(f) == 'string' then
6✔
523
        iter,obj = io.lines(f,...)
×
524
    elseif not f.read then
6✔
525
        error("Pass either a string or a file-like object",2)
×
526
    end
527
    if not iter then
6✔
528
        iter,obj = f:lines(...)
8✔
529
    end
530
    if obj then -- LuaJIT version returns a function operating on a file
6✔
531
        local lines,file = iter,obj
×
532
        iter = function() return lines(file) end
×
533
    end
534
    return SW(iter)
6✔
535
end
536

537
function seq.import ()
18✔
538
    debug.setmetatable(function() end,{
×
539
        __index = function(tbl,key)
540
            local s = overrides[key] or seq[key]
×
541
            if s then return s
×
542
            else
543
                return function(s,...) return seq.mapmethod(s,key,...) end
×
544
            end
545
        end
546
    })
547
end
548

549
return seq
18✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc