Advertisement
Guest User

Untitled

a guest
May 30th, 2016
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.31 KB | None | 0 0
  1. def product(f: Int => Int)(a: Int, b: Int): Int = mapReduce(f, (x, y) => x * y, 1)(a, b)
  2.  
  3.   def factorial(x: Int) = product(x => x)(1, x)
  4.  
  5.   def mapReduce(f: Int => Int, combine: (Int, Int) => Int, zero: Int)(a: Int, b: Int): Int =
  6.     if (a > b) zero
  7.     else combine(f(a), mapReduce(f, combine, zero)(a + 1, b))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement