0.1.4-dev-4 #86
@ -58,9 +58,13 @@ fun <T : Any> Sampler<T>.sampleBuffer(
|
|||||||
//creating temporary storage once
|
//creating temporary storage once
|
||||||
val tmp = ArrayList<T>(size)
|
val tmp = ArrayList<T>(size)
|
||||||
return sample(generator).collect { chain ->
|
return sample(generator).collect { chain ->
|
||||||
for (i in tmp.indices) {
|
//clear list from previous run
|
||||||
tmp[i] = chain.next()
|
tmp.clear()
|
||||||
|
//Fill list
|
||||||
|
repeat(size){
|
||||||
|
tmp.add(chain.next())
|
||||||
}
|
}
|
||||||
|
//return new buffer with elements from tmp
|
||||||
bufferFactory(size) { tmp[it] }
|
bufferFactory(size) { tmp[it] }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,4 +12,3 @@ class RandomChain<out R>(val generator: RandomGenerator, private val gen: suspen
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun <R> RandomGenerator.chain(gen: suspend RandomGenerator.() -> R): RandomChain<R> = RandomChain(this, gen)
|
fun <R> RandomGenerator.chain(gen: suspend RandomGenerator.() -> R): RandomChain<R> = RandomChain(this, gen)
|
||||||
fun <R> RandomGenerator.flow(gen: suspend RandomGenerator.() -> R) = chain(gen).fork()
|
|
@ -0,0 +1,17 @@
|
|||||||
|
package scientifik.kmath.prob
|
||||||
|
|
||||||
|
import kotlinx.coroutines.runBlocking
|
||||||
|
import kotlin.test.Test
|
||||||
|
|
||||||
|
class SamplerTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun bufferSamplerTest(){
|
||||||
|
val sampler: Sampler<Double> =
|
||||||
|
BasicSampler { it.chain { nextDouble() } }
|
||||||
|
val data = sampler.sampleBuffer(RandomGenerator.default, 100)
|
||||||
|
runBlocking {
|
||||||
|
println(data.next())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user