본문 바로가기

NHN 아카데미

Lambda Expression

24 . 02 .29

with Python.

def calc(a,b,c):
    return a(b,c)

def add(b,c):
    return b+c

calc(lambda a,b: a * b , 1, 2)

이름도 없고 파라미터도 없는 ?

Lambda Expression → 익명 메서드

간단한 연산에 쓰일 클래스를 만드는 과정을 없애준다.

with Java → 객체를 파라미터로 넘긴다.

함수를 객체화 → 함수 (파라미터 개수 , 리턴타입, …)

인터페이스 함수 1개여야함. → 함수를 객체화 한 것이라고 함 (자바에서)

Funtional interface //함수를 객체화하기 위한 인터페이스다

@FunctionalInterface
interface BinaryOp{
    int apply(int x, int y);

}
class Algo {
    public static int calc(BinaryOp binder , int i,int j) {
        return binder.apply(i,j);
    }
}

class Test {
    public static void main(String[] args) {
        Algo.calc((x,y) -> x + y,1,2); // 람다 식
    }
}

@FunctionalInterface - 어노테이션 붙혀

함수를 객체화 , 타입화 / class Or Interface

'NHN 아카데미' 카테고리의 다른 글

NHN 아카데미 6기 후기  (3) 2024.07.25
응답코드 ( HTTP response status code )  (0) 2024.04.16
Process & Thread  (0) 2024.03.12