Docs and samples for 'chunked' function

This commit is contained in:
Ilya Gorbunov
2017-05-16 23:53:46 +03:00
parent e418f61f0c
commit b2f2e3537b
12 changed files with 391 additions and 2 deletions
@@ -71,6 +71,15 @@ class Collections {
assertPrints(moreFrequencies, "{o=1, t=4, f=2, s=2, e=2, n=1}")
}
@Sample
fun chunked() {
val words = "one two three four five six seven eight nine ten".split(' ')
val chunks = words.chunked(3)
assertPrints(chunks, "[[one, two, three], [four, five, six], [seven, eight, nine], [ten]]")
}
@Sample
fun pairwise() {
val letters = ('a'..'f').toList()
@@ -50,5 +50,25 @@ class Strings {
assertPrints(withoutMargin2, "XYZ\nfoo\nbar")
}
@Sample
fun chunkedTransform() {
val codonTable = mapOf("ATT" to "Isoleucine", "CAA" to "Glutamine", "CGC" to "Arginine", "GGC" to "Glycine")
val dnaFragment = "ATTCGCGGCCGCCAA"
val proteins = dnaFragment.chunked(3) { codon: CharSequence -> codonTable[codon.toString()] ?: error("Unknown codon") }
assertPrints(proteins, "[Isoleucine, Arginine, Glycine, Arginine, Glutamine]")
}
@Sample
fun chunkedTransformToSequence() {
val codonTable = mapOf("ATT" to "Isoleucine", "CAA" to "Glutamine", "CGC" to "Arginine", "GGC" to "Glycine")
val dnaFragment = "ATTCGCGGCCGCCAACGG"
val proteins = dnaFragment.chunkedSequence(3) { codon: CharSequence -> codonTable[codon.toString()] ?: error("Unknown codon") }
// sequence is evaluated lazily, so that unknown codon is not reached
assertPrints(proteins.take(5).toList(), "[Isoleucine, Arginine, Glycine, Arginine, Glutamine]")
}
}