Java 8 FunctionalInterface 1/4 Function
Function 예제
/**
* Function, The Transformer
* function consumer predicate supplier
*/
/*
//abstract class instance
Function<String, Integer> toInt = new Function<String, Integer>(){
@Override
public Integer apply(String value){
return Integer.parseInt(value);
}
};
int result = toInt.apply("100");
System.out.println(result);
*/
//mapper, String -> Integer
Function<String, Integer> toInt = value->Integer.parseInt(value); //Lambda Expression
int result = toInt.apply("123");
System.out.println(result);//output : 123
//같은 Type을 return 하면 Identity라고 한다.
/**
* 함수형프로그래밍에서는 함수라는것 자체가 First Class Citizen이기 때문에
* parameter로만 받는게 아니라 return 값으로도 함수를 쓸 수 있다.
*/
//Function<Integer, Integer> identity = Function.identity(); //java.util.function에 있는 기본함수
Function<Integer, Integer> identity = t -> t; //Lambda를 이용해 구현한 identity function
System.out.println(identity.apply(200));//output:200
Function.java 내용
@FunctionalInterface
public interface Function<T, R> {
/**
* Applies this function to the given argument.
*
* @param t the function argument
* @return the function result
*/
R apply(T t);
/**
* Returns a composed function that first applies the {@code before}
* function to its input, and then applies this function to the result.
* If evaluation of either function throws an exception, it is relayed to
* the caller of the composed function.
*
* @param <V> the type of input to the {@code before} function, and to the
* composed function
* @param before the function to apply before this function is applied
* @return a composed function that first applies the {@code before}
* function and then applies this function
* @throws NullPointerException if before is null
*
* @see #andThen(Function)
*/
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
}
/**
* Returns a composed function that first applies this function to
* its input, and then applies the {@code after} function to the result.
* If evaluation of either function throws an exception, it is relayed to
* the caller of the composed function.
*
* @param <V> the type of output of the {@code after} function, and of the
* composed function
* @param after the function to apply after this function is applied
* @return a composed function that first applies this function and then
* applies the {@code after} function
* @throws NullPointerException if after is null
*
* @see #compose(Function)
*/
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
}
/**
* Returns a function that always returns its input argument.
*
* @param <T> the type of the input and output objects to the function
* @return a function that always returns its input argument
*/
static <T> Function<T, T> identity() {
return t -> t;
}
'Java' 카테고리의 다른 글
JPA - 소개 (0) | 2017.11.01 |
---|---|
JPA - ORM 튜토리얼 (0) | 2017.11.01 |
Lambda Expression, 람다 예제 (0) | 2017.01.05 |
String StringBuffer StringBuilder 차이 (2) | 2016.12.27 |
Spring DI와 IoC [ Dependency Injection + Inversion of Control ] 요약 (0) | 2016.12.23 |