Skip to content

Instantly share code, notes, and snippets.

@ramntry
Created September 22, 2014 13:53
Show Gist options
  • Select an option

  • Save ramntry/cbda823fe7aa41c5a17a to your computer and use it in GitHub Desktop.

Select an option

Save ramntry/cbda823fe7aa41c5a17a to your computer and use it in GitHub Desktop.
Type inference + Function overloading
object QuadraticEquationSolver {
def solve(a: Double, b: Double, c: Double) = {
val discriminant = b*b - 4*a*c
if (discriminant < 0)
List()
else {
val sqrtOfDisr = math.sqrt(discriminant)
List((-b - sqrtOfDisr)/2, (-b + sqrtOfDisr)/2)
}
}
def main(args: Array[String]) {
val x1 = -2
val x2 = 3
val a = 1
val b = -(x1 + x2)
val c = x1*x2
println("roots: " + solve(a, b, c) + ", expected: (" + x1 + ", " + x2 + ")")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment