-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Week1][입문편] 4강 코틀린에서 람다를 다루는 방법 (#5)
- Loading branch information
Showing
2 changed files
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package lec18 | ||
|
||
data class Fruit( | ||
val id: Long, | ||
val name: String, | ||
val factoryPrice: Long, | ||
val currentPrice: Long | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package lec18 | ||
|
||
fun main() { | ||
val fruits: List<Fruit> = listOf() | ||
val apples = fruits.filterIndexed { idx, fruit -> | ||
println(idx) | ||
fruit.name == "사과" | ||
} | ||
val applePrices = fruits.filter { fruit -> fruit.name == "사과" } | ||
.map { fruit -> fruit.currentPrice } | ||
|
||
val applePricesWithIndex = fruits.filter { fruit -> fruit.name == "사과" } | ||
.mapIndexed { idx, fruit -> | ||
println(idx) | ||
fruit.currentPrice | ||
} | ||
|
||
val isAllApple = fruits.all { fruit -> fruit.name == "사과" } | ||
val isNoApple = fruits.none { fruit -> fruit.name == "사과" } | ||
|
||
val map: Map<String, List<Long>> = fruits | ||
.groupBy({ fruit -> fruit.name }, { fruit -> fruit.currentPrice }) | ||
val map2: Map<Long, Long> = fruits | ||
.associateBy({fruit -> fruit.id}, { fruit -> fruit.currentPrice }) | ||
} |