-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const originalLoxFileString = ` | ||
// Base class for a Calculator | ||
class Calculator { | ||
compute(x) { | ||
return x; // Default implementation | ||
} | ||
} | ||
// Fibonacci calculator extends the base class | ||
class Fibonacci < Calculator { | ||
compute(n) { | ||
if (n <= 1) return n; | ||
return this.compute(n - 1) + this.compute(n - 2); | ||
} | ||
} | ||
// Factorial calculator extends the base class | ||
class Factorial < Calculator { | ||
compute(n) { | ||
if (n <= 1) return 1; | ||
return n * this.compute(n - 1); | ||
} | ||
} | ||
// Dynamic calculator handler | ||
class DynamicCalculator { | ||
init(calculator) { | ||
this.calculator = calculator; | ||
} | ||
perform(input) { | ||
return this.calculator.compute(input); | ||
} | ||
} | ||
// Main execution | ||
var input = 10; | ||
fun showRes(name, func){ | ||
print name + " of"; | ||
print input; | ||
print "results in:"; | ||
print func(input); | ||
print ""; | ||
} | ||
// Using a Fibonacci calculator | ||
var fibCalculator = DynamicCalculator(Fibonacci()); | ||
showRes("Fibonacci", fibCalculator.perform); | ||
// Using a Factorial calculator | ||
var factCalculator = DynamicCalculator(Factorial()); | ||
showRes("Factorial", factCalculator.perform); | ||
`; | ||
|
||
export { originalLoxFileString }; |