Split prompts-for-review enumeration out from `main.lua`
John Smith

John Smith commited on 2022-0 21:26:59
Showing 2 changed files, with 38 additions and 28 deletions.


`main.lua`'s algorithm to construct a shuffled list of prompts to review
has been moved into `review.lua`.
... ...
@@ -23,24 +23,6 @@ local function clear()
23 23
     io.write("\x1bc")
24 24
 end
25 25
 
26
---[[
27
-    returns a shuffled version of the table,
28
-    destroying the table in-place
29
-    adapted from
30
-    https://stackoverflow.com/q/55192727
31
-]]
32
-local function shuffle(t)
33
-    local ret = {}
34
-    local ci = 1
35
-    while #t > 0 do
36
-        local ri = math.random(1, #t)
37
-        ret[ci] = t[ri]
38
-        table.remove(t, ri)
39
-        ci = ci + 1
40
-    end
41
-    return ret
42
-end
43
-
44 26
 --[[
45 27
     check action based on first command-line argument;
46 28
     if nothing valid given, default to help
... ...
@@ -105,15 +87,7 @@ elseif action == "r" then
105 87
     local deck = loader.load(deckfname)
106 88
     if deck then
107 89
         -- collect list of index-pairs for prompts to be reviewed this session
108
-        local ctrt = {}
109
-        for i, cg in ipairs(deck) do
110
-            for j, pr in ipairs(cg) do
111
-                if review.should(pr) then
112
-                    table.insert(ctrt, { i, j })
113
-                end
114
-            end
115
-        end
116
-        ctrt = shuffle(ctrt)
90
+        local ctrt = review.list(deck)
117 91
         io.write(string.format(
118 92
             "%d prompts are ready for review.\nHow many to review in this session? ", #ctrt
119 93
         ))
... ...
@@ -25,4 +25,40 @@ local function should(prompt)
25 25
     return os.time() - prompt.time >= prompt.delay
26 26
 end
27 27
 
28
-return { update = update, should = should }
28
+--[[
29
+    returns a shuffled version of the table,
30
+    destroying the table in-place
31
+    adapted from
32
+    https://stackoverflow.com/q/55192727
33
+]]
34
+local function shuffle(t)
35
+    local ret = {}
36
+    local ci = 1
37
+    while #t > 0 do
38
+        local ri = math.random(1, #t)
39
+        ret[ci] = t[ri]
40
+        table.remove(t, ri)
41
+        ci = ci + 1
42
+    end
43
+    return ret
44
+end
45
+
46
+--[[
47
+    returns a table of pairs of integers to index the deck
48
+    to get prompts to be reviewed now;
49
+    prompts are shuffled in order
50
+]]
51
+local function toreviewlist(deck)
52
+    local ctrt = {}
53
+    for i, cg in ipairs(deck) do
54
+        for j, pr in ipairs(cg) do
55
+            if should(pr) then
56
+                table.insert(ctrt, { i, j })
57
+            end
58
+        end
59
+    end
60
+    ctrt = shuffle(ctrt)
61
+    return ctrt
62
+end
63
+
64
+return { update = update, should = should, list = toreviewlist }
29 65