Skip to content

Commit 5b4c2d0

Browse files
committed
Add EveryNthSequence
1 parent f815786 commit 5b4c2d0

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ The repo was motivated by [this post](https://medium.com/@desaismital/declarativ
55
* [Continuations, Sequences and Yields](src/main/kotlin/org/athenian/ContinuationsSequencesAndYields.kt)
66
* [Collection Operations](src/main/kotlin/org/athenian/CollectionOperations.kt)
77
* [Chained Operations](src/main/kotlin/org/athenian/ChainedOperations.kt)
8-
* [Sequnce Ordering](src/main/kotlin/org/athenian/SequenceOrdering.kt)
8+
* [Sequence Ordering](src/main/kotlin/org/athenian/SequenceOrdering.kt)
99
* [Eager vs Lazy Ordering](src/main/kotlin/org/athenian/EvaluationOrdering.kt)
1010
* [More Eager vs Lazy Ordering](src/main/kotlin/org/athenian/MoreEvaluationOrdering.kt)
1111
* [Expensive Calculations](src/main/kotlin/org/athenian/ExpensiveCalculations.kt)
1212
* [Processing Data](src/main/kotlin/org/athenian/LogReader.kt)
13+
* [Every Other Sequence Action](src/main/kotlin/org/athenian/EveryOtherSequence.kt)
14+
* [Every Nth Sequence Action](src/main/kotlin/org/athenian/EveryNthSequence.kt)
1315

1416
A good post on Sequences is [here](https://blog.kotlin-academy.com/effective-kotlin-use-sequence-for-bigger-collections-with-more-than-one-processing-step-649a15bb4bf).
17+
18+
A good post on creating Sequence Acitons is [here](https://typealias.com/guides/inside-kotlin-sequences/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.athenian
2+
3+
fun <T> Sequence<T>.everyNth(inc: Int) = EveryNthSequence(inc, this)
4+
5+
class EveryNthSequence<T>(private val inc: Int, private val underlyingSequence: Sequence<T>) : Sequence<T> {
6+
override fun iterator() =
7+
object : Iterator<T> {
8+
val iterator = underlyingSequence.iterator()
9+
10+
override fun hasNext() = iterator.hasNext()
11+
12+
override fun next(): T {
13+
val item = iterator.next()
14+
(1..inc - 1).forEach {
15+
if (iterator.hasNext())
16+
iterator.next()
17+
}
18+
return item
19+
}
20+
}
21+
}
22+
23+
fun main() {
24+
(0..50)
25+
.asSequence()
26+
.everyNth(5)
27+
.forEach { println("Value: $it") }
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.athenian
2+
3+
fun <T> Sequence<T>.everyOther() = EveryOtherSequence(this)
4+
5+
class EveryOtherSequence<T>(private val underlyingSequence: Sequence<T>) : Sequence<T> {
6+
override fun iterator() =
7+
object : Iterator<T> {
8+
val iterator = underlyingSequence.iterator()
9+
10+
override fun hasNext() = iterator.hasNext()
11+
12+
override fun next(): T {
13+
val item = iterator.next()
14+
if (iterator.hasNext())
15+
iterator.next()
16+
return item
17+
}
18+
}
19+
}
20+
21+
fun main() {
22+
(0..10)
23+
.asSequence()
24+
.everyOther()
25+
.forEach { println("Value: $it") }
26+
}

0 commit comments

Comments
 (0)