Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- do -- Local scope for Iter.
- -- ReverseIpairs’s iterator; Arr is the “invariant state”,
- -- and I is the control variable’s previous value:
- local function Iter(I, Arr) --reversed Arguments
- if I > 1 then
- I = I - 1
- return I, Arr[I] -- Violates structured programming
- -- (not a severe misdeed in such a small function).
- end
- end
- -- An iterator factory -- like ipairs, but goes through
- -- the array in reverse order:
- function ReverseIpairs(Arr)
- return Iter, Arr, #Arr + 1
- end
- end
- for I, Str in ReverseIpairs({“one”, “two”, “three”}) do
- print(I, Str)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement