일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- Spring XD
- 엘라스틱서치
- scala
- design pattern
- intellij
- DDD
- 제주
- hadoop
- hdfs
- nginx
- Spring Boot
- Hbase
- Java
- docker
- 인텔리J
- Linux
- elasticsearch
- elastic search
- SBT
- Clean Code
- apache storm
- Storm
- Angular2
- 스프링 배치
- 도메인주도설계
- hibernate
- Spring
- Spring Batch
- Gradle
- spark
Archives
- Today
- Total
욱'S 노트
Scala - 익명 함수 본문
반응형
스칼라에서 익명함수를 정의하는 방법은 매우 간단하다. 아래는 가장 간편하게 정의한 예이다. 왼쪽부분은 파라미터를 정의한 부분이고 오른쪽 부분의 수식을 정의한 부분이다.
(x: Int) => x + 1
위의 익명함수는 사실 아래와 같은 정의를 단순한 한 것이다.
new Function1[Int,Int] {
override def apply(x: Int): Int = x + 1
}
다수의 파라미터의 경우는 아래와 같이 정의한다.
(x: Int, y:Int) => x + y
파라미터가 없는 경우는 다음과 같다.
() => System.getProperty("user.dir")
위의 각 함수들을 테스트하면 아래와 같이 수행할 수 있다.
object FunctionTest extends App {
val plusOne = (x: Int) => x + 1
val plusOne2 = new Function1[Int,Int] {
override def apply(x: Int): Int = x + 1
}
val plus = (x: Int, y:Int) => x + y
val userDir = () => System.getProperty("user.dir")
println(plusOne(1))
println(plusOne2(1))
println(plus(1,2))
println(userDir)
}
출처 : http://docs.scala-lang.org/tutorials/tour/anonymous-function-syntax
반응형
'Programming > Scala' 카테고리의 다른 글
Scala - 내포된 함수 (0) | 2016.11.02 |
---|---|
Scala - 고차함수 (0) | 2016.11.02 |
Scala - 믹스인 클래스 컴포지션 (0) | 2016.11.01 |
Scala - 트래잇 (0) | 2016.11.01 |
Scala - 클래스 (0) | 2016.11.01 |