람다
* 람다
- 람다는 이름을 가질 필요 없는 익명 함수
* 람다 구조 예시
val sum2 : (Int, Int) -> (Int) = { x, y ->
print("x + y =")
x + y
}
* 람다 구조 설명
//변수 sum 선언 옆
1. (Int, Int) : 람다의 입력 변수 자료형
2. -> (Int) : 람다의 출력 변수 자료형
//중괄호 내부
3. x, y : 입력 변수
4. print문과 x+y는 본문 (x+y : 반환값)
-> 입, 출력 변수 부분은 생략이 가능함
* 람다 구조 예시2(입출력 변수 타입 생략)
val sum2 = { x:Int, y:Int ->
print("x + y =")
x + y
}
-> x와 y 변수 옆에 자료형 명시
filter
filter란? : 특정 기준을 거쳐 필터링 된 요소만 걸러내어 리스트로 얻을 수 있는 함수
종류 및 구조 | 설명 |
- filter() Result<T>.filter(predicate:(T) -> Boolean) : List<T> |
Result의 원소들을 iterate하여 predicate의 반환 값이 true인 원소만 뽑은 리스트를 반환 |
- filterNot() Result.filterNot(predicate:(T) -> Boolean) : List |
Result의 원소들을 iterate하여 predicate의 반환 값이 false인 원소만 뽑은 리스트를 반환 |
- filterIndexed() Result.filter(predicate:(T) -> Boolean) : List |
fliter() 함수의 predicate에 원소 뿐만 아니라 index도 활용 가능한 함수 |
- filterNotNull() Result<T?>.filterNotNull() : List<T> |
Result의 원소들을 iterate하여 Null이 아닌 원소만 뽑은 리스트를 반환 |
- filterIsInstane() Result<*>.filterIsInstane() : List<@kotlin.internal.NoInfer R> |
Iterable의 원소들을 iterate하여 R의 타입인 원소들만 반환 |
- filterTo() Result.filterTo(destination:C, Predicate:(T) -> Boolean) : C |
Result의 원소들을 iterate하여 predicate의 반환값이 true인 원소만 뽑아 destination에 추가 |
iterate :성공적인 결과를 얻을 때까지 반복하는 걸 의미
predicate : 조건의 Boolean 값을 반환(조건)
destination : 목적지(저장할 해당 위치?)
* filter 예제
fun main() {
val arrayList = arrayListOf(1, 2, 3, 4, 5, 6, 7, 8, 9)
val arrayList2 = arrayListOf(null, "test1", 'C', "test2", 6)
//filter() : 조건(it%2==0)을 만족하는 원소만 담은 리스트를 반환
print("filter() : ")
println(arrayList.filter { it % 2 == 0 })
//filterNot() : 조건(it%2==0)을 만족하지 않는 원소만 담은 리스트를 반환
print("filterNot() : ")
println(arrayList.filterNot { it % 2 == 0 })
//filterIndexed() : 위치의 번호(index)와 해당 위치의 값(i)을 더한 후 3으로 나눈 나머지가 0인 원소만 반환
print("filterIndexed() : ")
println(arrayList.filterIndexed { index, i ->
(index + i) % 3 == 0
})
//filterNotNull() : null이 아닌 원소들만 담은 리스트를 반환
print("filterNotNull() : ")
println(arrayList2.filterNotNull())
//filterIsInstance() : 특정 타입(현재 String)의 원소만 담은 리스트를 반환
print("filterIsInstance() : ")
println(arrayList2.filterIsInstance<String>())
//filterTo() : arrayList를 filter 조건에 맞는 값을 반환한 후 목적지(arrayList2)에 추가함
print("filterTo() : ")
arrayList.filterTo(arrayList2) { it % 2 == 0 }
println(arrayList2)
}
* 출력 결과
filter() : [2, 4, 6, 8]
filterNot() : [1, 3, 5, 7, 9]
filterIndexed() : [2, 5, 8]
filterNotNull() : [test1, C, test2, 6]
filterIsInstance() : [test1, test2]
filterTo() : [null, test1, C, test2, 6, 2, 4, 6, 8]
map
map이란? 어떤 함수를 거쳐 변환된 결과를 담은 리스트를 반환하는 기능
종류 및 구조 | 설명 |
- map() Result<T>.map(transform :(T) -> R) : List<R> |
Result의 원소들을 map을 통해 transform한 결과를 담은 리스트를 반환 |
- mapIndexed() Result<T>.mapIndexed(transform :(index : Int, T) -> R) : List<R> |
transform에 원소 뿐만 아니라 index도 활용 |
- mapNotNull() Result<T>.mapNotNull(transform:(T) -> R?) : List<R> |
map()의 결과를 destination에 추가하는 함수 |
- mapTo() Result<T>.mapTo(destination : C, transform :(T) -> R) : C |
Result의 원소들을 iterate하여 Null이 아닌 원소만 transform한 결과를 담은 리스트를 변환 |
transform : 변환한다는 의미
* map과 filter의 차이점
- map의 경우는 원소들을 특정 함수 혹은 식을 통해 변환하여 결괏값을 출력하는 것
- filter는 조건을 작성하여 해당 조건을 만족(true)하는 원소들을 반환하는 것
- 그래서 map은 특정 조건을 거쳐서 반환하는 mapNot, mapIsInstance가 없음(NotNull은 존재함)
* map 예제
fun main() {
val arrayList = arrayListOf(1, 2, 3, 4, 5, 6, 7, 8, 9)
val arrayList2 = arrayListOf(null, "result1", 'A', "result2", 6)
//map() : 원소들을 2를 곱한 값으로 변환한 후 리스트로 반환
print("map() : ")
println(arrayList.map { it * 2 })
//mapIndexed() : 위치(index)와 해당 위치의 원소의 값(i)을 곱한 값으로 변환한 후 리스트로 반환
print("mapIndexed() : ")
println(arrayList.mapIndexed { index, i ->
index * i
})
//mapNotNull() : 원소들 중에 Null값이 아닌 원소들만 리스트로 반환
print("mapNotNull() : ")
println(arrayList2.mapNotNull { it })
//mapTo() : arrayList의 원소들을 2를 곱한 값으로 변환한 후 목적지(arrayList2)에 추가함
print("mapTo() : ")
arrayList.mapTo(arrayList2) { it * 2 }
println(arrayList2)
}
* 출력 결과
map() : [2, 4, 6, 8, 10, 12, 14, 16, 18]
mapIndexed() : [0, 2, 6, 12, 20, 30, 42, 56, 72]
mapNotNull() : [result1, A, result2, 6]
mapTo() : [null, result1, A, result2, 6, 2, 4, 6, 8, 10, 12, 14, 16, 18]
참고 자료 및 보면 유용한 자료
'Android 앱개발 공부 > TIL(Today I Learned)' 카테고리의 다른 글
[Android] TIL 29일차 (0) | 2024.07.04 |
---|---|
[Android] TIL 28일차 (0) | 2024.06.28 |
[Android] TIL 26일차 (0) | 2024.06.26 |
[Android] TIL 25일차 (0) | 2024.06.25 |
[Android] TIL 24일차 (0) | 2024.06.24 |