-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from WaveBeans/stream-concatenation
#82 Concatenation operation
- Loading branch information
Showing
17 changed files
with
358 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [ [#82](https://github.com/WaveBeans/wavebeans/issues/82) ] [Concatenation](/docs/user/api/operations/concatenation-operation.md) operation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
Concatenation operation | ||
===== | ||
|
||
<!-- START doctoc generated TOC please keep comment here to allow auto update --> | ||
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> | ||
<!-- END doctoc generated TOC please keep comment here to allow auto update --> | ||
|
||
Whenever you need to concatenate stream one after another you may use this operator. The leading stream though should be finite and be the type of `FiniteStream<T>`, another stream operand should be finite or infinite but the same type. Depending on the second argument type, the resulting stream will be either finite of type `FiniteStream<T>`, or infinite of type `BeanStream<T>`. | ||
|
||
This is what is happening schematically, the stream are being read one after another: | ||
|
||
```text | ||
[1, 2, 3, 4] .. [10, 12, 13] -> [1, 2, 3, 4, 10, 12, 13] | ||
``` | ||
|
||
The inline operator `..` concatenate two streams, you may use them one-by-one as many as you like to concatenate more streams altogether: | ||
|
||
``` | ||
// finite streams | ||
val a = listOf(1, 2).input() | ||
val b = listOf(3, 4, 5).input() | ||
val c = listOf(10, 20, 30).input() | ||
// infinite stream | ||
val d = listOf(100, 200).input().stream(AfterFilling(0)) | ||
(a..b).asSequence(44100.0f).toList() | ||
// results in stream [1, 2, 3, 4, 5] | ||
(b..c).asSequence(44100.0f).toList() | ||
// results in stream [3, 4, 5, 10, 20, 30] | ||
(a..b..c).asSequence(44100.0f).toList() | ||
// results in stream [1, 2, 3, 4, 5, 10, 20, 30] | ||
(a..b..c..d).asSequence(44100.0f).take(15).toList() | ||
// results in stream [1, 2, 3, 4, 5, 10, 20, 30, 100, 200, 0, 0, 0, 0, 0] | ||
(d..c).asSequence(44100.0f).take(15).toList() | ||
// won't compile as the first operand `d` is infinite stream | ||
``` | ||
|
||
If you want to do a finite stream from the infinite one, you may use [trim operation](trim-operation.md): | ||
|
||
```kotlin | ||
val sine1 = 440.sine().trim(3) | ||
val sine2 = listOf(0.1, 0.2, 0.3).map { sampleOf(it) }.input() | ||
|
||
(sine1..sine2).asSequence(1000.0f).toList() | ||
// results in stream [1.0, -0.9297764858882515, 0.7289686274214119, 0.1, 0.2, 0.3] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
exe/src/main/kotlin/io/wavebeans/execution/pod/AnyStreamingPod.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,19 @@ | ||
package io.wavebeans.execution.pod | ||
|
||
import io.wavebeans.lib.BeanStream | ||
import io.wavebeans.lib.stream.FiniteStream | ||
|
||
class AnyStreamingPod( | ||
bean: BeanStream<Any>, | ||
podKey: PodKey | ||
) : StreamingPod<Any, BeanStream<Any>>( | ||
bean = bean, | ||
podKey = podKey | ||
) | ||
class AnyFiniteStreamingPod( | ||
bean: FiniteStream<Any>, | ||
podKey: PodKey | ||
) : StreamingPod<Any, FiniteStream<Any>>( | ||
bean = bean, | ||
podKey = podKey | ||
) |
22 changes: 22 additions & 0 deletions
22
exe/src/main/kotlin/io/wavebeans/execution/podproxy/AnyFiniteStreamPodProxy.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.wavebeans.execution.podproxy | ||
|
||
import io.wavebeans.execution.medium.value | ||
import io.wavebeans.execution.pod.PodKey | ||
import io.wavebeans.lib.stream.FiniteStream | ||
import java.util.concurrent.TimeUnit | ||
|
||
class AnyFiniteStreamPodProxy( | ||
podKey: PodKey, | ||
forPartition: Int | ||
) : FiniteStream<Any>, StreamingPodProxy( | ||
pointedTo = podKey, | ||
forPartition = forPartition | ||
) { | ||
override fun length(timeUnit: TimeUnit): Long { | ||
val bush = podDiscovery.bushFor(pointedTo) | ||
val caller = bushCallerRepository.create(bush, pointedTo) | ||
return caller.call("length?timeUnit=${timeUnit}") | ||
.get(5000, TimeUnit.MILLISECONDS) | ||
.value() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
lib/src/main/kotlin/io/wavebeans/lib/stream/AfterFillingFiniteStream.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package io.wavebeans.lib.stream | ||
|
||
import io.wavebeans.lib.* | ||
import kotlinx.serialization.Serializable | ||
|
||
class AfterFilling<T : Any>( | ||
private val zeroFiller: T | ||
) : FiniteToStream<T> { | ||
override fun convert(finiteStream: FiniteStream<T>): BeanStream<T> { | ||
return AfterFillingFiniteStream(finiteStream, AfterFillingFiniteStreamParams(zeroFiller)) | ||
} | ||
} | ||
|
||
@Serializable | ||
data class AfterFillingFiniteStreamParams<T>( | ||
val zeroFiller: T | ||
) : BeanParams() | ||
|
||
private class AfterFillingFiniteStream<T : Any>( | ||
val finiteStream: FiniteStream<T>, | ||
val params: AfterFillingFiniteStreamParams<T> | ||
) : BeanStream<T>, SingleBean<T> { | ||
|
||
override val parameters: BeanParams = params | ||
|
||
override val input: Bean<T> = finiteStream | ||
|
||
override fun asSequence(sampleRate: Float): Sequence<T> { | ||
return object : Iterator<T> { | ||
|
||
val iterator = finiteStream | ||
.asSequence(sampleRate) | ||
.iterator() | ||
|
||
override fun hasNext(): Boolean = true | ||
|
||
override fun next(): T { | ||
return if (iterator.hasNext()) { // there is something left to read | ||
iterator.next() | ||
} else { | ||
params.zeroFiller | ||
} | ||
} | ||
}.asSequence() | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
lib/src/main/kotlin/io/wavebeans/lib/stream/ConcatenatedStream.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.wavebeans.lib.stream | ||
|
||
import io.wavebeans.lib.* | ||
import java.util.concurrent.TimeUnit | ||
|
||
operator fun <T : Any> FiniteStream<T>.rangeTo(finiteStream: FiniteStream<T>): FiniteStream<T> = | ||
ConcatenatedFiniteStream(this, finiteStream) | ||
|
||
operator fun <T : Any> FiniteStream<T>.rangeTo(stream: BeanStream<T>): BeanStream<T> = | ||
ConcatenatedStream(this, stream) | ||
|
||
class ConcatenatedFiniteStream<T : Any>( | ||
private val stream1: FiniteStream<T>, | ||
private val stream2: FiniteStream<T>, | ||
override val parameters: NoParams = NoParams() | ||
) : FiniteStream<T>, MultiBean<T>, SinglePartitionBean { | ||
|
||
override val inputs: List<Bean<T>> | ||
get() = listOf(stream1, stream2) | ||
|
||
override fun asSequence(sampleRate: Float): Sequence<T> { | ||
return stream1.asSequence(sampleRate) + stream2.asSequence(sampleRate) | ||
} | ||
|
||
override fun length(timeUnit: TimeUnit): Long { | ||
return stream1.length(timeUnit) + stream2.length(timeUnit) | ||
} | ||
} | ||
|
||
class ConcatenatedStream<T : Any>( | ||
private val stream1: FiniteStream<T>, | ||
private val stream2: BeanStream<T>, | ||
override val parameters: NoParams = NoParams() | ||
) : BeanStream<T>, MultiBean<T>, SinglePartitionBean { | ||
|
||
override val inputs: List<Bean<T>> | ||
get() = listOf(stream1, stream2) | ||
|
||
override fun asSequence(sampleRate: Float): Sequence<T> { | ||
return stream1.asSequence(sampleRate) + stream2.asSequence(sampleRate) | ||
} | ||
} |
Oops, something went wrong.