Created
July 25, 2019 09:30
-
-
Save ayago/350b4cbd64b89a93b172f76d9bd389c5 to your computer and use it in GitHub Desktop.
Higher order Functions in Java
This file contains hidden or 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
import java.util.function.Function; | |
import static java.lang.String.format; | |
class HigherOrderFunctions { | |
public static void main(String[] args) { | |
Function<Integer, Integer> addOne = describeOperationFor(1); | |
Function<Integer, Integer> multiplyOne = a -> a * 1; | |
Function<Integer, Integer> subtractOne = a -> a - 1; | |
Function<Integer, Integer> divideByOne = a -> a / 1; | |
System.out.println(describeOperationFor3(addOne)); | |
System.out.println(describeOperationFor3(multiplyOne)); | |
System.out.println(describeOperationFor3(subtractOne)); | |
System.out.println(describeOperationFor3(divideByOne)); | |
} | |
//a higher order function that takes a function | |
private static String describeOperationFor3(Function<Integer, Integer> operation){ | |
final Integer value = 3; | |
return format("Operation on 3 resulted to %s", operation.apply(3)); | |
} | |
//a higher order function that returns a function | |
private static Function<Integer, Integer> describeOperationFor(Integer addend){ | |
return a -> a + addend; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment