-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollectionOperations.kt
43 lines (34 loc) · 1.5 KB
/
CollectionOperations.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package org.athenian
fun evenNumbers() =
sequence {
for (i in 0..Int.MAX_VALUE)
if (i % 2 == 0)
yield(i)
}
fun main() {
val everyOther =
doReMi()
.mapIndexed { i, note -> if (i % 2 == 0) note else "" }
.filter { it.isNotEmpty() }
.toList()
println("Every other: ${everyOther.toList()}")
println("Even numbers <= 10: ${evenNumbers().take(10).toList()}")
println("Even numbers <= 10: (${evenNumbers().take(10).joinToString(", ")})")
val reversedOdds = oddNumbers().take(11).toList().reversed().joinToString(", ")
println("Odd numbers <= 11 reversed: $reversedOdds")
evenNumbers()
.take(6)
.map { it * it }
.zip(evenNumbers().take(6).map { it * it * it })
.joinToString(", ")
.also { println("Zipped squares and cubes: $it") }
println("Min even number <= 10: ${evenNumbers().take(10).min()}")
println("Max even number <= 10: ${evenNumbers().take(10).max()}")
println("Count even numbers <= 40: ${evenNumbers().take(40).count()}")
println("Average of even numbers <= 30: ${evenNumbers().take(30).average()}")
println("First even number <= 20: ${evenNumbers().take(20).first()}")
println("Last even number <= 20: ${evenNumbers().take(20).last()}")
// Boolean operations
println("Any even numbers <= 10 <= 6: ${evenNumbers().take(10).any { it <= 6 }}")
println("All even numbers <= 10 <= 6: ${evenNumbers().take(10).all { it <= 6 }}")
}