Created
May 29, 2020 12:34
-
-
Save RyanSusana/552fd95fc8073a7da0defa1877ff0c81 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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