Skip to content

Commit

Permalink
[Week1][입문편] 4강 코틀린에서 람다를 다루는 방법 (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
yjsmk0902 committed Jun 26, 2024
1 parent ee93c73 commit 8dad0ea
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
8 changes: 8 additions & 0 deletions 양승민/입문/src/lec18/Fruit.kt
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
)
25 changes: 25 additions & 0 deletions 양승민/입문/src/lec18/lec18Main.kt
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 })
}

0 comments on commit 8dad0ea

Please sign in to comment.