Created
March 6, 2024 14:58
-
-
Save adamgordonbell/18a0dbdf560ef39a78bd9f6632aaa49e to your computer and use it in GitHub Desktop.
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
import scala.math.abs | |
object HybridStyleCalculator { | |
class Calculator { | |
private var result: Double = 0; | |
// Start Java Style | |
// Returns, braces and semi-colons | |
def getResult(): Double = { | |
return result; | |
} | |
def multiply(number: Double): Calculator = { | |
if (number == 0) { | |
println("Multiplication skipped: number is 0"); | |
} else { | |
result = result * abs(number); | |
} | |
return this; | |
} | |
// Switch to Python with implicit returns | |
// significant whitespace, no returns, no semi colons | |
def add(number: Double): Calculator = | |
result += abs(number) | |
this | |
def subtract(number: Double): Calculator = | |
result -= abs(number) | |
this | |
} | |
def main(args: Array[String]): Unit = { | |
// Call Ruby Style | |
// no braces, no dots | |
val calc = new Calculator add -5 subtract -3 multiply -2 | |
println(s"Result: ${calc.getResult()}") | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment