2023. 10. 18. 13:32ㆍ프로그래밍/공식 문서 번역 + 공부
Oracle에서 발췌한 JDK8에서 추가된 기능인 java의 람다 표현식에 대한 표현법이다.
consist : ~로 이루어져있다, ~로 되어있다.
following : 다음.
omit : 빠뜨리다, 누락시키다, 생략하다.
parentheses : 괄호
enclose : 둘러싸다, 에워싸다
treat : 대하다, 치부하다
concise : 간결한, 축약된
even : ~도
-람다식에 대한 설명
One issue with anonymous classes is that if the implementation of your anonymous class is very simple, such as an interface that contains only one method, then the syntax of anonymous classes may seem unwieldy and unclear. In these cases, you're usually trying to pass functionality as an argument to another method, such as what action should be taken when someone clicks a button. Lambda expressions enable you to do this, to treat functionality as method argument, or code as data.
The previous section, Anonymous Classes, shows you how to implement a base class without giving it a name. Although this is often more concise than a named class, for classes with only one method, even an anonymous class seems a bit excessive and cumbersome. Lambda expressions let you express instances of single-method classes more compactly.
Syntax of Lambda Expressions
A lambda expression consists of the following:
- 메서드 참고
printPersonsWithPredicate(
roster,
p -> p.getGender() == Person.Sex.MALE
&& p.getAge() >= 18
&& p.getAge() <= 25
);
- A comma-separated list of formal parameters enclosed in parentheses. The CheckPerson.test method contains one parameter, p, which represents an instance of the Person class.
- Note: You can omit the data type of the parameters in a lambda expression. In addition, you can omit the parentheses if there is only one parameter. For example, the following lambda expression is also valid:
p -> p.getGender() == Person.Sex.MALE
&& p.getAge() >= 18
&& p.getAge() <= 25
- The arrow token, ->
- A body, which consists of a single expression or a statement block. This example uses the following expression:If you specify a single expression, then the Java runtime evaluates the expression and then returns its value. Alternatively, you can use a return statement:A return statement is not an expression; in a lambda expression, you must enclose statements in braces ({}). However, you do not have to enclose a void method invocation in braces. For example, the following is a valid lambda expression:
- email -> System.out.println(email)
- p -> { return p.getGender() == Person.Sex.MALE && p.getAge() >= 18 && p.getAge() <= 25; }
- p.getGender() == Person.Sex.MALE && p.getAge() >= 18 && p.getAge() <= 25
Note that a lambda expression looks a lot like a method declaration; you can consider lambda expressions as anonymous methods—methods without a name.
The following example, Calculator, is an example of lambda expressions that take more than one formal parameter:
public class Calculator {
interface IntegerMath {
int operation(int a, int b);
}
public int operateBinary(int a, int b, IntegerMath op) {
return op.operation(a, b);
}
public static void main(String... args) {
Calculator myApp = new Calculator();
IntegerMath addition = (a, b) -> a + b;
IntegerMath subtraction = (a, b) -> a - b;
System.out.println("40 + 2 = " +
myApp.operateBinary(40, 2, addition));
System.out.println("20 - 10 = " +
myApp.operateBinary(20, 10, subtraction));
}
}
차근차근 뜯어서보자.
당신의 익명클래스가 굉장히 심플하게 구현돼있을 때 예를 들어 인터페이스가 오직 하나의 메서드만 포함하고 있을때 익명클래스의 문법은 확장성이 없고 간결하지 못한것처럼 보이는 문제가 있다.
이런 상황에서 당신은 누군가 버튼을 클릭했을때 액션을 취하도록 다른 버튼의 인수로서 pass하기 위해 시도했을 것이다.
람다 표현식은 함수를 메서드의 인수로 혹은 코드를 데이터로 다루기 위해 이런것을 가능하게 한다.
-번역
익명 클래스의 한 가지 문제는 익명 클래스의 구현이 매우 간단한 경우, 예를 들어 하나의 메서드만을 포함하는 인터페이스와 같은 경우, 익명 클래스의 구문이 다루기 불편하고 명확하지 않게 느껴질 수 있다는 것입니다. 이러한 경우에는 보통 다른 메서드에 기능을 인자로 전달하려고 하며, 예를 들어 누군가 버튼을 클릭할 때 어떤 작업을 수행해야 하는지와 같은 상황입니다. 람다 표현식을 사용하면 이를 가능하게 하며, 기능을 메서드 인자로 취급하거나 코드를 데이터로 취급할 수 있습니다.
이전 섹션인 익명클래스(링크)에서는 이름을 지정하지 않고 어떻게 주 클래스를 구현하는지를 보여줍니다.
익명 클래스는 일반적인 클래스의 이름을 짓는것보다는 더 간결할지도 모르나 클래스가 단 하나의 메서드만을 가지고 있다면
even an anonymous classes 익명클래스도(even : ~도) 지나치고 번거로워 보일 수 있다.
람다 표현식은 단일 메서드를 가진 클래스들의 인스턴스(객체)들을 compactly(사전적 : 빽빽하게 , 의역 : 간결하게)하게 표현할 수 있게 해준다.
-번역
이전 섹션인 익명 클래스는 이름을 지정하지 않고 기본 클래스를 구현하는 방법을 보여줍니다. 이는 종종 이름이 지정된 클래스보다 간결하지만, 하나의 메서드만을 가진 클래스의 경우에는 익명 클래스조차도 다소 과도하고 번거로워 보일 수 있습니다. 람다 표현식을 사용하면 단일 메서드 클래스의 인스턴스를 더 간결하게 표현할 수 있습니다.
이 공식문서의 내용을 요약하자면 다음과 같다. 단 한번만 사용하기 좋을 때 우리는 보통 익명클래스를 통해 객체를 생성했다. 클래스로 정의하기에는 메서드나 필드가 많이 필요없으나 한번씩은 사용할 것 같을 때 익명클래스를 사용한다. 그러나 이것 조차도 귀찮을때, 필드도 별로 없고 메서드도 1개 뿐인 간단한 익명클래스를 구현할 때는 이것조차 별로이니까 이럴 때는 lambda 표현식을 사용시에 더 편리하다라는 뜻인듯 하다.
번역하면서 느낀 점 : 컴마는 그냥 문장이 이어지듯이 자연스레 해석해도 될듯하다. that과 같이 하나의 문장이 어떤 문장의 형용사절로서 작용할까봐 그렇게 생각했는데 읽히는대로 읽는 것이 맞는듯하다.
.
이와 관련된 실습 내용은 따로 정리해야 할 듯 하다. 양이 너무 많다.
'프로그래밍 > 공식 문서 번역 + 공부' 카테고리의 다른 글
[JavaScript] 호이스팅이란? (0) | 2023.08.04 |
---|---|
jqueryForm plugin이란 무엇인가? (0) | 2023.08.04 |
[JavaScript] DOM이란? (0) | 2023.08.03 |
공식문서 독해를 위한 영어공부의 필요성 (0) | 2023.08.02 |