Skip to content

Instantly share code, notes, and snippets.

@bmarcot
Created March 28, 2013 15:38
Show Gist options
  • Save bmarcot/5264164 to your computer and use it in GitHub Desktop.
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)
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