ရိုးရိုးလေး ရှင်းပြမယ်
Mini project ကို ရိုးရိုးလေး ပြောရရင်
ဒီ mini project မှာ course စာရင်းသွင်းတဲ့ report သေးသေးလေးတစ်ခု ဆောက်ပါမယ်: စာရင်းသွင်းတဲ့ line အနည်းငယ် ဝင်သွားပြီး course တစ်ခုချင်းစီရဲ့ ဝင်ငွေ၊ မပေးရသေးတဲ့ လက်ကျန်နဲ့ ပယ်ချခံရတဲ့ line တွေပါတဲ့ ဖတ်လို့လွယ်တဲ့ report ထွက်လာပါမယ်။ Line တွေကို program ထဲမှာ list တစ်ခုအဖြစ် ရေးထားတာမို့ keyboard input မလို၊ file မလိုဘဲ အမြဲ တူညီစွာ run ပါတယ်။ ထဲမှာပါတာအားလုံး အရင် lesson တွေကပါ: data class၊ list၊ function၊ lambda၊ extension function၊ Result နဲ့ sealed interface တို့ပါ။
ဆိုင်ငယ်တစ်ခုရဲ့ ညနေ အလုပ်ပြီးချိန် လုပ်ငန်းစဉ်ကို စဉ်းစားကြည့်ပါ။ အရောင်းစလစ်တွေ ဝင်လာပြီး တချို့က မှိန်နေတာ ဒါမှမဟုတ် မပြည့်စုံတာ ရှိပါတယ်။ ဖတ်မရတဲ့ဟာတွေကို မှတ်စုနဲ့ ဖယ်ထားပြီး ကောင်းတာတွေကို ကုန်ပစ္စည်းအလိုက် ပေါင်းကာ ပိုင်ရှင်အတွက် စာမျက်နှာတစ်ခု အကျဉ်းချုပ် ရေးပါတယ်။ ဒီ program ကလည်း အလုပ်သုံးခု လုပ်ပါတယ်: စလစ်တွေကို ခွဲ၊ ကောင်းတာတွေကို ပေါင်းပြီး ဖယ်ထားတာတွေကို report လုပ်ပါတယ်။
Problem → Why → Solution
ပြဿနာ: main ထဲမှာ အားလုံးရောထားတဲ့ program
အလွယ်ဆုံး ရေးနည်းက main function ရှည်ကြီးတစ်ခုပါ: line တစ်ခုချင်းစီကို split လုပ်၊ error ကို လျစ်လျူရှု၊ ပြောင်းလဲတဲ့ variable တွေထဲ number တွေ ပေါင်းပြီး ရေးရင်းနဲ့ print ထုတ်တာပါ။ Happy path မှာ အလုပ်လုပ်ပါတယ်။ ဒါပေမဲ့ field ပျောက်တာ ဒါမှမဟုတ် မရှိတဲ့ course လို မှားနေတဲ့ line တစ်ခု ပေါ်လာတာနဲ့ program က report အလယ်မှာ crash ဖြစ်သွားတယ်၊ ဒါမှမဟုတ် line ကို တိတ်တိတ်ကျော်ပြီး ဘယ်သူမှ မသိဘဲ မှားနေတဲ့ စုစုပေါင်းကို ပေးပါတယ်။
ဒုတိယပြဿနာက စမ်းသပ်ခြင်းပါ။ Parsing၊ တွက်ချက်ခြင်းနဲ့ print ထုတ်ခြင်းတွေ ရောနှောနေတဲ့အတွက် စည်းကမ်းတစ်ခုကို စစ်ဖို့ တစ်ခုတည်းသော နည်းက print ထွက်တဲ့ output ကို မျက်စိနဲ့ ဖတ်ကြည့်တာပါ။ စည်းကမ်းတစ်ခု ပြောင်းလိုက်ရင် တခြားဘာတွေ ရွေ့သွားလဲ ဆိုတာ မပြောနိုင်တော့ပါဘူး။
fun main() {
val lines = listOf("Mya, Kotlin Basics, true", "broken line")
var total = 0
for (line in lines) {
val parts = line.split(",")
total += parts[1].trim().length // IndexOutOfBoundsException on "broken line"
println("processed $line")
}
println("total = $total")
}Slice ခွဲခြင်းက ပြဿနာနှစ်ခုလုံးကို ဖြေရှင်းတယ်
Program ကို slice သေးသေးလေးတွေ ခွဲရင် ပြဿနာနှစ်ခုလုံး ပြေလည်ပါတယ်။ Result ပြန်ပေးတဲ့ parsing slice က line တိုင်းကို အကြောင်းရင်းနဲ့ လက်ခံ ဒါမှမဟုတ် ပယ်ချရအောင် တွန်းအားပေးပါတယ်၊ ရှင်းလင်းတဲ့ typed data ကိုပဲ လက်ခံတဲ့ summary slice က text ကြောင့် crash မဖြစ်နိုင်ပါဘူး၊ report slice ကတော့ format ချရုံပဲ လုပ်ပါတယ်။ Slice တစ်ခုချင်းစီကို သီးခြားစစ်လို့ရပြီး main အဆုံးက check() တွေက သေးငယ်တဲ့ test လိုပါပဲ: line တစ်ခု ပျောက်သွားရင် ယုံနိုင်စရာကောင်းပေမဲ့ မှားနေတဲ့ report ကို print မထုတ်ဘဲ program ရပ်သွားပါတယ်။
Core concept: model၊ parse၊ classify၊ summarise၊ report
Project က ဒီလေ့ကျင့်ခန်းကနေ တကယ့် service တွေအထိ ကြီးထွားနိုင်တဲ့ pattern ကို လိုက်နာပါတယ်: model၊ parse၊ classify၊ summarise၊ report။ Slice တစ်ခုစီက အလုပ်တစ်ခုပဲ လုပ်ပြီး type ပါတဲ့ value တစ်ခုကို နောက် slice ဆီ လွှဲပေးပါတယ်။ Model slice က Level (enum)၊ Course၊ Enrollment နဲ့ Accepted နဲ့ Rejected ပါတဲ့ sealed interface Line ကို သတ်မှတ်ပါတယ်။ အဲဒီထဲမှာ text format တွေ ဒါမှမဟုတ် print ထုတ်တာတွေကို ဘာမှ မသိပါဘူး။
Parse slice က မလုံခြုံတာအားလုံးကို ပိုင်ဆိုင်ပါတယ်။ parseEnrollment က raw line တစ်ကြောင်းကိုယူပြီး Result<Enrollment> ပြန်ပေးပါတယ်။ runCatching ထဲမှာ require က field သုံးခု ရှိမရှိနဲ့ student နာမည် အလွတ်မဟုတ်ကြောင်း စစ်ပါတယ်၊ error က မသိတဲ့ course ကို အစီရင်ခံပြီး when က true ဒါမှမဟုတ် false ကိုပဲ လက်ခံတာမို့ 'maybe' က မရည်ရွယ်ဘဲ false ဖြစ်မသွားဘဲ fail ဖြစ်ပါတယ် (standard library ရဲ့ toBooleanStrict ကလည်း အလုပ်တူတူ လုပ်ပါတယ်)။ Failure တိုင်းမှာ ပြဿနာကို ရှင်းပြတဲ့ message ပါပြီး အဲဒီ message ကိုပဲ report က နောက်ပိုင်းမှာ ပြပါတယ်။
Classify slice က fold နဲ့ Result ကို sealed Line type အဖြစ် ပြောင်းပါတယ်။ ဒီအချိန်ကစပြီး ဘယ်သူမှ exception ကို မကိုင်တော့ပါဘူး: line တစ်ခုက Enrollment ပါတဲ့ Accepted ဒါမှမဟုတ် မူလ text နဲ့ အကြောင်းရင်းပါတဲ့ Rejected ပါပဲ။ filterIsInstance က list ထဲက အမျိုးအစားတစ်မျိုးကို ရွေးပြီး type မှန်တဲ့ element တွေ ပြန်ပေးလို့ accepted က cast မလိုဘဲ List<Enrollment> ဖြစ်သွားပါတယ်။
Summary slice က lesson 20 က toolbox နဲ့ စစ်စစ် data ကိုင်တွယ်တာပါ: filter က ပေးချေပြီးသား enrollment တွေကို ထားပြီး groupBy က course title အလိုက် စုစည်း၊ mapValues နဲ့ sumOf က group တစ်ခုစီကို ဝင်ငွေ ဖြစ်စေပြီး maxByOrNull က အလုပ်အများဆုံး student ကို ရှာပါတယ်။ Type ပါတဲ့ value တွေပေါ်မှာ အလုပ်လုပ်တာမို့ string parsing မပါဘဲ မှားတဲ့ text ကြောင့် ပစ်မထုတ်နိုင်ပါဘူး။ Report slice က format ချရုံပါ: padEnd က column တွေကို ညှိပြီး Int.asMmk() extension function က ငွေပမာဏ format ကို နေရာတစ်ခုတည်းမှာ ကိုင်ပါတယ်။ နောက်ဆုံးမှာ check(accepted.size + rejected.size == script.size) က invariant တစ်ခုကို ကြေညာပါတယ်: input line တစ်ကြောင်းမှ မပျောက်ရဘူး။ နောင်တစ်ချိန် ပြောင်းလဲမှုက အဲဒါကို ချိုးဖောက်ရင် ယုံနိုင်စရာကောင်းပေမဲ့ မှားနေတဲ့ report မထုတ်ဘဲ program က ကျယ်လောင်စွာ fail ဖြစ်ပါတယ်။
| Slice | Input နဲ့ output | Course ထဲက tool |
|---|---|---|
| Model | input မရှိ၊ type သတ်မှတ်ချက် | data class၊ enum class၊ sealed interface |
| Parse | String -> Result<Enrollment> | runCatching၊ require၊ error၊ when |
| Classify | Result -> Line | fold၊ sealed interface |
| Summary | List<Enrollment> -> number တွေ | filter၊ groupBy၊ mapValues၊ sumOf၊ maxByOrNull |
| Report | number တွေ -> text | extension function၊ padEnd၊ string template၊ check |
- Raw line တွေ program ထဲ ဝင်လာတယ်
- Parse: line တိုင်းကို လက်ခံ ဒါမှမဟုတ် ပယ်
- လက်ခံတဲ့ data ကိုပဲ အကျဉ်းချုပ်
- Report ပြီး မပျောက်ကြောင်း check
Report ထဲက number တစ်ခု ထူးဆန်းနေရင် ဘယ်ဘက်ကနေ စမ်းပါ: parse အရင်၊ ပြီးတော့ classify၊ နောက်ဆုံး summary ပါ။
အထွေထွေ ပုံစံ
data class Model(val field: Type)
sealed interface Line {
data class Accepted(val item: Model) : Line
data class Rejected(val raw: String, val reason: String) : Line
}
fun parse(raw: String): Result<Model> = runCatching { ... }
fun classify(raw: String): Line =
parse(raw).fold({ Line.Accepted(it) }, { Line.Rejected(raw, "...") })Model ကို အရင်ကြေညာပါ၊ parse က လုံခြုံမှုမရှိတာတွေကို Result ထဲ ဖမ်းပြီး classify က Result ကို Accepted ဒါမှမဟုတ် Rejected အဖြစ် ပြောင်းပေးပါတယ်။ Summary နဲ့ report တို့က typed value တွေကိုပဲ မြင်ရပါတယ်။
Worked example: scripted line တွေကနေ enrollment report
Script ထဲမှာ line ရှစ်ကြောင်း ပါပြီး သုံးကြောင်းက တမင်ထည့်ထားတဲ့ မှားတာတွေပါ: မှားတဲ့ paid တန်ဖိုး၊ မသိတဲ့ course နဲ့ field မပြည့်တဲ့ line ပါ။ Program က အဲဒီ line တွေကို crash မဖြစ်ဘဲ အကြောင်းရင်းနဲ့ ပြရပါမယ်။ ကျန်တဲ့ ငါးကြောင်းကနေ revenue၊ မပေးရသေးတာနဲ့ အလုပ်အများဆုံး student ကို တွက်ပါတယ်။ Run မလုပ်ခင် ဝင်ငွေ စုစုပေါင်း ဘယ်လောက်ရမလဲ ကြိုတွက်ကြည့်ပါ။
enum class Level { BEGINNER, INTERMEDIATE, ADVANCED }
data class Course(val title: String, val price: Int, val level: Level)
data class Enrollment(val student: String, val course: Course, val paid: Boolean)
sealed interface Line {
data class Accepted(val enrollment: Enrollment) : Line
data class Rejected(val raw: String, val reason: String) : Line
}
val catalog = listOf(
Course("Kotlin Basics", 30000, Level.BEGINNER),
Course("Android UI", 45000, Level.INTERMEDIATE),
Course("Backend API", 50000, Level.ADVANCED),
)
val script = listOf(
"Mya, Kotlin Basics, true",
"Zin, Backend API, true",
"Kyaw, Android UI, false",
"Mya, Backend API, true",
"Su, Kotlin Basics, maybe",
"Hla, Rust Basics, true",
"broken line",
"Aye, Kotlin Basics, true",
)
fun Int.asMmk(): String = "$this MMK"
fun parseEnrollment(raw: String): Result<Enrollment> = runCatching {
val parts = raw.split(",").map { it.trim() }
require(parts.size == 3) { "expected 3 fields but found ${parts.size}" }
val (student, title, paid) = parts
require(student.isNotEmpty()) { "student name is empty" }
val course = catalog.firstOrNull { it.title == title }
?: error("unknown course '$title'")
val isPaid = when (paid) {
"true" -> true
"false" -> false
else -> error("paid must be true or false, got '$paid'")
}
Enrollment(student, course, isPaid)
}
fun classify(raw: String): Line = parseEnrollment(raw).fold(
onSuccess = { Line.Accepted(it) },
onFailure = { Line.Rejected(raw, it.message ?: "unknown error") },
)
fun main() {
val lines = script.map(::classify)
val accepted = lines.filterIsInstance<Line.Accepted>().map { it.enrollment }
val rejected = lines.filterIsInstance<Line.Rejected>()
println("== Enrollment report ==")
val counts = "accepted: ${accepted.size}, rejected: ${rejected.size}"
println("Lines: ${lines.size}, $counts")
println("-- Revenue (paid only) --")
val revenue = accepted.filter { it.paid }
.groupBy { it.course.title }
.mapValues { (_, list) -> list.sumOf { it.course.price } }
for (course in catalog) {
println("${course.title.padEnd(14)} ${(revenue[course.title] ?: 0).asMmk()}")
}
println("Total: ${revenue.values.sum().asMmk()}")
println("-- Unpaid --")
for (e in accepted.filterNot { it.paid }) {
println("${e.student} still owes ${e.course.price.asMmk()} for ${e.course.title}")
}
println("-- Rejected --")
for (line in rejected) {
println("'${line.raw}' -> ${line.reason}")
}
val perStudent = accepted.groupBy { it.student }.mapValues { (_, list) -> list.size }
val busiest = perStudent.maxByOrNull { it.value }
println("Most active: ${busiest?.key} with ${busiest?.value} courses")
check(accepted.size + rejected.size == script.size) { "a line was lost" }
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- The model
data class Enrollment(val student: String, val course: Course, val paid: Boolean)Enrollment က student နာမည်၊ Course ကိုယ်တိုင် (နာမည်သက်သက် မဟုတ်ပါဘူး) နဲ့ paid flag ကို သိမ်းပါတယ်။ Course ကို တိုက်ရိုက်ကိုင်ထားလို့ နောက်ပိုင်းမှာ ဈေးကို lookup ပြန်လုပ်စရာ မလိုဘဲ it.course.price ရပါတယ်။
- Fail with a reason
?: error("unknown course '$title'")firstOrNull က မတွေ့ရင် null ပြန်ပေးပြီး ?: error(...) က null အတွက် IllegalStateException ကို ပစ်ပါတယ်။ error ရဲ့ type က Nothing ဖြစ်လို့ expression မှာ အဆင်ပြေပါတယ်။ runCatching က အဲဒီ exception ကို ဖမ်းလို့ message ဖြစ်တဲ့ unknown course 'Rust Basics' က report ဆီ ရောက်ပါတယ်။
- Strict input
else -> error("paid must be true or false, got '$paid'")when က paid ကို true နဲ့ false ကိုပဲ လက်ခံပြီး ကျန်တာအားလုံးကို ပယ်ပါတယ်။ ဒါကြောင့် 'maybe' က report ထဲမှာ unpaid အဖြစ် တိတ်တိတ်ကျသွားတာမျိုး မဖြစ်ဘဲ ပယ်ချခံရတဲ့ line အဖြစ် ပေါ်ပါတယ်။ ဒါက မှားနေတဲ့ ငွေစာရင်းကို ကာကွယ်တဲ့ အရေးကြီးတဲ့ စည်းကမ်းပါ။
- Result to sealed type
fun classify(raw: String): Line = parseEnrollment(raw).fold(fold က success ဖြစ်ရင် Accepted၊ failure ဖြစ်ရင် မူလ text နဲ့ message ပါတဲ့ Rejected ကို ဆောက်ပါတယ်။ Return type Line က နှစ်ခုလုံးရဲ့ ပေါင်းစပ် supertype ဖြစ်လို့ Kotlin က fold ရဲ့ ရလဒ် type ကို infer လုပ်နိုင်ပါတယ်။ ဒီနောက်မှာ exception တစ်ခုမှ မကျန်ပါဘူး။
- Typed filtering
val accepted = lines.filterIsInstance<Line.Accepted>().map { it.enrollment }filterIsInstance က Line list ထဲက Accepted တွေကိုပဲ ထားပြီး element type ကို Line.Accepted အဖြစ် ပြောင်းပေးလို့ map { it.enrollment } မှာ cast မလိုပါဘူး။ ရလဒ်က List<Enrollment> ဖြစ်ပါတယ်။
- Group, then total
.groupBy { it.course.title }ပေးချေပြီးတဲ့ enrollment တွေကိုပဲ filter လုပ်ပြီးမှ course title အလိုက် group ပါတယ်၊ ဒါကြောင့် Kyaw ရဲ့ Android UI က ငွေမပေးရသေးလို့ revenue ထဲ မပါတော့ပါဘူး။ mapValues နဲ့ sumOf က group တစ်ခုစီကို ဈေးစုစုပေါင်း ဖြစ်စေပါတယ်။ ဒီအတွက် catalog ကို loop ပတ်ရင် Android UI က 0 MMK နဲ့ ပေါ်ပါတယ်။
- A self-check
check(accepted.size + rejected.size == script.size) { "a line was lost" }Line အားလုံးက Accepted ဒါမှမဟုတ် Rejected ဖြစ်ရမယ်ဆိုတဲ့ invariant ကို check ကြေညာပါတယ်။ အမြဲမှန်နေတာမို့ ဘာမှ မထွက်ပါဘူး၊ ဒါပေမဲ့ နောက်မှ ပြောင်းလဲမှုတစ်ခုက line တစ်ခု ကျသွားစေရင် IllegalStateException နဲ့ message ကို ပြပြီး ရပ်ပါလိမ့်မယ်။
== Enrollment report ==
Lines: 8, accepted: 5, rejected: 3
-- Revenue (paid only) --
Kotlin Basics 60000 MMK
Android UI 0 MMK
Backend API 100000 MMK
Total: 160000 MMK
-- Unpaid --
Kyaw still owes 45000 MMK for Android UI
-- Rejected --
'Su, Kotlin Basics, maybe' -> paid must be true or false, got 'maybe'
'Hla, Rust Basics, true' -> unknown course 'Rust Basics'
'broken line' -> expected 3 fields but found 1
Most active: Mya with 2 coursesstdin မပါဘဲ စမ်းသပ်ခြင်း: scripted data နဲ့ check()
Input ကို list အဖြစ် program ထဲမှာ ရေးထားတာက keyboard ကို စောင့်စရာမလိုပါဘူး၊ run တိုင်း တူညီတဲ့ ရလဒ်ရတာမို့ မှားတာကို ပြန်ဖြစ်စေလို့ ရပါတယ်။ check() ကလည်း မျှော်လင့်ချက်တစ်ခုကို ကုဒ်အဖြစ် ရေးပေးပြီး မှားရင် IllegalStateException နဲ့ message ကို ပစ်ပါတယ်။ အောက်ကနမူနာက function တစ်ခုအတွက် သေးငယ်တဲ့ စစ်ဆေးချက်တွေကို ရေးပြပါတယ်: မှန်တဲ့ input၊ နည်းနည်းမှားတဲ့ input နှစ်မျိုးပါ။
fun parseQuantity(text: String): Result<Int> = runCatching {
val n = text.trim().toInt()
require(n in 1..99) { "quantity $n is outside 1..99" }
n
}
fun expectFailure(text: String, fragment: String) {
val message = parseQuantity(text).exceptionOrNull()?.message
check(message != null && fragment in message) {
"'$text' should fail with '$fragment', got $message"
}
}
fun main() {
check(parseQuantity(" 7 ").getOrNull() == 7) { "a padded number should parse" }
expectFailure("0", "outside")
expectFailure("abc", "For input string")
println("all checks passed")
}ဒီ program က all checks passed ကို print ထုတ်ပါတယ်။ စစ်ဆေးချက်တစ်ခု မှားရင် အဲဒီ message နဲ့ program ရပ်သွားပါတယ်။ ဒါက kotlin.test လို တကယ့် test framework အတွက် အစားမထိုးနိုင်ပါဘူး (နောက် lesson တစ်ခုမှာ လေ့လာပါမယ်)၊ ဒါပေမဲ့ ဘာမှ install မလုပ်ဘဲ ကိုယ့် program ကို ကိုယ်တိုင် ယုံနိုင်စရာကောင်းအောင် လုပ်ဖို့ အကောင်းဆုံး ပထမခြေလှမ်းပါ။
နောက်ပြောင်းလဲမှုက ဘယ် slice ကို ထိမလဲ
Slice ခွဲခြားထားခြင်းရဲ့ တန်ဖိုးက ပြောင်းလဲမှုတစ်ခုက နေရာတစ်ခုတည်းကို ထိတာမှာ ပေါ်လာပါတယ်။ အောက်ကဇယားက ဖြစ်နိုင်တဲ့ တောင်းဆိုချက်သုံးခုကို ပြပါတယ်။ Parse လုပ်တဲ့ code က file ကိုပဲ ဖတ်ဖတ် list ကနေပဲ ဖတ်ဖတ် မသိဘူးဆိုတာကို သတိပြုပါ၊ ဒါကြောင့် input source ပြောင်းတာက parse ကို မထိပါဘူး။
| တောင်းဆိုချက် | ပြောင်းရတဲ့ slice | မပြောင်းရတဲ့ slice |
|---|---|---|
| ADVANCED course တွေအတွက် discount ထည့်ရန် | Summary (နဲ့ Course data) | Parse၊ report format |
| List အစား file ကနေ line တွေဖတ်ရန် | Input source (parse အပြင်ဘက်) | Parse၊ summary၊ report |
| JSON report ပြရန် | Report | Parse၊ summary |
လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ ဒီပုံစံကို ဘယ်မှာ တွေ့ရလဲ
တကယ့် data အလုပ်တွေကို ဒီပုံစံနဲ့ပဲ စီစဉ်ထားပါတယ်: importer က CSV file ဒါမှမဟုတ် API ကနေ row တွေ ဖတ်၊ validator က row တစ်ခုချင်းစီကို type ပါတဲ့ record ဒါမှမဟုတ် အကြောင်းရင်းပါတဲ့ ပယ်ချမှု ဖြစ်စေ၊ aggregator က စုစုပေါင်းတွေ တွက်ပြီး reporter က အကျဉ်းချုပ်ကို ရေးတာ ဒါမှမဟုတ် dashboard ဆီ ပို့တာပါ။ Kotlin back end တွေက order import နဲ့ payment reconciliation အတွက် တူညီတဲ့ ဖွဲ့စည်းပုံကို သုံးပြီး Android app တွေကတော့ server response ကို UI state အဖြစ် ပြောင်းဖို့ သုံးပါတယ်။ ပယ်ချခံရတဲ့ line စာရင်းက အပိုမဟုတ်ပါဘူး။ Production မှာ ၈ ခုထဲက ၃ ခု ဘာကြောင့် မရောက်လာလဲ ကို ဘယ်သူမဆို ရှာတွေ့နိုင်စေတာပါ၊ လကုန် စုစုပေါင်းမှာမှ အဲဒီအကွာအဝေးကို သိတာမျိုး မဖြစ်စေပါဘူး။
တကယ့် project တွေကို ယူသွားစရာ အကျင့်နှစ်ခု ရှိပါတယ်။ Input ကိုင်တွယ်တာကို အစွန်းတွေမှာ ထားပြီး program အလယ်ပိုင်းက type ပါတဲ့ value တွေပေါ်မှာ အလုပ်လုပ်ပါစေ၊ ပြီးတော့ program က ကိုယ့်ကိုယ်ကို ရှင်းပြနိုင်အောင် လုပ်ပါ: လက်ခံတာနဲ့ ပယ်တာ အရေအတွက်ကို အကြောင်းရင်းနဲ့ ပြတာက ကုန်ကျစရိတ် အနည်းဆုံး monitoring ပါ။
အမှားများ
Project တစ်ခုမှာ ဖြစ်လေ့ရှိတဲ့ အမှားများ
Program တစ်ခုလုံးကို main ထဲ ရေးခြင်း
Parsing၊ တွက်ချက်ခြင်းနဲ့ print ထုတ်ခြင်းတွေ main တစ်ခုထဲမှာ ရောထားရင် စည်းကမ်းတစ်ခုကို သီးခြားမစစ်နိုင်ဘဲ output ကို မျက်စိနဲ့ပဲ ဖတ်ရပါတယ်။ Slice တစ်ခုစီကို type ပါတဲ့ input နဲ့ output ရှိတဲ့ function တစ်ခုအဖြစ် ခွဲပါ (parseEnrollment၊ classify၊ summary function)၊ ဒါမှ test ကနေ တိုက်ရိုက်ခေါ်လို့ရပါမယ်။
မှားတဲ့ line တွေကို တိတ်တိတ်ကျော်ခြင်း
mapNotNull { parseEnrollment(it).getOrNull() } က စမ်းချင်စရာ ကောင်းပေမဲ့ အကြောင်းရင်းတွေကို ပစ်ထုတ်လိုက်ပါတယ်။ ဒီနမူနာမှာ ပယ်ချခံရတဲ့ line သုံးကြောင်း ပျောက်သွားပြီး စုစုပေါင်းက ပုံမှန်လို ထင်ရမှာပါ။ Failure တွေကို ကိုင်ထားပြီး print ထုတ်ပါ၊ အရေအတွက် check ကိုလည်း ထည့်ပါ။
ကျယ်ဝန်းတဲ့ input ကို မှန်တယ်လို့ လက်ခံခြင်း
"maybe".toBoolean() က false ပြန်ပေးတာမို့ typo ("ture") ဒါမှမဟုတ် မမျှော်လင့်တဲ့ စာလုံးက unpaid ဖြစ်သွားပါတယ်၊ ဘာ error မှ မပေါ်ပါဘူး။ ငွေစာရင်းလို နေရာမှာ မျှော်လင့်ထားတာကိုပဲ လက်ခံပြီး ကျန်တာအားလုံးကို အကြောင်းရင်းနဲ့ ပယ်တဲ့ strict parser က ပိုလုံခြုံပါတယ်။
Best Practices
Best practices
- Slice တစ်ခုစီကို နာမည်ရှိတဲ့ function တစ်ခုအဖြစ် ပေးပြီး type ပါတဲ့ input နဲ့ output ထားပါ၊ ဒါမှ test ကနေ ခေါ်လို့ရပါမယ်။
- Scripted data ကို အပေါ်ဆုံးမှာ list တစ်ခုထဲ ထားပြီး တမင်မှားတဲ့ line တွေ ထည့်ပါ၊ ဒါမှ run တိုင်း failure path တွေကို စမ်းဖြစ်ပါမယ်။
- main ကို input line တစ်ကြောင်းမှ မပျောက်ရဘူး ဆိုတဲ့ invariant တွေအတွက် check() နဲ့ အဆုံးသတ်ပါ။
- Int.asMmk() လို format helper တွေကို နေရာတစ်ခုတည်းမှာ ထားပါ၊ ငွေကြေးပုံစံ ပြောင်းတာက စာကြောင်းတစ်ကြောင်းကိုပဲ ထိသင့်ပါတယ်။
Mini challenge
Mini challenge: အမှတ်စာရင်း report
Slice တူတူကို တခြားပြဿနာတစ်ခုမှာ သုံးကြည့်ပါ။ 'Aye:92' လို line တွေပါတဲ့ list ထဲမှာ student နာမည်နဲ့ အမှတ် ပါပါတယ်။ Line တစ်ခုချင်းစီကို Result နဲ့ နာမည်နဲ့ အမှတ်အဖြစ် parse ပါ၊ ပုံစံမှားတဲ့ line တွေနဲ့ 0..100 အပြင်က အမှတ်တွေကို ပယ်ပါ။ အမှတ်တွေကို sealed interface Grade (၈၅ နဲ့အထက် Distinction၊ ၆၀ နဲ့အထက် Pass၊ ၆၀ အောက် Fail) နဲ့ ခွဲပြီး student တစ်ယောက်စီရဲ့ အမြင့်ဆုံးအမှတ်ကို ထားပါ၊ ပယ်ချခံရတဲ့ line တွေကို အကြောင်းရင်းနဲ့၊ student တစ်ယောက်ကို တစ်ကြောင်းစီ၊ grade တစ်ခုစီရဲ့ အရေအတွက်နဲ့ ကျောင်းတန်းရဲ့ ပျမ်းမျှ (ကိန်းပြည့်) ကို print ထုတ်ပါ။
- parseScore က Result<Pair<String, Int>> ပြန်ပေးပြီး ':' မပါတာ၊ ကိန်းမဟုတ်တာနဲ့ အကွာအဝေးပြင်ပကို ပယ်ရမယ်
- sealed interface Grade မှာ data object သုံးခု (Distinction၊ Pass၊ Fail) ပါပြီး gradeOf(score) က when သုံးရမယ်
- groupBy နဲ့ mapValues သုံးပြီး student တစ်ယောက်စီရဲ့ အမြင့်ဆုံးအမှတ်ကို ထားရမယ်
- ပယ်ချခံရတဲ့ line တွေကို အကြောင်းရင်းနဲ့၊ student တစ်ယောက်ကို တစ်ကြောင်းစီ၊ grade တစ်ခုစီရဲ့ အရေအတွက်နဲ့ ပျမ်းမျှကို print ထုတ်ရမယ်
// TODO: sealed interface Grade with data objects Distinction, Pass and Fail
// TODO: 85+ is Distinction, 60+ is Pass, anything lower is Fail
fun gradeOf(score: Int): Grade {
TODO()
}
// TODO: an exhaustive when over Grade that returns a lower-case label, with no else
fun label(grade: Grade): String {
TODO()
}
// TODO: split on ':', require exactly two parts and a score in 0..100
fun parseScore(line: String): Result<Pair<String, Int>> {
TODO()
}
fun main() {
val input = listOf(
"Aye:92", "Bo:58", "Chit:abc", "Dee:105", "Eain:74", "Bo:81", "Fay", "Gus:40",
)
// TODO: parse every line and print each failure as: skipped '<line>': <reason>
// TODO: keep each student's best score and print: <name> best=<score> <label>
// TODO: count the students per label and print the map
// TODO: print the class average as a whole number
}ရလဒ်: ပယ်ချခံရတဲ့ line သုံးကြောင်း (Chit၊ Dee နဲ့ Fay) ကို အကြောင်းရင်းနဲ့ ပြပြီးနောက် Aye best=92 distinction၊ Bo best=81 pass၊ Eain best=74 pass၊ Gus best=40 fail ကို print ထုတ်ပါတယ်။ အဆုံးမှာ {distinction=1, pass=2, fail=1} နဲ့ class average = 71 ပါ။
အရိပ်အမြွက် ကြည့်ရန်
Parse လုပ်ပြီးသား (line, result) အတွဲတွေကို ကိုင်ထားပြီး failure ကို result.onFailure { ... } နဲ့ ပြပါ၊ အောင်မြင်တာတွေကို mapNotNull { (_, result) -> result.getOrNull() } နဲ့ ယူပါ။ အမြင့်ဆုံးအမှတ်အတွက် scores.groupBy { it.first } ကို mapValues နဲ့ ဆက်ပြီး pairs.maxOf { it.second } သုံးပါ။ ပျမ်းမျှက values.sum() / size ပါ။
အဖြေ ကြည့်ရန်
sealed interface Grade {
data object Distinction : Grade
data object Pass : Grade
data object Fail : Grade
}
fun gradeOf(score: Int): Grade = when {
score >= 85 -> Grade.Distinction
score >= 60 -> Grade.Pass
else -> Grade.Fail
}
fun label(grade: Grade): String = when (grade) {
Grade.Distinction -> "distinction"
Grade.Pass -> "pass"
Grade.Fail -> "fail"
}
fun parseScore(line: String): Result<Pair<String, Int>> = runCatching {
val parts = line.split(":").map { it.trim() }
require(parts.size == 2) { "expected name:score" }
val score = parts[1].toInt()
require(score in 0..100) { "score $score is outside 0..100" }
parts[0] to score
}
fun main() {
val input = listOf(
"Aye:92", "Bo:58", "Chit:abc", "Dee:105", "Eain:74", "Bo:81", "Fay", "Gus:40",
)
val parsed = input.map { it to parseScore(it) }
for ((raw, result) in parsed) {
result.onFailure { println("skipped '$raw': ${it.message}") }
}
val scores = parsed.mapNotNull { (_, result) -> result.getOrNull() }
val best = scores.groupBy { it.first }
.mapValues { (_, pairs) -> pairs.maxOf { it.second } }
for ((name, score) in best) {
println("$name best=$score ${label(gradeOf(score))}")
}
val perGrade = best.values.groupBy { label(gradeOf(it)) }
.mapValues { (_, list) -> list.size }
println(perGrade)
println("class average = ${best.values.sum() / best.size}")
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Program ကို model၊ parse၊ classify၊ summarise နဲ့ report slice တွေ ခွဲပြီး type ပါတဲ့ value တစ်ခုချင်းကို နောက် slice ဆီ လွှဲပေးပါ။
- Parse က မလုံခြုံတာအားလုံးကို Result ထဲ ဖမ်းပြီး classify က Accepted နဲ့ Rejected sealed type အဖြစ် ပြောင်းလို့ ပယ်ချမှုတိုင်းမှာ အကြောင်းရင်း ပါပါတယ်။
- Summary က filter၊ groupBy၊ mapValues နဲ့ sumOf ကို typed data ပေါ်မှာ သုံးပြီး parsing မပါလို့ text ကြောင့် မ crash နိုင်ပါဘူး။
- Scripted data နဲ့ check() invariant တွေက stdin မလိုဘဲ program ကို ထပ်ခါထပ်ခါ စစ်လို့ရစေပြီး ပျောက်သွားတဲ့ line ကို မထင်ရှားဘဲ မကျန်စေပါဘူး။
Kotlin language guide — Kotlin