File tree 3 files changed +60
-1
lines changed
src/main/kotlin/org/athenian
3 files changed +60
-1
lines changed Original file line number Diff line number Diff line change @@ -5,10 +5,14 @@ The repo was motivated by [this post](https://medium.com/@desaismital/declarativ
5
5
* [ Continuations, Sequences and Yields] ( src/main/kotlin/org/athenian/ContinuationsSequencesAndYields.kt )
6
6
* [ Collection Operations] ( src/main/kotlin/org/athenian/CollectionOperations.kt )
7
7
* [ 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 )
9
9
* [ Eager vs Lazy Ordering] ( src/main/kotlin/org/athenian/EvaluationOrdering.kt )
10
10
* [ More Eager vs Lazy Ordering] ( src/main/kotlin/org/athenian/MoreEvaluationOrdering.kt )
11
11
* [ Expensive Calculations] ( src/main/kotlin/org/athenian/ExpensiveCalculations.kt )
12
12
* [ 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 )
13
15
14
16
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments