partition
所在位置是kotlin.sequences.partition
,其相关用法介绍如下。
用法:
将原始序列拆分为一对列表,其中 first
list 包含 predicate 产生 true
的元素,而 second
list 包含 predicate 产生 false
的元素。
操作是 terminal
。
例子:
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
fun fibonacci(): Sequence<Int> {
// fibonacci terms
// 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, ...
return generateSequence(Pair(0, 1), { Pair(it.second, it.first + it.second) }).map { it.first }
}
val (even, odd) = fibonacci().take(10).partition { it % 2 == 0 }
println(even) // [0, 2, 8, 34]
println(odd) // [1, 1, 3, 5, 13, 21]
//sampleEnd
}
输出:
[0, 2, 8, 34] [1, 1, 3, 5, 13, 21]
相关用法
- Kotlin padEnd用法及代码示例
- Kotlin padStart用法及代码示例
- Kotlin plus用法及代码示例
- Kotlin associateBy用法及代码示例
- Kotlin all用法及代码示例
- Kotlin map用法及代码示例
- Kotlin filterNot用法及代码示例
- Kotlin reduceRight用法及代码示例
- Kotlin Random.Default用法及代码示例
- Kotlin Byte.inc用法及代码示例
- Kotlin getValue用法及代码示例
- Kotlin Double.dec用法及代码示例
- Kotlin windowedSequence用法及代码示例
- Kotlin contentToString用法及代码示例
- Kotlin groupByTo用法及代码示例
- Kotlin commonPrefixWith用法及代码示例
- Kotlin MatchResult.Destructured用法及代码示例
- Kotlin Delegates.notNull用法及代码示例
- Kotlin ifBlank用法及代码示例
- Kotlin filterNotTo用法及代码示例
- Kotlin getOrPut用法及代码示例
- Kotlin Triple.<init>用法及代码示例
- Kotlin Duration.toString用法及代码示例
- Kotlin windowed用法及代码示例
- Kotlin indexOf用法及代码示例
注:本文由纯净天空筛选整理自kotlinlang.org大神的英文原创作品 kotlin.sequences.partition。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。