Skip to content

Instantly share code, notes, and snippets.

@SungjinYoo
Last active November 16, 2018 12:16
Show Gist options
  • Save SungjinYoo/cfae9c062ca9fc73f0c1c9b425030c56 to your computer and use it in GitHub Desktop.
Save SungjinYoo/cfae9c062ca9fc73f0c1c9b425030c56 to your computer and use it in GitHub Desktop.
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