-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChainedOperations.kt
69 lines (59 loc) · 1.81 KB
/
ChainedOperations.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package org.athenian
import java.io.File
fun main() {
evenNumbers()
.take(10)
.filter { it <= 8 }
.average()
.also { println("Average of even numbers <= 8: $it") }
evenNumbers()
.take(10)
.filter { it <= 8 }
.map { it * it }
.average()
.also { println("Average of even numbers <= 8 squared: $it") }
evenNumbers()
.take(5)
.plus(oddNumbers().take(5))
.sorted()
.toList()
.also { println("All numbers <= 10: $it") }
(0..6)
.flatMap {
if (it % 2 == 0)
List(it) { v -> it }
else
emptyList()
}
.also { println("Flatmap numbers: $it") }
(1..10)
.flatMap { (1..(it * 2)).map { i -> it } }
//.also { println(it) }
.groupBy { it }
//.also { println(it) }
.map { (i, v) -> i to v.size }
.also { println("Mapped flatmap numbers: $it") }
File("data/words")
.bufferedReader()
.lineSequence()
.filter { it.startsWith("pa") }
.groupBy { it.length }
.map { (k, v) -> k to v.size }
.also { println("Dict words: $it") }
val palindromes =
File("data/words")
.bufferedReader()
.lineSequence()
.filter { it.length > 1 }
.filter { it == it.reversed() }
.toList()
println("${palindromes.size} palindromes: $palindromes")
palindromes
.groupBy { it.length }
.maxBy { it.key }
.also { println("Longest palindromes with length of ${it.key}: ${it.value}") }
palindromes
.groupBy { it.length }
.maxBy { it.value.size }
.also { println("Greatest # (${it.value.size}) of palindromes with length of ${it.key}: ${it.value}") }
}