Advertisement
haythammed

lua tables custom made loops

Feb 10th, 2023
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. do -- Local scope for Iter.
  2. -- ReverseIpairs’s iterator; Arr is the “invariant state”,
  3. -- and I is the control variable’s previous value:
  4. local function Iter(I, Arr) --reversed Arguments
  5. if I > 1 then
  6. I = I - 1
  7. return I, Arr[I] -- Violates structured programming
  8. -- (not a severe misdeed in such a small function).
  9. end
  10. end
  11. -- An iterator factory -- like ipairs, but goes through
  12. -- the array in reverse order:
  13. function ReverseIpairs(Arr)
  14. return Iter, Arr, #Arr + 1
  15. end
  16. end
  17. for I, Str in ReverseIpairs({“one”, “two”, “three”}) do
  18. print(I, Str)
  19. end
  20.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement