-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiter_exerc71.lua
More file actions
53 lines (42 loc) · 782 Bytes
/
Copy pathiter_exerc71.lua
File metadata and controls
53 lines (42 loc) · 782 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
function iter(m, n)
if n >= m then return nil end
return n + 1
end
function fromto(n, m)
return iter, m, n - 1
end
function nextWord(st)
local s, e = string.find(st.buf, "%w+", st.pos)
if s then
st.pos = e + 1
return string.sub(st.buf, s, e)
else
return nil
end
end
function allWords(buf)
local st = { pos = 1, buf = buf}
return nextWord, st
end
function uniquewords(file)
local st = {}
local fp = io.open(file, "r")
local buf = fp:read("*a")
io.close(fp)
for word in allWords(buf) do
local counter = st[word]
if not counter then
counter = 1
else
counter = counter + 1
end
st[word] = counter
end
return next, st, nil
end
for i in fromto(1,10) do
print(i)
end
for k, v in uniquewords("iter_exerc71.lua") do
print(k, v)
end