ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
ဒီ lesson ကလည်း အသစ်ဘာမှ မသင်ပါဘူး။ Class၊ inheritance၊ data class၊ collection နဲ့ lambda တွေကို တကယ့် program သေးသေးလေးတစ်ခု လုပ်တာနဲ့ ပိုနီးတဲ့ task ကြီးကြီး သုံးခုအဖြစ် ပေါင်းထားတာပါ။ Fundamentals set လိုပဲ ကိုယ့် version ကို အရင်ရေးပြီး reference solution ကို နှိုင်းယှဉ်ဖို့ပဲ သုံးပါ။
ဆိုင်သေးသေးလေးတစ်ခုနဲ့ တိရစ္ဆာန်ရုံသေးသေးလေးတစ်ခုကို တစ်ပြိုင်နက် လုပ်နေတယ်လို့ မြင်ကြည့်ပါ။ တိရစ္ဆာန်ရုံက မတူတဲ့ တိရစ္ဆာန်တွေပါတဲ့ list တစ်ခုကို အတူတူ ကိုင်တွယ်လို့ရတာကို ပြပါတယ်၊ တိရစ္ဆာန်တိုင်းက သူ့အသံကို သူသိလို့ပါ။ ဆိုင်ကတော့ ကုန်စာရင်းမူရင်းကို မထိဘဲ product list ကို filter၊ အဆင့်ခွဲ၊ လျှော့စျေးပေးလို့ရတာကို ပြပါတယ်။ အဆင့်တိုင်းက list အသစ်ကို ပေးပြီး မူရင်းက အရင်အတိုင်း ကျန်ပါတယ်။
Problem → Why → Solution
ပြဿနာ: ပထမ draft ထိမိတတ်တဲ့ နံရံလေးခု
ဒီ task တွေရဲ့ ပထမ draft က နံရံလေးခုကို ထိမိတတ်ပါတယ်။ Animal("Rex") က abstract class ကို instance မလုပ်နိုင်လို့ compile မဖြစ်ပါဘူး။ product.price = newPrice ကလည်း price က val မို့ compile မဖြစ်ပါဘူး။ sortedByDescending ကို statement အဖြစ် ခေါ်ရင် compile ဖြစ်ပြီး ဘာမှ မလုပ်ဘဲ တိတ်ဆိတ်နေပါတယ်၊ ဘယ်သူမှ မသိမ်းတဲ့ list အသစ်ကို ပြန်ပေးလို့ပါ။ ပြီးတော့ empty list ကို average လုပ်ရင် 0 မဟုတ်ဘဲ NaN ထွက်ပါတယ်။
ပထမနံရံနှစ်ခုက compiler error တွေဖြစ်ပြီး program မ run ခင် တားပေးတဲ့ ချစ်စရာ အမျိုးအစားပါ။ cannot create an instance of an abstract class နဲ့ 'val' cannot be reassigned ပါ။ နောက်နှစ်ခုက compile ဖြစ်၊ run ၊ တစ်ခုခု print ထုတ်လို့ output ကို သေချာဖတ်မှသာ သတိထားမိပါတယ်။
abstract class Animal(val name: String) {
abstract fun sound(): String
}
data class Product(val name: String, val price: Double)
fun main() {
val pet = Animal("Rex") // error: abstract class
val laptop = Product("Laptop", 1_500_000.0)
laptop.price = 1_350_000.0 // error: val
val products = listOf(Product("Pen", 500.0), Product("Laptop", 1_500_000.0))
products.sortedByDescending { it.price } // result thrown away
println(products.map { it.name }) // [Pen, Laptop], not sorted
println(emptyList<Double>().average()) // NaN, not 0
}နှစ်ခုက ဘာကြောင့် မညည်းဘဲ run တာလဲ
sortedByDescending၊ map နဲ့ filter တို့က ခေါ်ထားတဲ့ list ကို ဘယ်တော့မှ မပြောင်းပါဘူး၊ တစ်ခုချင်းစီက list အသစ်ကို ပြန်ပေးပါတယ်။ Return value ကို လျစ်လျူရှုရင် call က ခွင့်ပြုထားပေမယ့် အဓိပ္ပာယ်မရှိပါဘူး။ Empty collection အပေါ် average() က သုညကို သုညနဲ့ စားလို့ Double အတွက် exception မဟုတ်ဘဲ NaN ပါ။ နှစ်ခုလုံးက type error မဟုတ်လို့ compiler က မကူညီနိုင်ပါဘူး၊ empty case ကို ကိုယ်တိုင် စဉ်းစားပြီး ပြန်ရတဲ့ value ကို သုံးရပါမယ်။
Core concept: task တစ်ခုစီမှာ အယူအဆတစ်ခု၊ အကျင့်တစ်ခု
Task သုံးခုစီက intermediate နဲ့ advanced chapter တွေက အယူအဆတစ်ခုစီကို သီးသန့်ထုတ်ပြထားပါတယ်။ စတုတ္ထအချက်က သုံးခုလုံးကို ဖြတ်သန်းပါတယ်။ Function တိုင်းမှာ data မရှိတဲ့ အခြေအနေအတွက် အဖြေ လိုပါတယ်။ Code မရေးခင် ဒီမှတ်စုလေးခုကို ဖတ်ပြီး ကိုယ့် function တွေကို reference solution နဲ့ နှိုင်းယှဉ်ပါ။
Task 1: Abstract class နဲ့ override
abstract class Animal(val name: String) က တိရစ္ဆာန်တိုင်း တူညီတာကို ကိုင်ထားပြီး body မပါတဲ့ abstract fun sound() ကို ကြေညာပါတယ်။ Dog(name: String) : Animal(name) လို subclass က sound() ကို override လုပ်ရမယ်၊ မလုပ်ရင် compiler က ငြင်းပါတယ်။ Dog က Animal ဖြစ်လို့ List<Animal> မှာ ခွေးနဲ့ ကြောင်ကို အတူ ထည့်လို့ရပြီး animal.sound() က object တစ်ခုစီရဲ့ ကိုယ်ပိုင်ဗားရှင်းကို run ပါတယ်။ ဒါကို polymorphism လို့ခေါ်ပါတယ်။ Loop က ဘယ်အမျိုးလဲ ဘယ်တော့မှ မမေးဘဲ describe() ကို base class မှာ တစ်ခါရေးပြီး subclass တိုင်းက ပြန်သုံးပါတယ်။
Task 2: Data list ကို filter၊ sort၊ group လုပ်ပါ
filter က element တွေကို ထိန်းထားတယ်၊ map က ပြောင်းလဲတယ်၊ sortedWith က စီပေးတယ်၊ သုံးခုစလုံးက list အသစ်ကို ပြန်ပေးပါတယ်။ စျေးနှုန်းနှစ်ခု တူရင် စျေးနှုန်းတစ်ခုတည်းနဲ့ sort လုပ်ရင် input အစီအစဉ်အတိုင်း ကျန်ပြီး input ပြောင်းတိုင်း ပြောင်းသွားနိုင်ပါတယ်။ thenBy { it.name } ထည့်ရင် tie-break ကို ရှင်းရှင်းပြောထားရာ ရောက်ပါတယ်။ groupBy { it.category } က Map ပြန်ပေးပြီး map ကို မရှိတဲ့ key နဲ့ ရှာရင် null ရလို့ orEmpty() က မရှိတဲ့ category ကို empty list ဖြစ်စေပါတယ်။
Task 3: Copy လုပ်တဲ့ higher-order function
applyDiscount က discountFn: (Double) -> Double ကို လက်ခံလို့ rule ကို caller ဆုံးဖြတ်ပါတယ်။ ၁၀ ရာခိုင်နှုန်း လျှော့တာ၊ ပုံသေလျှော့တာ၊ ဘာမဆိုပါ။ map က product တိုင်းမှာ အဲဒါကို သုံးပြီး copy(price = ...) က စျေးအသစ်နဲ့ Product အသစ်ကို ဆောက်ကာ မူရင်းကို မထိပါဘူး။ coerceAtLeast(0.0) က စျေးကို သုညအောက် ရောက်စေမယ့် rule ကို ကာကွယ်ပြီး empty list ကတော့ empty list ဖြစ်သွားရုံပါပဲ။
အကျင့်: empty နဲ့ missing က ဘာလဲ ပြောပါ
ဒီမှာ function တိုင်းမှာ data မရှိတဲ့ အခြေအနေအတွက် အဖြေ ရှိပါတယ်။ chorus() က (silence) ကို print ထုတ်တယ်၊ averagePrice() က NaN အစား null ပြန်တယ်၊ byCategory["Toys"].orEmpty() က empty list ပေးတယ်။ ဒီအဖြေတွေကို function ရေးတဲ့အချိန်မှာ ဆုံးဖြတ်ပါ၊ customer က crash တွေ့ပြီးမှ မဟုတ်ပါဘူး။
| Tool | ပြန်ပေးတာ | အံ့ဩစရာ ဖြစ်တဲ့နေရာ |
|---|---|---|
| filter၊ map၊ sortedBy | List အသစ် | ရလဒ်ကို လျစ်လျူရှုရင် ဘာမှ မဖြစ်ဘူး |
| copy() | Data class instance အသစ် | မူရင်းက တန်ဖိုးအဟောင်းကို ထိန်းထားတယ် |
| map[key] | Value ဒါမှမဟုတ် null | မရှိတဲ့ key က error မဟုတ်ဘဲ null ပေးတယ် |
| average() | Double တစ်ခု | Empty collection အတွက် NaN |
| maxByOrNull | Element ဒါမှမဟုတ် null | Empty အတွက် null၊ tie ဆိုရင် ပထမကနိုင်တယ် |
- Class နဲ့ hierarchy နဲ့ model လုပ်
- filter၊ map၊ sort နဲ့ ပြောင်း
- Object အသစ်ပြန်ပေး၊ input ကို ထိန်း
- Empty နဲ့ missing က ဘာလဲ ဆုံးဖြတ်
Collection pipeline ထဲက အဆင့်တိုင်းက အသစ်တစ်ခုကို ထုတ်ပေးတယ်၊ ရှေ့က ဘာကိုမှ မပြင်ပါဘူး။
အခြေခံ ပုံစံ
abstract class Base(val name: String) {
abstract fun act(): String
}
class Child(name: String) : Base(name) {
override fun act() = "..."
}
val ranked = items.filter { it.size > 3 }.sortedByDescending { it.size }
val changed = items.map { it.copy(size = rule(it.size)) }
val group = byKind["missing"].orEmpty()Abstract base က subclass တွေ ဘာလုပ်ရမလဲ ကြေညာတယ်၊ collection function တွေက list အသစ်ပြန်ပေးတယ်၊ copy() က ပြောင်းထားတဲ့ duplicate ဆောက်တယ်၊ orEmpty() က မရှိတဲ့ lookup ကို empty list ဖြစ်စေတယ်။
Reference answer: edge case တွေပါတဲ့ task သုံးခု
Task တစ်ခုချင်းစီက function သေးသေးလေးဖြစ်ပြီး main က ပုံမှန် data နဲ့ edge case အနည်းဆုံးတစ်ခုနဲ့ စမ်းပါတယ်။ Empty list၊ tie၊ မရှိတဲ့ category ဒါမှမဟုတ် ကြီးလွန်းတဲ့ discount တို့ပါ။ ဒီမှာ ငွေကို exercise တိုအောင်ပဲ Double နဲ့ သိမ်းထားတာပါ၊ production code မှာ BigDecimal ဒါမှမဟုတ် အသေးဆုံး ယူနစ်ကို ကိန်းပြည့်နဲ့ သုံးပါလိမ့်မယ်။ အောက်မှာ print ထွက်တဲ့ နံပါတ်တိုင်းက ဒီ program ကို တကယ် run ထားတာကနေ ရတာပါ။
abstract class Animal(val name: String) {
abstract fun sound(): String
fun describe(): String = "$name says ${sound()}"
}
class Dog(name: String) : Animal(name) {
override fun sound() = "Woof"
}
class Cat(name: String) : Animal(name) {
override fun sound() = "Meow"
}
data class Product(val name: String, val price: Double, val category: String)
fun chorus(animals: List<Animal>): String =
if (animals.isEmpty()) "(silence)" else animals.joinToString(" / ") { it.describe() }
fun rankByPrice(products: List<Product>): List<Product> =
products.sortedWith(compareByDescending<Product> { it.price }.thenBy { it.name })
fun averagePrice(products: List<Product>): Double? =
if (products.isEmpty()) null else products.map { it.price }.average()
fun applyDiscount(
products: List<Product>,
discountFn: (Double) -> Double,
): List<Product> =
products.map { it.copy(price = discountFn(it.price).coerceAtLeast(0.0)) }
fun main() {
val animals: List<Animal> = listOf(Dog("Rex"), Cat("Mimi"))
println("Task 1: " + chorus(animals))
println("Task 1 (empty): " + chorus(emptyList()))
val products = listOf(
Product("Laptop", 1_500_000.0, "Electronics"),
Product("Mouse", 25_000.0, "Electronics"),
Product("Book", 8_000.0, "Stationery"),
Product("Phone", 1_200_000.0, "Electronics"),
Product("Keyboard", 25_000.0, "Electronics"),
Product("Pen", 500.0, "Stationery"),
)
val expensive = products.filter { it.price > 1_000_000 }.map { it.name }
println("Task 2 expensive: $expensive")
println("Task 2 ranked: ${rankByPrice(products).map { it.name }}")
val byCategory = products.groupBy { it.category }
val stationery = byCategory["Stationery"].orEmpty()
val toys = byCategory["Toys"].orEmpty()
println("Task 2 categories: Stationery=${stationery.size}, Toys=${toys.size}")
val average = averagePrice(products)
println("Task 2 average: $average, empty: ${averagePrice(emptyList())}")
val discounted = applyDiscount(products) { price -> price * 0.9 }
println("Task 3 discounted: ${discounted.map { it.price }}")
println("Task 3 original untouched: ${products.first().price}")
val clamped = applyDiscount(products) { price -> price - 50_000.0 }
println("Task 3 oversized discount: ${clamped.map { it.price }}")
println("Task 3 empty: ${applyDiscount(emptyList()) { it * 0.5 }}")
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Abstract member
abstract fun sound(): Stringဒီမှာ body မပါပါဘူး။ Concrete subclass တိုင်းက တစ်ခု ပေးရပါမယ်။ Animal("Rex") က compile မဖြစ်ပေမယ့် Dog("Rex") က ဖြစ်ပြီး base class ထဲက describe() က တကယ့် object မှာ ရှိတဲ့ sound() ကို ခေါ်ပါတယ်။
- Override in a subclass
override fun sound() = "Woof"override က မဖြစ်မနေ လိုပြီး return type String ကို literal ကနေ ခန့်မှန်းပါတယ်။ Cat ကလည်း Meow နဲ့ အတူတူလုပ်လို့ list က တိရစ္ဆာန်တစ်ကောင်စီရဲ့ ကိုယ်ပိုင်အသံကို print ထုတ်ပါတယ်။
- Explicit tie-break
products.sortedWith(compareByDescending<Product> { it.price }.thenBy { it.name })Mouse နဲ့ Keyboard နှစ်ခုစလုံး 25000.0 ပါ။ thenBy က Keyboard ကို Mouse ရှေ့မှာ အက္ခရာစဉ်အတိုင်း ထားလို့ ranked output က ခန့်မှန်းလို့ရပြီး input အစီအစဉ်ပေါ် မမူတည်ပါဘူး။
- Null instead of NaN
if (products.isEmpty()) null else products.map { it.price }.average()Empty collection ရဲ့ average() က NaN ဖြစ်ပြီး နောက်ပိုင်း တွက်ချက်မှုတွေထဲ ပျံ့နှံ့သွားပါတယ်။ Double? ပြန်ရင် empty case ကို ရှင်းရှင်းပြတာပါ။ Product ခြောက်ခုအတွက် average က 459750.0၊ မရှိရင် null ပါ။
- A rule parameter that returns copies
products.map { it.copy(price = discountFn(it.price).coerceAtLeast(0.0)) }discountFn က စျေးအသစ်ကို ဆုံးဖြတ်ပြီး coerceAtLeast(0.0) က အနုတ်မဖြစ်အောင် တားကာ copy က Product object အသစ်တွေ ဆောက်ပါတယ်။ မူရင်း list ကို မထိပါဘူး၊ original untouched စာကြောင်းက သက်သေပြပါတယ်။
- A missing key
val toys = byCategory["Toys"].orEmpty()groupBy က ရှိတဲ့ key တွေကိုပဲ ဆောက်လို့ Toys ဆိုတဲ့ category မရှိပါဘူး။ Lookup က null ပြန်ပြီး orEmpty() က size 0 ရှိတဲ့ empty list ဖြစ်စေပါတယ်။
Task 1: Rex says Woof / Mimi says Meow
Task 1 (empty): (silence)
Task 2 expensive: [Laptop, Phone]
Task 2 ranked: [Laptop, Phone, Keyboard, Mouse, Book, Pen]
Task 2 categories: Stationery=2, Toys=0
Task 2 average: 459750.0, empty: null
Task 3 discounted: [1350000.0, 22500.0, 7200.0, 1080000.0, 22500.0, 450.0]
Task 3 original untouched: 1500000.0
Task 3 oversized discount: [1450000.0, 0.0, 0.0, 1150000.0, 0.0, 0.0]
Task 3 empty: []copy()၊ map နဲ့ sorting က မူရင်းကို မပြင်ဘူး
Collection မှာ အဖြစ်အများဆုံး အံ့ဩစရာတွေက identity နဲ့ပဲ ဆိုင်ပါတယ်။ ဘယ် list ဒါမှမဟုတ် object က အသစ်လဲ ဆိုတာပါ။ Snippet ကို run ပြီး စာကြောင်းလေးကြောင်းကို အစဉ်လိုက် ဖတ်ပါ။
data class Product(val name: String, val price: Double)
fun main() {
val original = listOf(Product("Pen", 500.0), Product("Book", 8000.0))
val halfPrice = original.map { it.copy(price = it.price * 0.5) }
println(original.map { it.price })
println(halfPrice.map { it.price })
val ranked = original.sortedByDescending { it.price }
println(ranked.map { it.name })
println(original.map { it.name })
}halfPrice မှာ စျေးတစ်ဝက်ဖြစ်တဲ့ Product object အသစ်တွေ ရှိပြီး original က 500.0 နဲ့ 8000.0 ကို ဆက်ပြပါတယ်။ ranked က စျေးနဲ့ sort လုပ်ထားတဲ့ list အသစ်ဖြစ်ပြီး original က input အစီအစဉ်ကို ထိန်းထားပါတယ်။ မူရင်း list ကိုယ်တိုင် ပြောင်းစေချင်ရင် MutableList နဲ့ sortByDescending လို in-place call လိုပါတယ်၊ sortedByDescending နဲ့ function မတူပါဘူး။
Abstract class လား interface လား
Animal ကို interface အဖြစ်လည်း ရေးလို့ရပါတယ်။ ရွေးချယ်မှုက type တွေ ဘာတူညီလဲပေါ် မူတည်ပြီး အောက်က table က ဆုံးဖြတ်ပုံရဲ့ အတိုချုပ်ပါ။
| မေးခွန်း | Abstract class | Interface |
|---|---|---|
| Constructor ထဲမှာ state ကိုင်လို့ရလား | ရတယ်၊ Animal(val name: String) လို | Constructor မရှိ၊ backing field မရှိ |
| Class တစ်ခုက ဘယ်နှခု သုံးလို့ရလဲ | တစ်ခုပဲ extend လုပ်လို့ရတယ် | လိုသလောက် implement လုပ်လို့ရတယ် |
| အသုံးများပုံ | Code မျှဝေတဲ့ ဘုံ base | Reportable လို စွမ်းရည်တစ်ခု |
လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ
Abstract base ပါတဲ့ class hierarchy တွေက list တစ်ခုထဲမှာ အမျိုးမျိုးသော အရာတွေ ထည့်ထားတဲ့ နေရာတိုင်းမှာ ရှိပါတယ်။ ပုံဆွဲ program ထဲက ပုံသဏ္ဍာန်တွေ၊ checkout မှာ ငွေပေးချေမှုနည်းလမ်းတွေ၊ server ထဲက notification channel တွေပါ။ Subclass အသစ်ပေါ်လာရင်လည်း list ကို loop ပတ်တဲ့ code က မပြောင်းရပါဘူး၊ Animal task ရဲ့ အဓိကအချက် အဲဒါပါပဲ။ ခေတ်သစ် Kotlin မှာ subclass အစုအဝေး ပိတ်ထားရင် ဒီအလုပ်အတွက် sealed class နဲ့ interface တွေကို ရွေးသုံးတာလည်း တွေ့ရပါလိမ့်မယ်။
Product task တွေက report code ကို ပုံတူကူးထားတာပါ။ Order တွေကို filter လုပ်၊ category နဲ့ group လုပ်၊ တန်ဖိုးနဲ့ sort လုပ်ပြီး ရလဒ်ကို format လုပ်ပါတယ်။ Production code မှာတော့ ငွေကို Double နဲ့ မသိမ်းဘဲ BigDecimal ဒါမှမဟုတ် အသေးဆုံးငွေကြေး ယူနစ်ရဲ့ ကိန်းပြည့်နဲ့ သိမ်းပါလိမ့်မယ်၊ Double က 0.1 လို ပမာဏကို တိတိကျကျ မဖော်ပြနိုင်လို့ပါ။ Immutable copy တွေက thread နှစ်ခုက product list တစ်ခုတည်းကို ဖတ်လို့ရပေမယ့် ဘယ်သူမှ မပြောင်းနိုင်တဲ့ concurrent program တွေမှာ ဒီ pipeline တွေကို ဘေးကင်းစေတာလည်း ဖြစ်ပါတယ်။
အမှားများ
ဖြစ်လေ့ရှိတဲ့ အမှားများ
Abstract class ကို instance လုပ်ဖို့ ကြိုးစားတာ
Animal("X") က cannot create an instance of an abstract class ဆိုတဲ့ error နဲ့ fail ပါတယ်။ Dog လို concrete subclass ကို ဆောက်ပါ၊ ဒါမှမဟုတ် List<Animal> ကြေညာပြီး subclass instance တွေနဲ့ ဖြည့်ပါ။
Data class property ကို assign လုပ်တာ
product.price = newPrice က 'val' cannot be reassigned ဆိုတဲ့ error နဲ့ fail ပါတယ်။ product.copy(price = newPrice) နဲ့ ပြောင်းထားတဲ့ copy ကို ဆောက်ပြီး လိုတဲ့နေရာမှာ အဲဒီ copy ကို သိမ်းပါ။
sortedByDescending၊ map၊ filter ရဲ့ ရလဒ်ကို လျစ်လျူရှုတာ
ဒီ function တွေက list အသစ်တွေ ပြန်ပေးပါတယ်။ products.sortedByDescending { it.price } ကို သူ့စာကြောင်းမှာ ရေးရင် products မပြောင်းဘဲ နောက်မှ products ကို print ထုတ်ရင် အစီအစဉ်အဟောင်းကိုပဲ ပြပါတယ်။ ရလဒ်ကို val ထဲ assign လုပ်ပါ၊ ဒါမှမဟုတ် MutableList မှာ in-place sortByDescending ကို သုံးပါ။
Empty list ရဲ့ average သို့ maximum ကို မစစ်ဘဲ ယူတာ
Empty collection အပေါ် average() က NaN ပေးပြီး maxOf က empty collection မှာ NoSuchElementException ပစ်ပါတယ်၊ maxByOrNull ကတော့ null ပြန်ပါတယ်။ null ပြန်တဲ့ ဗားရှင်းကို ရွေးပါ၊ ဒါမှမဟုတ် isEmpty() ကို အရင်စစ်ပါ။
Mini challenge
Shape၊ အဆင့်ခွဲခြင်းနဲ့ ပြောင်းလို့ရတဲ့ top-N
အယူအဆတူတွေကို domain အသစ်မှာ ကျင့်ပါ။ Subclass နှစ်ခုပါတဲ့ abstract Shape class ကို ဆောက်ပြီး empty list အတွက် null ပြန်တဲ့ largest(shapes)၊ shape နာမည်တစ်ခုချင်းစီအတွက် area ပေါင်းတဲ့ areaByKind(shapes) နဲ့ ranking rule အဖြစ် function တစ်ခုကို လက်ခံတဲ့ topN(shapes, n, selector) ကို ရေးပါ။ Empty list၊ tie နဲ့ ကြီးလွန်း ဒါမှမဟုတ် အနုတ်ဖြစ်တဲ့ n ကို စမ်းပါ။
- Shape က abstract ဖြစ်ပြီး abstract area() ပါရမယ်၊ Rectangle နဲ့ Triangle က override လုပ်ရမယ်
- largest() က Shape? ပြန်ပြီး empty list အတွက် null ဖြစ်ရမယ်၊ tie ဆိုရင် ပထမ shape က နိုင်ရမယ်
- areaByKind() က Map<String, Double> ပြန်ပြီး empty list အတွက် empty map ပြန်ရမယ်
- topN() က selector: (Shape) -> Double ကိုယူပြီး tie ကို နာမည်နဲ့ဖြတ်ကာ n <= 0 ဒါမှမဟုတ် n > size အတွက် ဘယ်တော့မှ မ throw ရဘူး
abstract class Shape(val name: String) {
// TODO: abstract fun area(): Double, and a toString() such as Rectangle(12.0)
}
class Rectangle(/* width, height */) : Shape("Rectangle") {
// TODO: override area()
}
class Triangle(/* base, height */) : Shape("Triangle") {
// TODO: override area()
}
fun largest(shapes: List<Shape>): Shape? {
// TODO: null for an empty list, the first shape wins a tie
}
fun areaByKind(shapes: List<Shape>): Map<String, Double> {
// TODO: group by name and sum the areas
}
fun topN(shapes: List<Shape>, n: Int, selector: (Shape) -> Double): List<Shape> {
// TODO: sort by selector descending, then by name; guard n <= 0
}
fun main() {
// TODO: four shapes (include a tie), then test empty lists and n = 2, 10, -3
}ရလဒ်: largest က Rectangle(12.0) ကို print ထုတ်ပြီး မရှိရင် null၊ areaByKind က Rectangle=16.0 နဲ့ Triangle=17.0 ကို print ထုတ်ပြီး မရှိရင် empty map၊ topN နှစ်ခုက Rectangle(12.0) နဲ့ Triangle(12.0) ကို ပြမယ်၊ n = 10 က shape လေးခုလုံး ပြန်မယ်၊ n = -3 က empty list ပြန်မယ်၊ ပြောင်းပြန် selector က အသေးဆုံး shape နှစ်ခုကို ပြပါမယ်။
အရိပ်အမြွက် ကြည့်ရန်
maxByOrNull { it.area() } က empty list အတွက် null ပြန်ပါတယ်။ groupBy { it.name } နောက်မှာ mapValues { (_, group) -> group.sumOf { it.area() } } က ပေါင်းလဒ်တွေ ပေးပါတယ်။ topN အတွက် sortedWith(compareByDescending<Shape> { selector(it) }.thenBy { it.name }) ကို သုံးပြီး take(n.coerceAtLeast(0)) လုပ်ပါ။
အဖြေ ကြည့်ရန်
abstract class Shape(val name: String) {
abstract fun area(): Double
override fun toString() = "$name(${area()})"
}
class Rectangle(private val width: Double, private val height: Double) :
Shape("Rectangle") {
override fun area() = width * height
}
class Triangle(private val base: Double, private val height: Double) : Shape("Triangle") {
override fun area() = base * height / 2
}
fun largest(shapes: List<Shape>): Shape? = shapes.maxByOrNull { it.area() }
fun areaByKind(shapes: List<Shape>): Map<String, Double> =
shapes.groupBy { it.name }.mapValues { (_, group) -> group.sumOf { it.area() } }
fun topN(shapes: List<Shape>, n: Int, selector: (Shape) -> Double): List<Shape> =
shapes.sortedWith(compareByDescending<Shape> { selector(it) }.thenBy { it.name })
.take(n.coerceAtLeast(0))
fun main() {
val shapes = listOf(
Rectangle(3.0, 4.0),
Triangle(6.0, 4.0),
Rectangle(2.0, 2.0),
Triangle(10.0, 1.0),
)
println("largest: ${largest(shapes)}")
println("largest of none: ${largest(emptyList())}")
println("area by kind: ${areaByKind(shapes)}")
println("area of none: ${areaByKind(emptyList())}")
println("top 2 by area: ${topN(shapes, 2) { it.area() }}")
println("top 10 by area: ${topN(shapes, 10) { it.area() }.size} shapes")
println("top -3 by area: ${topN(shapes, -3) { it.area() }}")
println("smallest first: ${topN(shapes, 2) { -it.area() }}")
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Abstract class က shared state ကို ကိုင်ပြီး abstract member တွေကို ကြေညာတယ်၊ subclass တိုင်းက override လုပ်ရမယ်၊ List<Animal> တစ်ခုက subclass အားလုံးကို ကိုင်နိုင်တယ်
- filter၊ map နဲ့ sortedBy တို့က list အသစ်ပြန်ပေးလို့ ရလဒ်ကို သုံးပါ၊ tie တွေကို ခန့်မှန်းလို့ရစေချင်ရင် thenBy ထည့်ပါ
- Data class ရဲ့ copy() က ပြောင်းထားတဲ့ duplicate ကို ဆောက်လို့ higher-order function တစ်ခုက မူရင်းတွေကို မပြင်ဘဲ product တွေကို ပြောင်းလဲနိုင်ပါတယ်
- Empty နဲ့ missing က ဘာလဲ ဆုံးဖြတ်ပါ။ map lookup အတွက် orEmpty()၊ average အတွက် NaN အစား null ပါ
Kotlin language guide — Kotlin