Skip to content

Commit

Permalink
Improve samples for partition: add destructuring
Browse files Browse the repository at this point in the history
(cherry picked from commit 606b4db)
  • Loading branch information
ilya-g committed Apr 10, 2020
1 parent ed3479b commit 5fd6818
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
5 changes: 3 additions & 2 deletions libraries/stdlib/samples/test/samples/collections/arrays.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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]")
}
}

Expand Down
13 changes: 10 additions & 3 deletions libraries/stdlib/samples/test/samples/collections/sequences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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]")
}
}

Expand Down
7 changes: 4 additions & 3 deletions libraries/stdlib/samples/test/samples/text/strings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5fd6818

Please sign in to comment.