Recap: Dispatch Reports
Lesson 42 of 48 in Coddy's Lua for C Developers course.
Challenge
MediumImplement two modules, short.lua and full.lua, each providing format(self,name,n). Short returns name:n; full returns name has n. Input has name, integer n, and style short or full. App selects the module and returns its format result.
Edit app.lua, short.lua, full.lua. The visible locked main.lua reads stdin into a sequence of lines, calls app.run(lines), and prints its returned value with one final newline. Do not print diagnostics or change the module names. Empty input gives an empty sequence. All numeric inputs are small enough to avoid overflow.
Public API checks: Both formatter modules must implement their public format(self, name, n) method independently. The locked driver checks this behavior before calling app.run. These checks print nothing when successful.
Try it yourself
-- Public API checks
do
local short,full=require("short"),require("full")
assert(short:format("pen",3)=="pen:3","short module format")
assert(full:format("book",0)=="book has 0","full module format")
assert(short:format("x",-1)=="x:-1","signed values are formatted unchanged")
end
local app = require("app")
local lines = {}
for line in io.lines() do lines[#lines + 1] = line end
print(app.run(lines))
All lessons in Lua for C Developers
1First Lua Programs
Run Lua ModulesPrint and CommentNames and Local ScopeNumbers and ConversionStrings and Byte PositionsRecap: Format a Receipt4Functions and Closures
Functions and ArgumentsDefaults and Options TablesMultiple Results and VarargsCallbacks and Captured StateRecap: Pricing Functions2Values and Decisions
Absence and Table FieldsTruth and Default ValuesBranches and BoundsValues and Table IdentityRecap: Admission Rules5Tables as Collections
Arrays to Dense TablesRecords and Dynamic KeysSets and MembershipAliasing and Shallow CopiesRecap: Count Inventory8Composition and Object Lookup
Customize String ConversionDelegate to a CollaboratorBuild Prototype Lookup ChainsUse a Shared Method ProtocolCompare Values with __eqRecap: Dispatch ReportsPractice on your own: Online Lua compiler