Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def product(f: Int => Int)(a: Int, b: Int): Int = mapReduce(f, (x, y) => x * y, 1)(a, b)
- def factorial(x: Int) = product(x => x)(1, x)
- def mapReduce(f: Int => Int, combine: (Int, Int) => Int, zero: Int)(a: Int, b: Int): Int =
- if (a > b) zero
- else combine(f(a), mapReduce(f, combine, zero)(a + 1, b))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement