Improve samples for partition: add destructuring
This commit is contained in:
@@ -150,8 +150,9 @@ class Arrays {
|
||||
@Sample
|
||||
fun partitionArrayOfPrimitives() {
|
||||
val array = intArrayOf(1, 2, 3, 4, 5)
|
||||
val partition = array.partition { it % 2 == 0 }
|
||||
assertPrints(partition, "([2, 4], [1, 3, 5])")
|
||||
val (even, odd) = array.partition { it % 2 == 0 }
|
||||
assertPrints(even, "[2, 4]")
|
||||
assertPrints(odd, "[1, 3, 5]")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -231,9 +231,16 @@ class Sequences {
|
||||
|
||||
@Sample
|
||||
fun partition() {
|
||||
val sequence = sequenceOf(1, 2, 3, 4, 5)
|
||||
val result = sequence.partition { it % 2 == 0 }
|
||||
assertPrints(result, "([2, 4], [1, 3, 5])")
|
||||
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 }
|
||||
|
||||
assertPrints(even, "[0, 2, 8, 34]")
|
||||
assertPrints(odd, "[1, 1, 3, 5, 13, 21]")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -172,9 +172,10 @@ class Strings {
|
||||
|
||||
@Sample
|
||||
fun partition() {
|
||||
val string = "Hello"
|
||||
val result = string.partition { it == 'l' }
|
||||
assertPrints(result, "(ll, Heo)")
|
||||
fun isVowel(c: Char) = "aeuio".contains(c, ignoreCase = true)
|
||||
val string = "Discussion"
|
||||
val result = string.partition(::isVowel)
|
||||
assertPrints(result, "(iuio, Dscssn)")
|
||||
}
|
||||
|
||||
@Sample
|
||||
|
||||
Reference in New Issue
Block a user