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

lunarmodules / Penlight / 580

07 Aug 2025 08:56PM UTC coverage: 89.265% (+0.4%) from 88.871%
580

Pull #498

appveyor

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

see #497
Pull Request #498: fix(*): some Lua 5.5 fixes for constant loop variables

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

79 existing lines in 14 files now uncovered.

5480 of 6139 relevant lines covered (89.27%)

165.48 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
12✔
8
local strfind,format = string.find,string.format
12✔
9
local mrandom = math.random
12✔
10
local tsort,tappend = table.sort,table.insert
12✔
11
local io = io
12✔
12
local utils = require 'pl.utils'
12✔
13
local callable = require 'pl.types'.is_callable
12✔
14
local function_arg = utils.function_arg
12✔
15
local assert_arg = utils.assert_arg
12✔
16
local debug = require 'debug'
12✔
17

18
local seq = {}
12✔
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)
12✔
23
  return function(v)
24
    return tonumber(v) > x
100✔
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)
12✔
31
  return function(v)
32
    return tonumber(v) < x
100✔
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)
12✔
39
  if type(x) == "number" then
8✔
40
    return function(v)
41
      return tonumber(v) == x
20✔
42
    end
43
  else
44
    return function(v)
45
      return v == x
20✔
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)
12✔
53
  return function(v)
54
     return strfind(v,s)
12✔
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)
12✔
66
  assert_arg(1,t,'table')
156✔
67
  if not nexti then
156✔
68
    nexti = ipairs{}
4✔
69
  end
70
  local key,value = 0
156✔
71
  return function()
72
    key,value = nexti(t,key)
604✔
73
    return value
604✔
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)
12✔
81
  assert_arg(1,t,'table')
4✔
82
  local key
83
  return function()
84
    key = next(t,key)
16✔
85
    return key
16✔
86
  end
87
end
88

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

95
seq.iter = default_iter
12✔
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)
12✔
101
  local i = start - 1
4✔
102
  return function()
103
      i = i + 1
20✔
104
      if i > finish then return nil
20✔
105
      else return i end
16✔
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)
12✔
115
  local i = 0
4✔
116
  seq.foreach(iter,function(val)
8✔
117
        if condn(val,arg) then i = i + 1 end
16✔
118
  end)
119
  return i
4✔
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)
12✔
127
  local vmin,vmax = 1e70,-1e70
4✔
128
  for val in default_iter(iter) do
44✔
129
    local v = tonumber(val)
40✔
130
    if v < vmin then vmin = v end
40✔
131
    if v > vmax then vmax = v end
40✔
132
  end
133
  return vmin,vmax
4✔
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)
12✔
140
  local s = 0
4✔
141
  local i = 0
4✔
142
  for val in default_iter(iter) do
24✔
143
    local v
144
    if fn then
20✔
NEW
145
      v = fn(val)
×
146
    else
147
      v = val
20✔
148
    end
149
    s = s + v
20✔
150
    i = i + 1
20✔
151
  end
152
  return s,i
4✔
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)
12✔
161
    local res,k = {},1
112✔
162
    for v in default_iter(iter) do
432✔
163
        res[k] = v
320✔
164
        k = k + 1
320✔
165
    end
166
    setmetatable(res, require('pl.List'))
112✔
167
    return res
112✔
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)
12✔
177
    local res,k = {},1
160✔
178
    for v1,v2 in iter,i1,i2 do
556✔
179
        res[k] = {v1,v2}
396✔
180
        k = k + 1
396✔
181
    end
182
    return res
160✔
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)
12✔
190
    iter = default_iter(iter)
4✔
191
    local res = {}
4✔
192
    local row = {iter()}
4✔
193
    while #row > 0 do
24✔
194
        tappend(res,row)
20✔
195
        row = {iter()}
20✔
196
    end
197
    return res
4✔
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)
12✔
206
  local rand
207
  assert(type(n) == 'number')
12✔
208
  if u then
12✔
209
     rand = function() return mrandom(l,u) end
32✔
210
  elseif l then
8✔
211
     rand = function() return mrandom(l) end
32✔
212
  else
213
     rand = mrandom
4✔
214
  end
215

216
  return function()
217
     if n == 0 then return nil
96✔
218
     else
219
       n = n - 1
84✔
220
       return rand()
84✔
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)
12✔
229
    local t = seq.copy(iter)
8✔
230
    tsort(t,comp)
8✔
231
    return list(t)
8✔
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)
12✔
239
    iter1 = default_iter(iter1)
4✔
240
    iter2 = default_iter(iter2)
4✔
241
    return function()
242
        return iter1(),iter2()
16✔
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)
12✔
254
    local t = {}
4✔
255
    local v
256
    for s in default_iter(iter) do
24✔
257
        v = t[s]
20✔
258
        if v then t[s] = v + 1
20✔
259
        else t[s] = 1 end
12✔
260
    end
261
    return setmetatable(t, require('pl.Map'))
4✔
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)
12✔
269
    local t = seq.count_map(iter)
4✔
270
    local res,k = {},1
4✔
271
    for key in pairs(t) do res[k] = key; k = k + 1 end
16✔
272
    table.sort(res)
4✔
273
    if returns_table then
4✔
274
        return res
×
275
    else
276
        return list(res)
4✔
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)
12✔
286
  local write = io.write
4✔
287
  if not sep then sep = ' ' end
4✔
288
  if not nfields then
4✔
289
      if sep == '\n' then nfields = 1e30
4✔
290
      else nfields = 7 end
4✔
291
  end
292
  if fmt then
4✔
293
    local fstr = fmt
4✔
294
    fmt = function(v) return format(fstr,v) end
44✔
295
  end
296
  local k = 1
4✔
297
  for v in default_iter(iter) do
44✔
298
     if fmt then v = fmt(v) end
40✔
299
     if k < nfields then
40✔
300
       write(v,sep)
36✔
301
       k = k + 1
36✔
302
    else
303
       write(v,'\n')
4✔
304
       k = 1
4✔
305
    end
306
  end
307
  write '\n'
4✔
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)
12✔
314
  iter1 = default_iter(iter1)
4✔
315
  iter2 = default_iter(iter2)
4✔
316
  local iter = iter1
4✔
317
  return function()
318
    local ret = iter()
20✔
319
    if ret == nil then
20✔
320
      if iter == iter1 then
8✔
321
        iter = iter2
4✔
322
        return iter()
4✔
323
      else return nil end
4✔
324
   else
325
       return  ret
12✔
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)
12✔
338
    fn = function_arg(1,fn)
8✔
339
    iter = default_iter(iter)
8✔
340
    return function()
341
        local v1,v2 = iter()
28✔
342
        if v1 == nil then return nil end
28✔
343
        return fn(v1,arg or v2) or false
20✔
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)
12✔
352
    pred = function_arg(2,pred)
44✔
353
    return function ()
354
        local v1,v2
355
        while true do
356
            v1,v2 = iter()
296✔
357
            if v1 == nil then return nil end
296✔
358
            if pred(v1,arg or v2) then return v1,v2 end
252✔
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)
12✔
370
   fn = function_arg(1,fn)
28✔
371
   iter = default_iter(iter)
28✔
372
   local val = initval or iter()
28✔
373
   if val == nil then return nil end
28✔
374
   for v in iter do
80✔
375
       val = fn(val,v)
56✔
376
   end
377
   return val
24✔
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)
12✔
385
    iter = default_iter(iter)
20✔
386
    return function()
387
        if n < 1 then return end
68✔
388
        local val1,val2 = iter()
52✔
389
        if not val1 then return end
52✔
390
        n = n - 1
48✔
391
        return val1,val2
48✔
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)
12✔
399
    n = n or 1
8✔
400
    for i = 1,n do
24✔
401
        if iter() == nil then return list{} end
20✔
402
    end
403
    return iter
4✔
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)
12✔
411
    local i = 0
4✔
412
    iter = default_iter(iter)
4✔
413
    return function  ()
414
        local val1,val2 = iter()
24✔
415
        if not val1 then return end
24✔
416
        i = i + 1
20✔
417
        return i,val1,val2
20✔
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)
12✔
427
    iter = default_iter(iter)
4✔
428
    return function()
429
        local val = iter()
12✔
430
        if not val then return end
12✔
431
        local fn = val[name]
8✔
432
        if not fn then error(type(val).." does not have method "..name) end
8✔
433
        return fn(val,arg1,arg2)
8✔
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)
12✔
441
    iter = default_iter(iter)
12✔
442
    local val, l = iter(), nil
12✔
443
    if val == nil then return list{} end
12✔
444
    return function ()
445
        val,l = iter(),val
16✔
446
        if val == nil then return nil end
16✔
447
        return val,l
8✔
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)
12✔
455
    fn = function_arg(2,fn)
4✔
456
    for i1,i2,i3 in default_iter(iter) do fn(i1,i2,i3) end
20✔
457
end
458

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

461
local SMT
462

463
local function SW (iter,...)
464
    if callable(iter) then
176✔
465
        return setmetatable({iter=iter},SMT)
76✔
466
    else
467
        return iter,...
100✔
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
12✔
474
local overrides = {
12✔
475
    map = function(self,fun,arg)
476
        return map(fun,self,arg)
4✔
477
    end,
478
    reduce = function(self,fun,initval)
479
        return reduce(fun,self,initval)
24✔
480
    end
481
}
482

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

497
setmetatable(seq,{
24✔
498
    __call = function(tbl,iter,extra)
499
        if not callable(iter) then
96✔
500
            if type(iter) == 'table' then iter = seq.list(iter)
76✔
501
            else return iter
×
502
            end
503
        end
504
        if extra then
96✔
505
            return setmetatable({iter=function()
8✔
506
                return iter(extra)
16✔
507
            end},SMT)
7✔
508
        else
509
            return setmetatable({iter=iter},SMT)
92✔
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,...)
12✔
519
    local iter,obj
520
    if f == 'STDIN' then
4✔
521
        f = io.stdin
×
522
    elseif type(f) == 'string' then
4✔
523
        iter,obj = io.lines(f,...)
×
524
    elseif not f.read then
4✔
525
        error("Pass either a string or a file-like object",2)
×
526
    end
527
    if not iter then
4✔
528
        iter,obj = f:lines(...)
4✔
529
    end
530
    if obj then -- LuaJIT version returns a function operating on a file
4✔
531
        local lines,file = iter,obj
×
532
        iter = function() return lines(file) end
×
533
    end
534
    return SW(iter)
4✔
535
end
536

537
function seq.import ()
12✔
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
12✔
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