Created
March 28, 2013 15:38
-
-
Save bmarcot/5264164 to your computer and use it in GitHub Desktop.
Scala By Example -- usage: compute((x, y) => x * y)(x => x)(1, 3)
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.annotation.tailrec | |
def compute(op: (Int, Int) => Int)(f: Int => Int)(a: Int, b: Int) : Int = { | |
@tailrec | |
def iter(a: Int, result: Int) : Int = { | |
if (a > b) result | |
else iter(a + 1, op(f(a), result)) | |
} | |
iter(a + 1, f(a)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment