Commits

Chris Klimas committed 917fe6c

Add bind() function

Bind a function invocation to an owner object and argument set so it can
be called later.

Comments (0)

Files changed (1)

zoetrope/core/globals.lua

 		return ''
 	end
 end
+
+-- Function: bind
+-- Binds a function call to an object so it can be later called with
+-- the correct context.
+--
+-- Arguments:
+--		obj - object to use as function owner
+--		func - either a string name of a property of obj, or a free-standing
+--			   function.
+--		... - any number of extra arguments 
+
+function bind (obj, func, ...)
+	local arg = {...}
+
+	return function()
+		if type(func) == 'string' then
+			return obj[func](obj, unpack(arg))
+		else
+			return func(obj, unpack(arg))
+		end
+	end
+end