--[[ Exploring Languages with Interpreters and Functional Programming Copyright (C) 2018, H. Conrad Cunningham Lua prototype-based programming example from Ch. 3, Test Script 1234567890123456789012345678901234567890123456789012345678901234567890 2018-08-23: Original version --]] local CP = require "CountingPB" local function test() print("Create and execute x delegating to CountingPB (0,10)") x = CP:new({count = 0, maxc = 10}) print("Output from x:counter()") x:counter() print("Create and execute y delegating to x (10,15)") y = x:new({count = 10, maxc = 15}) print("Output from y:counter()") y:counter() print("Create and execute z delegating to y (_,400), " .. "new doubling adv, new method bye") z = y:new( { maxc = 400, has_more = function (self,c,m) return c ~= 0 and math.abs(c) <= math.abs(m) end, adv = function(self) self.count = self.count * 2 end, bye = function(self) print(self.msg) end, msg = "Good-Bye!" } ) print("Output from z:counter()") z:counter() z:bye() end test()