Skip to content

Instantly share code, notes, and snippets.

@RyanSusana
Created May 29, 2020 12:24
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/a111845b18195f7d366afa661467468c to your computer and use it in GitHub Desktop.
Save RyanSusana/a111845b18195f7d366afa661467468c to your computer and use it in GitHub Desktop.
//Wraps all operands with a high-order function (the f parameter)
def sum(f: Int => Int, a: Int, b: Int): Int =
if (a > b) 0 else f(a) + sum(f, a + 1, b)
// Create a cubical sum of all ints from a to b
def sumCube(a: Int, b: Int) = sum(cube, a, b)
// Same for factorial
def sumFactorial(a: Int, b: Int) = sum(factorial, a, b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment