17번째 줄: | 17번째 줄: | ||
==Introduction== | ==Introduction== | ||
{{참고|#TestShop.kt & Shop.kt}} | |||
<source lang='kotlin'> | <source lang='kotlin'> | ||
</source> | </source> | ||
==Filter map== | ==Filter map== | ||
{{참고|#TestShop.kt & Shop.kt}} | |||
<source lang='kotlin'> | <source lang='kotlin'> | ||
</source> | </source> | ||
==All Any and other predicates== | ==All Any and other predicates== | ||
{{참고|#TestShop.kt & Shop.kt}} | |||
<source lang='kotlin'> | <source lang='kotlin'> | ||
</source> | </source> | ||
==FlatMap== | ==FlatMap== | ||
{{참고|#TestShop.kt & Shop.kt}} | |||
<source lang='kotlin'> | <source lang='kotlin'> | ||
</source> | </source> | ||
==Max min== | ==Max min== | ||
{{참고|#TestShop.kt & Shop.kt}} | |||
<source lang='kotlin'> | <source lang='kotlin'> | ||
</source> | </source> | ||
==Sort== | ==Sort== | ||
{{참고|#TestShop.kt & Shop.kt}} | |||
<source lang='kotlin'> | <source lang='kotlin'> | ||
</source> | </source> | ||
==Sum== | ==Sum== | ||
{{참고|#TestShop.kt & Shop.kt}} | |||
<source lang='kotlin'> | <source lang='kotlin'> | ||
</source> | </source> | ||
==GroupBy== | ==GroupBy== | ||
{{참고|#TestShop.kt & Shop.kt}} | |||
<source lang='kotlin'> | <source lang='kotlin'> | ||
</source> | </source> | ||
==Partition== | ==Partition== | ||
{{참고|#TestShop.kt & Shop.kt}} | |||
<source lang='kotlin'> | <source lang='kotlin'> | ||
</source> | </source> | ||
==Fold== | ==Fold== | ||
{{참고|#TestShop.kt & Shop.kt}} | |||
<source lang='kotlin'> | <source lang='kotlin'> | ||
</source> | </source> | ||
==Compound tasks== | ==Compound tasks== | ||
{{참고|#TestShop.kt & Shop.kt}} | |||
<source lang='kotlin'> | <source lang='kotlin'> | ||
</source> | </source> | ||
==Get used to new style== | ==Get used to new style== |
2019년 4월 7일 (일) 00:57 판
# | Kotlin Koans |
---|---|
Kotlin Koans - Introduction | |
Kotlin Koans - Conventions | |
Kotlin Koans - Collections | |
Kotlin Koans - Properties | |
Kotlin Koans - Builders | |
Kotlin Koans - Generics |
1 Introduction
#TestShop.kt & Shop.kt 문서를 참고하십시오.
Kotlin
2 Filter map
#TestShop.kt & Shop.kt 문서를 참고하십시오.
Kotlin
3 All Any and other predicates
#TestShop.kt & Shop.kt 문서를 참고하십시오.
Kotlin
4 FlatMap
#TestShop.kt & Shop.kt 문서를 참고하십시오.
Kotlin
5 Max min
#TestShop.kt & Shop.kt 문서를 참고하십시오.
Kotlin
6 Sort
#TestShop.kt & Shop.kt 문서를 참고하십시오.
Kotlin
7 Sum
#TestShop.kt & Shop.kt 문서를 참고하십시오.
Kotlin
8 GroupBy
#TestShop.kt & Shop.kt 문서를 참고하십시오.
Kotlin
9 Partition
#TestShop.kt & Shop.kt 문서를 참고하십시오.
Kotlin
10 Fold
#TestShop.kt & Shop.kt 문서를 참고하십시오.
Kotlin
11 Compound tasks
#TestShop.kt & Shop.kt 문서를 참고하십시오.
Kotlin
12 Get used to new style
#TestShop.kt & Shop.kt 문서를 참고하십시오.
Kotlin
Copy
fun doSomethingStrangeWithCollection(collection: Collection<String>): Collection<String>? {
val groupsByLength = collection. groupBy { s -> s.length }
val maximumSizeOfGroup = groupsByLength.values.map { group -> group.size }.max()
return groupsByLength.values.firstOrNull { group -> group.size == maximumSizeOfGroup }
}
13 TestShop.kt & Shop.kt
TestShop.kt
Kotlin
Copy
//products
val idea = Product("IntelliJ IDEA Ultimate", 199.0)
val reSharper = Product("ReSharper", 149.0)
val dotTrace = Product("DotTrace", 159.0)
val dotMemory = Product("DotTrace", 129.0)
val dotCover = Product("DotCover", 99.0)
val appCode = Product("AppCode", 99.0)
val phpStorm = Product("PhpStorm", 99.0)
val pyCharm = Product("PyCharm", 99.0)
val rubyMine = Product("RubyMine", 99.0)
val webStorm = Product("WebStorm", 49.0)
val teamCity = Product("TeamCity", 299.0)
val youTrack = Product("YouTrack", 500.0)
//customers
val lucas = "Lucas"
val cooper = "Cooper"
val nathan = "Nathan"
val reka = "Reka"
val bajram = "Bajram"
val asuka = "Asuka"
val riku = "Riku"
//cities
val Canberra = City("Canberra")
val Vancouver = City("Vancouver")
val Budapest = City("Budapest")
val Ankara = City("Ankara")
val Tokyo = City("Tokyo")
fun customer(name: String, city: City, vararg orders: Order) = Customer(name, city, orders.toList())
fun order(vararg products: Product, isDelivered: Boolean = true) = Order(products.toList(), isDelivered)
fun shop(name: String, vararg customers: Customer) = Shop(name, customers.toList())
val shop = shop("jb test shop",
customer(lucas, Canberra,
order(reSharper),
order(reSharper, dotMemory, dotTrace)
),
customer(cooper, Canberra),
customer(nathan, Vancouver,
order(rubyMine, webStorm)
),
customer(reka, Budapest,
order(idea, isDelivered = false),
order(idea, isDelivered = false),
order(idea)
),
customer(bajram, Ankara,
order(reSharper)
),
customer(asuka, Tokyo,
order(idea)
),
customer(riku, Tokyo,
order(phpStorm, phpStorm),
order(phpStorm)
)
)
val customers: Map<String, Customer> = shop.customers.fold(hashMapOf<String, Customer>(), {
map, customer ->
map[customer.name] = customer
map
})
val orderedProducts = setOf(idea, reSharper, dotTrace, dotMemory, rubyMine, webStorm, phpStorm)
val sortedCustomers = listOf(cooper, nathan, bajram, asuka, lucas, riku, reka).map { customers[it] }
val groupedByCities = mapOf(
Canberra to listOf(lucas, cooper),
Vancouver to listOf(nathan),
Budapest to listOf(reka),
Ankara to listOf(bajram),
Tokyo to listOf(asuka, riku)
).mapValues { it.value.map { name -> customers[name] } }
Shop.kt
Kotlin
Copy
data class Shop(val name: String, val customers: List<Customer>)
data class Customer(val name: String, val city: City, val orders: List<Order>) {
override fun toString() = "$name from ${city.name}"
}
data class Order(val products: List<Product>, val isDelivered: Boolean)
data class Product(val name: String, val price: Double) {
override fun toString() = "'$name' for $price"
}
data class City(val name: String) {
override fun toString() = name
}
편집자 Jmnote 125.143.162.136 Jmnote bot
로그인하시면 댓글을 쓸 수 있습니다.