Created
September 22, 2014 13:53
-
-
Save ramntry/cbda823fe7aa41c5a17a to your computer and use it in GitHub Desktop.
Type inference + Function overloading
This file contains hidden or 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
| 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