0 Replies - 338 Views - Last Post: 25 August 2009 - 05:29 PM

#1 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,235
  • Joined: 14-September 07

[LUA]Wrapping a Function in an Iterator

Posted 25 August 2009 - 05:29 PM

Description: Implemented when iterators need their own for loop rather then being reference in one. Iterator takes a function name parameter (print for example) and calls it upon each iteration of the inner for loop. Example usage is supplied.
function values(t)
	local i = 0
	return function () --anon function, creates a closure
		i = i + 1
		return t[i]

--this function acts as an iterator using the above function
--eliminates the need of an outer for loop in the program
function iter(func)
	for item in values(myArray) do
		func(item) --print, could be another function, up to you


Is This A Good Question/Topic? 0
  • +

Page 1 of 1