Created
September 13, 2011 04:41
-
-
Save cgopalan/1213129 to your computer and use it in GitHub Desktop.
Scala generalized sum/product function
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
object OperatorOverRange { | |
def operateOverRange(f: Int => Int)(operate: (Int, Int) => Int)(base: Int)(a: Int, b:Int): Int = | |
if (a > b) base else operate(f(a), operateOverRange(f)(operate)(base)(a + 1, b)) | |
def main(args: Array[String]) { | |
println("Sum of range of numbers from 3 to 8 is: " + operateOverRange(x => x)((x,y) => x + y)(0)(3,8)) | |
println("Product of range of numbers from 3 to 8 is: " + operateOverRange(x => x)((x,y) => x * y)(1)(3,8)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment