David Kubecka
12/02/2024, 2:44 PM"".split(",").size == 1
trap? ๐Joffrey
12/02/2024, 2:45 PMVampire
12/02/2024, 2:46 PMDavid Kubecka
12/02/2024, 2:47 PMVampire
12/02/2024, 2:48 PM"foo,".split(",").size == 2
Joffrey
12/02/2024, 2:48 PM,
separator. Every element around ,
separators must be accounted for, empty strings are not special. When there is no separator, I wouldn't expect "a"
to behave differently from ""
, I get one element which is the whole string.Joffrey
12/02/2024, 2:50 PMVampire
12/02/2024, 2:50 PM"foo,".split(",")
results in "foo"
and ""
.
"foo,,bar".split(",")
results in "foo"
, ""
, and "bar"
.
",".split(",")
results in ""
and ""
.
"".split(",")
results in ""
.David Kubecka
12/02/2024, 2:51 PMVampire
12/02/2024, 2:51 PMJoffrey
12/02/2024, 2:51 PMJoffrey
12/02/2024, 2:52 PM(s1 + "," + s2).split(",")
, or if you split s1
and s2
separatelyKlitos Kyriacou
12/02/2024, 2:53 PMsplit
behaves differently.Vampire
12/02/2024, 2:54 PMVampire
12/02/2024, 2:54 PMKlitos Kyriacou
12/02/2024, 2:56 PM"one,".split(",")
returns one element.Vampire
12/02/2024, 2:56 PM-1
, sorry, forgot about that.David Kubecka
12/02/2024, 2:57 PMjoinToString
๐
listOf<Int>().joinToString(",").split(",").size == 1
David Kubecka
12/02/2024, 2:57 PMJoffrey
12/02/2024, 2:59 PMjoinToString()
is not injective (joining an empty list yields the same as joining a list of one element mapped to the empty string), so there is no way split()
could be its inverseVampire
12/02/2024, 2:59 PMlistOf<String>().joinToString(",").split(",") == listOf("").joinToString(",").split(",")
Vampire
12/02/2024, 3:00 PMDavid Kubecka
12/02/2024, 3:03 PMVampire
12/02/2024, 3:03 PMjoinToString
is operating on list of strings.
If you don't give an explicit transform to make string out of it, it calls toString()
Vampire
12/02/2024, 3:04 PMsplit
is not a list of ints, but a list of stringsVampire
12/02/2024, 3:04 PMDavid Kubecka
12/02/2024, 3:10 PMfun intListToCommaString(list: List<Int>) = list.joinToString(",")
fun commaStringToIntList(string: String) = string.split(",").map { it.toInt() }
I would think that those two functions are inverse. And indeed they are for every valid input except for the empty list case, which is kind of strange IMO. Or at least not obvious.David Kubecka
12/02/2024, 3:14 PM