rocketraman
10/18/2021, 6:31 PMpartition
only supports two outputs. What about adding something like this to the stdlib?
/**
* Similar to the stdlib partition method, but supports an arbitrary number of output lists. The number
* of outputs must be known in advance.
*/
@Throws(IndexOutOfBoundsException::class)
fun <T> List<T>.partitionInto(size: Int, predicate: (T) -> Int): List<List<T>> {
val lists = listOf(*(0.until(size).map { mutableListOf<T>() }.toTypedArray()))
for (element in this) {
val index = predicate(element)
lists[index].add(element)
}
return lists
}
mbonnin
10/18/2021, 6:34 PMlist.groupBy { predicate(it) }.values
ephemient
10/18/2021, 6:36 PMpredicate
to keySelector
😉mbonnin
10/18/2021, 6:36 PMgroupBy
wasdoing the jobrocketraman
10/18/2021, 6:36 PMmbonnin
10/18/2021, 6:37 PMrocketraman
10/18/2021, 6:37 PMephemient
10/18/2021, 6:37 PMresult[0]
, result[1]
etc. will give you the correct lists regardlessrocketraman
10/18/2021, 6:38 PMMap<Int, List<T>>
, but that isn't "partition" then 🙂ephemient
10/18/2021, 6:39 PMval map = groupBy { .. }
List(size) { map[it].orEmpty() }
mbonnin
10/18/2021, 6:39 PMval (even, odd) = list.partition { it%2 == 0 }
With multiple buckets, destructuring isn't really possible so using groupBy
is usually good enoughrocketraman
10/18/2021, 6:40 PMrocketraman
10/18/2021, 6:40 PMmbonnin
10/18/2021, 6:40 PMrocketraman
10/18/2021, 6:40 PMrocketraman
10/18/2021, 6:40 PMephemient
10/18/2021, 6:41 PMmbonnin
10/18/2021, 6:41 PMephemient
10/18/2021, 6:43 PMval map = groupBy { ...toString() }
val `0` by map
val `1` by map
(yes, this actually works, but don't)rocketraman
10/18/2021, 6:43 PMephemient
10/18/2021, 6:44 PMgroupBy
with some follow-up transform is finerocketraman
10/18/2021, 6:45 PMpartiionInto
in my project locally, which I think is clearer than groupBy + transform.rocketraman
10/18/2021, 6:46 PMval (foo, bar, baz) = l.partitionInto(3) { ... }
vs
val map = groupBy { .. }
val (foo, bar, baz) = List(size) { map[it].orEmpty() }
ephemient
10/18/2021, 6:46 PMrocketraman
10/18/2021, 6:49 PMwithIndex
. Though using withIndex
to do a combined drop+take is about as complex as the groupBy + transform above.jimn
10/18/2021, 8:16 PMval lists=Array(size){mutableListOf<T>()}
😀rocketraman
10/18/2021, 8:19 PMval lists = List(size) { mutableListOf<T>() }
better, but that needs an extra .map { it.toList() }
at the return.rocketraman
10/18/2021, 8:20 PMephemient
10/18/2021, 8:24 PMval a: List<List<Any>> = MutableList(10) { mutableListOf() }
works just fine, as does val a: List<List<Any>> = Array<MutableList<Any>>(10) { mutableListOf() }.asList()
rocketraman
10/18/2021, 8:25 PMjimn
10/18/2021, 8:39 PMmyList.fold(List(predicate.size){mutableListOf()}){a,it-> a.apply{this[predicate(it)]+=it}}
rocketraman
10/18/2021, 8:40 PMpartition
as possible. That is:
val first = ArrayList<T>()
val second = ArrayList<T>()
for (element in this) {
if (predicate(element)) {
first.add(element)
} else {
second.add(element)
}
}
return Pair(first, second)
jimn
10/18/2021, 8:41 PMrocketraman
10/18/2021, 8:42 PMindexSelector
jimn
10/18/2021, 8:47 PM