How to partition and typecast a List in Kotlin

2019-07-11 04:02发布

问题:

In Kotlin I can:

val (specificMembers, regularMembers) = members.partition {it is SpecificMember}

However to my knowledge I can not do something like:

val (specificMembers as List<SpecificMember>, regularMembers) = members.partition {it is SpecificMember}

My question would be - is there's an idiomatic way to partition iterable by class and typecast it those partitioned parts if needed.

回答1:

The partition function will return a Pair<List<T>, List<T>> with T being the generic type of your Iterable. You can transform the partitioned values again using e.g. let:

val (specificMembers, regularMembers) = lists
    .partition { it is SpecificMember }
    .let { Pair(it.first as List<SpecificMember>, it.second) }


回答2:

If you require that functionality more often, you may just reimplement the actual partition according to your needs, e.g.:

inline fun <reified U : T, T> Iterable<T>.partitionByType(): Pair<List<U>, List<T>> {
  val first = ArrayList<U>()
  val second = ArrayList<T>()
  for (element in this) {
    if (element is U) first.add(element)
    else second.add(element)
  }
  return Pair(first, second)
}

with a usage similar as to follows:

val (specificMembers, regularMembers) = members.partitionByType<SpecificMember, Member>()
// where specificMembers : List<SpecificMember>
// and regularMembers : List<Member> for this example

Note that this way you can also set the second type to a more generic one. I leave that up to you whether this makes sense. At least this way an unchecked cast isn't necessary.

The alternative is also shown by Simon with the let-usage. You can also directly cast the result of partition (without let and another Pair) to whatever fits, e.g.:

val (specificMembers, regularMembers) = members.partition {it is SpecificMember} as Pair<List<SpecificMember>, List<Member>>