Skip to content

Commit c117cef

Browse files
committed
Add SequencesAndYields.kt
1 parent df18808 commit c117cef

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
The repo was motivated by [this post](https://medium.com/@desaismital/declarative-pipelines-in-kotlin-b9e18e77f2c5).
44

5+
* [Sequences and Yields](src/main/kotlin/org/athenian/SequencesAndYields.kt)
56
* [Collection Operations](src/main/kotlin/org/athenian/CollectionOperations.kt)
67
* [Chained Operations](src/main/kotlin/org/athenian/ChainedOperations.kt)
78
* [Easy vs Lazy Ordering](src/main/kotlin/org/athenian/EvaluationOrdering.kt)

src/main/kotlin/org/athenian/CollectionOperations.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ fun evenNumbers(max: Int): Sequence<Int> {
1010
}
1111

1212
fun main() {
13-
print("Even numbers <= 10: ")
14-
for (i in evenNumbers(10))
15-
print("$i ")
16-
println()
17-
1813
print("Even numbers <= 10: ")
1914
evenNumbers(10).forEach { print("$it ") }
2015
println()
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.athenian
2+
3+
fun main() {
4+
val numbers =
5+
sequence {
6+
println("one")
7+
yield(1)
8+
9+
println("two")
10+
yield(2)
11+
12+
println("three")
13+
yield(3)
14+
15+
println("Sequence finished...")
16+
}
17+
18+
for (n in numbers)
19+
println("number = $n")
20+
println("Done...")
21+
22+
print("Odd numbers <= 10: ")
23+
for (i in oddNumbers(10))
24+
print("$i ")
25+
println()
26+
27+
print("Odd numbers <= 20: ")
28+
for (i in oddNumbers(20))
29+
print("$i ")
30+
println()
31+
}
32+
33+
fun oddNumbers(max: Int): Sequence<Int> {
34+
return sequence {
35+
for (i in 0..max) {
36+
if (i % 2 != 0)
37+
yield(i)
38+
}
39+
}
40+
}
41+

0 commit comments

Comments
 (0)