Ray Rahke
03/06/2024, 4:40 PMval input = readln()
when input.startsWith() {
"y" -> {...}
"n" -> {...}
"help" -> {...}
"attack" -> {...}
"teleport" -> {...}
}
I could obviously do the following:
when {
input.startsWith("y") -> {...}
input.startsWith("n") -> {...}
input.startsWith("help") -> {...}
input.startsWith("attack") -> {...}
input.startsWith("teleport") -> {...}
}
but that is terribly repetitiveRay Rahke
03/06/2024, 4:50 PMwhen
over. This sucks because it requires writing each string twice. Like, for 5 string options, I would have to write 10 references.Ray Rahke
03/06/2024, 4:52 PMwhen
questions I need to ask nested.)Klitos Kyriacou
03/06/2024, 4:55 PMwhen (input.substringBefore(' ')) {
"y" -> {...}
"n" -> {...}
"help" -> {...}
"attack" -> {...}
"teleport" -> {...}
}
Ray Rahke
03/06/2024, 4:57 PMRay Rahke
03/06/2024, 4:57 PMRay Rahke
03/06/2024, 4:57 PMRay Rahke
03/06/2024, 4:57 PMRay Rahke
03/06/2024, 4:59 PMLuke
03/06/2024, 4:59 PMRay Rahke
03/06/2024, 5:01 PMmagic create commandbox "teleport base"
or
magic create commandbox "magic create diamondsword && give self diamondsword"
Ray Rahke
03/06/2024, 5:01 PMRay Rahke
03/06/2024, 5:01 PMLuke
03/06/2024, 5:01 PMwhen (prefix) { ... }
so there is not much gainRay Rahke
03/06/2024, 5:01 PMYoussef Shoaib [MOD]
03/06/2024, 5:02 PM@JvmInline value class Prefix(val prefix: String)
fun startingWith(prefix: String) = Prefix(prefix)
operator fun Prefix.contains(string: String) = string.startsWith(prefix)
when(input) {
in startingWith("y") -> ...
in startingWith("n") -> ...
}
Ray Rahke
03/06/2024, 5:58 PMRay Rahke
03/06/2024, 5:58 PMattack john
attack john
Ray Rahke
03/06/2024, 5:58 PMRay Rahke
03/06/2024, 5:59 PMKlitos Kyriacou
03/06/2024, 6:00 PMsubstringBefore(' ')
will stop at the first ' '
character, so both "attack john".substringBefore(' ')
and "attack john".substringBefore(' ')
will return "attack"
.Ray Rahke
03/06/2024, 6:01 PMKlitos Kyriacou
03/06/2024, 6:03 PM"y".substringBefore(' ')
will return "y"
.Luke
03/06/2024, 6:51 PM.split(" +".toRegex())
Luke
03/06/2024, 6:52 PMmagic create commandbox "teleport base"
, the quotes will end up in different stringsShawn
03/06/2024, 7:32 PMephemient
03/06/2024, 10:07 PM@JvmInline
value class StringsStartingWith(val prefix: String) {
operator fun contains(string: String): Boolean = prefix in string
}
val input = readln()
when (input) {
in StringsStartingWith("y") -> {...}
in StringsStartingWith("n") -> {...}
in StringsStartingWith("help") -> {...}
in StringsStartingWith("attack") -> {...}
in StringsStartingWith("teleport") -> {...}
}
but I 100% echo what Shawn says. the way to do this is to tokenize before parsing. it's more correct, more extensible, and easier in the end.Jacob
03/07/2024, 6:44 AMval cases = listOf(
"y" to { println("1") },
"n" to { println("2") },
"help" to { println("3") },
"attack" to { println("4") },
"teleport" to { println("5") },
)
val input = "helppp"
cases.firstOrNull { input.startsWith(it.first) }?.second?.invoke() ?: println("we hit the else")
Ray Rahke
03/08/2024, 5:14 PMShawn
03/09/2024, 3:52 AMShawn
03/09/2024, 3:53 AMy
and n
as aliases for yes
and no
, respectively, I would recommend just adding those tokens as commands that also map to yes
and no
, rather than trying to do partial string matching that excludes other possibilities