How to resolve the algorithm Define a primitive data type step by step in the Lua programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Define a primitive data type step by step in the Lua programming language
Table of Contents
Problem Statement
Demonstrate how to define a type that behaves like an integer but has a lowest valid value of 1 and a highest valid value of 10. Include all bounds checking you need to write, or explain how the compiler or interpreter creates those bounds checks for you.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Define a primitive data type step by step in the Lua programming language
Source code in the lua programming language
BI = { -- Bounded Integer
new = function(self, v) return setmetatable({v = self:_limit(v)}, BI_mt) end,
_limit = function(self,v) return math.max(1, math.min(math.floor(v), 10)) end,
}
BI_mt = {
__index = BI,
__call = function(self,v) return self:new(v) end,
__unm = function(self) return BI(-self.v) end,
__add = function(self,other) return BI(self.v+other.v) end,
__sub = function(self,other) return BI(self.v-other.v) end,
__mul = function(self,other) return BI(self.v*other.v) end,
__div = function(self,other) return BI(self.v/other.v) end,
__mod = function(self,other) return BI(self.v%other.v) end,
__pow = function(self,other) return BI(self.v^other.v) end,
__eq = function(self,other) return self.v==other.v end,
__lt = function(self,other) return self.v<other.v end,
__le = function(self,other) return self.v<=other.v end,
__tostring = function(self) return tostring(self.v) end
}
setmetatable(BI, BI_mt)
zero, one, two, three, four, five = BI(0), BI(1), BI(2), BI(3), BI(4), BI(5)
six, seven, eight, nine, ten, eleven = BI(6), BI(7), BI(8), BI(9), BI(10), BI(11)
print("zero through eleven:\t", zero, one, two, three, four, five, six, seven, eight, nine, ten, eleven)
print("zero less than one?\t", zero < one)
print("eleven greater than ten?", eleven > ten)
print("negative one equals one:", -one)
print("zero plus one equals two:", zero + one)
print("one minus one equals one:", one - one)
print("two minus three equals one:", two - three)
print("three minus two equals one:", three - two)
print("two times three equals six:", two * three)
print("two times three equals six?", two * three == six)
print("three times four equals ten?", three * four == ten)
print("one divided by two equals one:",one / two)
print("five divided by two equals two:", five / two)
print("order of operations (seven):", one + two * three)
print("order of operations (nine):", (one + two) * three)
print("eight mod three equals two:", eight % three)
print("eight mod two equals one:", eight % two)
print("two to the third equals eight:", two ^ three)
print("two to the fourth equals ten:", two ^ four)
You may also check:How to resolve the algorithm Terminal control/Inverse video step by step in the Z80 Assembly programming language
You may also check:How to resolve the algorithm Play recorded sounds step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Tokenize a string step by step in the XPath 2.0 programming language
You may also check:How to resolve the algorithm Sorting algorithms/Permutation sort step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Loops/Infinite step by step in the Java programming language