Skip to content

Instantly share code, notes, and snippets.

@RyanSusana
Created May 29, 2020 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RyanSusana/552fd95fc8073a7da0defa1877ff0c81 to your computer and use it in GitHub Desktop.
Save RyanSusana/552fd95fc8073a7da0defa1877ff0c81 to your computer and use it in GitHub Desktop.
// Creates a curried version of sum, where the
// first function is that to be applied to the input and the
// second function is the input
// (Int => Int) => (Int, Int) => Int
def sumCurry(f: Int => Int): (Int, Int) => Int = {
def sumF(a: Int, b: Int): Int =
if (a > b) 0 else f(a) + sumF(a + 1, b)
sumF
}
// Or with syntax sugar.....
def sumCurry(f: Int => Int)(a: Int, b: Int): Int =
if (a > b) 0 else f(a) + sumCurry(f)(a + 1, b)
// Sum can now be called like this. Pretty clean right?!
sumCurry(cube)(1, 6)
sumCurry(factorial)(1, 6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment