Last active
November 16, 2018 12:16
-
-
Save SungjinYoo/cfae9c062ca9fc73f0c1c9b425030c56 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
interface Function<T, R> { | |
R apply(T value); | |
} | |
// a function that returns the same type with the input type | |
Function<Integer, Integer> square = (num) -> { | |
return num * num; | |
} | |
System.out.println(square.apply(3)); // prints out 9 | |
// a function that converts into another type | |
Function<Integer, Boolean> mapper = (num) -> { | |
return num > 0; | |
} | |
// a function that returns nothing but uses the given input | |
Function<String, Void> printer = (value) -> { | |
System.out.println(value); | |
return null; | |
} | |
// a function that has no input but returns something | |
Function<Void, Integer> randomInt = (aVoid) -> { | |
return new Random().nextInt(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment