Michael Böiers
12/29/2022, 12:30 PMPaul Woitaschek
12/29/2022, 1:10 PMMichael Böiers
12/29/2022, 1:13 PMMichael Böiers
12/29/2022, 1:18 PMMichael Böiers
12/29/2022, 1:21 PMList<T>.split(sep: T): List<List<T>>
and/or
List<T>.split(sep: (T) -> Boolean): List<List<T>>
Then I could do lines.split("") or lines.split { it.isBlank() }.Michael Böiers
12/29/2022, 1:23 PMephemient
12/29/2022, 1:50 PMinline fun <T> Iterable<T>.splitOnce(predicate: (T) -> Boolean): Pair<List<T>, List<T>> {
val iterator = iterator()
return Iterable { iterator }.takeWhile { !predicate(it) } to Iterable { iterator }.toList()
}
although I don't think the collections extensions are guaranteed to consume the iterator just once... no good reason for them to behave otherwise thoughLuke Armitage
12/29/2022, 5:36 PMsplitOnce something you can then use in a declaration like the following?
val foo = listOf(1,2,3,4)
sis thatLuke Armitage
12/29/2022, 5:38 PMsplitOnce something you can then use in a declaration such as the following?
val foo = listOf(1,2,3,4,5)
val (firstTwo, rest) = foo.splitOnce { it.sign = 0 }
// firstTwo == listOf(1,2)
// rest = listOf(3,4,5)
Or how would you use it?ephemient
12/29/2022, 5:46 PMval (a, b) = listOf(1, 2, 3, 4, 5).splitOnce { it % 2 == 0 }
a == listOf(1)
b == listOf(3, 4, 5)ephemient
12/29/2022, 5:46 PM.splitOnce(String::isEmpty) would have been usefulephemient
12/29/2022, 5:47 PMLuke Armitage
12/29/2022, 5:58 PMString.split (which now is clear from the name) than a version of takeWhile that destructures to the matches and the remainder. thanks for the help!ephemient
12/29/2022, 6:08 PMspan in Haskell and is effectively equivalent to `takeWhile`+`dropWhile`. there's no one-line alternative in Kotlin but I think it could may useful as wellelizarov
01/09/2023, 8:56 AMsplit to my own AoC utils (called it parts) after seeing it for a second time, but I cannot find enough motivation to even ask for adding it to the standard library. Why would you ever need it in any kind of business context?