mikehearn
04/14/2016, 10:53 AMmikehearn
04/14/2016, 10:53 AMmikehearn
04/14/2016, 10:53 AMfellshard
04/14/2016, 11:02 AMfellshard
04/14/2016, 11:10 AMmathiasbn
04/14/2016, 12:23 PMmathiasbn
04/14/2016, 12:28 PMbamboo
04/14/2016, 1:46 PMkotlin
/**
* Intersperses element [separator] between the elements of the list.
*/
fun <E> List<E>.intersperse(separator: E): List<E> {
if (this.size < 2) {
return this
}
val result = ArrayList<E>(this.size * 2 - 1)
this.forEachIndexed { i, e ->
if (i > 0) result.add(separator)
result.add(e)
}
return result
}
kirillrakhman
04/14/2016, 1:50 PMjoinTo
but that only works for Appendable
kirillrakhman
04/14/2016, 1:52 PMlistOf("a", "b", "c").flatMap { listOf(it, "|") }.dropLast(1)
works but is not very efficientbamboo
04/14/2016, 6:14 PMkevinmost
04/14/2016, 11:49 PM/**
* Splits the receiver iterable into sublists, where a new sublist is started with every element
* that returns true when [splitPredicate] is tested against it.
*/
fun <T> Iterable<T>.splitWhen(
splitPredicate: (index: Int, element: T, currentSubList: List<T>) -> Boolean
): List<List<T>> {
val result = mutableListOf<MutableList<T>>()
forEachIndexed { index, element ->
if (index == 0 || splitPredicate(index, element, result.last())) {
result.add(mutableListOf<T>())
}
result.last().add(element)
}
return result
}
kevinmost
04/14/2016, 11:50 PMilya.gorbunov
04/15/2016, 4:09 PMwindow
function (https://youtrack.jetbrains.com/issue/KT-10021) for usages covered by splitEvery
in your proposal.kevinmost
04/15/2016, 4:11 PMkirillrakhman
04/18/2016, 12:02 PMMutableList
that combines firstOrNull
and remove
. Would you find something like that useful: val removed : T? = list.removeFirst { it.predicate() }
kirillrakhman
04/18/2016, 12:53 PMkmruiz
04/18/2016, 12:55 PMkirillrakhman
04/18/2016, 12:56 PMkirillrakhman
04/18/2016, 12:56 PMkmruiz
04/18/2016, 1:04 PMkirillrakhman
04/18/2016, 1:12 PMMutableList
, too. It's not a lot less efficient, since indexOfFirst
needs to allocate an IntRange
, tookmruiz
04/18/2016, 1:15 PMkmruiz
04/18/2016, 1:15 PMkirillrakhman
04/18/2016, 1:16 PMRandomAccess
kirillrakhman
04/18/2016, 1:16 PMkmruiz
04/18/2016, 1:16 PMkirillrakhman
04/18/2016, 1:16 PMkirillrakhman
04/18/2016, 1:17 PMkmruiz
04/18/2016, 1:18 PMpublic fun <T> mutableListOf(vararg elements: T): MutableList<T>
returns an ArrayList, but because I'm looking at the source code