Recap: Dispatch Reports
Lesson 42 of 48 in Coddy's Lua for Python 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 Python Developers
1First Lua Programs
Run Lua ModulesPrint and CommentNames and Local ScopeNumbers and ConversionStrings and Byte IndexesRecap: Format a Receipt4Functions and Closures
Functions and ReturnsDefaults and Options TablesMultiple Results and VarargsCallbacks and Captured StateRecap: Pricing Functions2Values and Decisions
None and Missing FieldsTruth and Default ValuesBranches and BoundsEquality and IdentityRecap: Admission Rules5Tables as Collections
Lists as Dense TablesDictionaries and pairsSets and MembershipAliasing and Shallow CopiesRecap: Count Inventory8Composition and Object Lookup
Readable ObjectsComposition and DelegationPrototype Lookup ChainsPolymorphism by ProtocolValue Equality MetamethodsRecap: Dispatch ReportsPractice on your own: Online Lua compiler