Skip to content

Commit

Permalink
Rewrite scala stream demos
Browse files Browse the repository at this point in the history
  • Loading branch information
gstamatelat committed Jul 5, 2018
1 parent 67d72c2 commit fd70b5f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 23 deletions.
28 changes: 17 additions & 11 deletions demo-scala/src/UnweightedStream.scala
Original file line number Diff line number Diff line change
@@ -1,44 +1,50 @@
import java.util.Random

import SamplingIterator._
import SamplingTraversableOnce._
import gr.james.sampling.{RandomSampling, WatermanSampling}

import scala.collection.JavaConverters._

/**
* Extension of the [[Iterator]] with the <code>sample</code> method.
* Extension of [[TraversableOnce]] with the <code>sample</code> method.
*
* @param it the source iterator
* @param it the source
* @tparam T the element type
*/
class SamplingIterator[T](val it: Iterator[T]) {
class SamplingTraversableOnce[T](val it: TraversableOnce[T]) {
private val foldOperation =
(rs: RandomSampling[T], i: T) => {
rs.feed(i)
rs
}

/**
* Samples this iterator using the provided algorithm.
* Samples this iterator using the provided algorithm and returns a copy of the reservoir.
*
* @param rs the sampling algorithm
* @return a [[List]] containing the sampled elements
* @throws NullPointerException if <code>rs</code> is <code>null</code>
* @throws IllegalArgumentException if <code>rs</code> is not empty
*/
def sample(rs: RandomSampling[T]): List[T] = it.foldLeft(rs)(foldOperation).sample().asScala.toList
def sample(rs: RandomSampling[T]): List[T] = {
require(rs.sample().isEmpty)
it.foldLeft(rs)(foldOperation).sample().asScala.toList
}
}

/**
* The [[SamplingIterator]] companion object with the <code>iteratorToSamplingIterator</code> implicit conversion.
* The [[SamplingTraversableOnce]] companion object with the <code>traversableOnceImplicitConversion</code> implicit
* conversion.
*/
object SamplingIterator {
implicit def iteratorToSamplingIterator[T](s: Iterator[T]): SamplingIterator[T] =
new SamplingIterator(s)
object SamplingTraversableOnce {
implicit def traversableOnceImplicitConversion[T](s: TraversableOnce[T]): SamplingTraversableOnce[T] =
new SamplingTraversableOnce(s)
}

/**
* Unweighted random sampling using functional constructs.
*/
object UnweightedStream extends App {
val sample = (0 until 20).iterator.sample(new WatermanSampling[Int](5, new Random()))
val sample = (0 until 20).sample(new WatermanSampling[Int](5, new Random()))
println(sample)
}
29 changes: 17 additions & 12 deletions demo-scala/src/WeightedStream.scala
Original file line number Diff line number Diff line change
@@ -1,39 +1,44 @@
import java.util.Random

import WeightedSamplingIterator._
import WeightedSamplingTraversableOnce._
import gr.james.sampling.{ChaoSampling, WeightedRandomSampling}

import scala.collection.JavaConverters._

/**
* Extension of the [[Iterator]] with the <code>sample</code> method.
* Extension of [[TraversableOnce]] with the <code>sample</code> method.
*
* @param it the source iterator
* @param it the source
* @tparam T the element type
*/
class WeightedSamplingIterator[T](val it: Iterator[(T, Double)]) {
class WeightedSamplingTraversableOnce[T](val it: TraversableOnce[(T, Double)]) {
private val foldOperation =
(rs: WeightedRandomSampling[T], i: (T, Double)) => {
rs.feed(i._1, i._2)
rs
}

/**
* Samples this iterator using the provided algorithm.
* Samples this iterator using the provided algorithm and returns a copy of the reservoir.
*
* @param wrs the sampling algorithm
* @return a [[List]] containing the sampled elements
* @throws NullPointerException if <code>wrs</code> is <code>null</code>
* @throws IllegalArgumentException if <code>wrs</code> is not empty
*/
def sample(wrs: WeightedRandomSampling[T]): List[T] = it.foldLeft(wrs)(foldOperation).sample().asScala.toList
def sample(wrs: WeightedRandomSampling[T]): List[T] = {
require(wrs.sample().isEmpty)
it.foldLeft(wrs)(foldOperation).sample().asScala.toList
}
}

/**
* The [[WeightedSamplingIterator]] companion object with the <code>iteratorToWeightedSamplingIterator</code> implicit
* conversion.
* The [[WeightedSamplingTraversableOnce]] companion object with the <code>traversableOnceImplicitConversion</code>
* implicit conversion.
*/
object WeightedSamplingIterator {
implicit def iteratorToWeightedSamplingIterator[T](s: Iterator[(T, Double)]): WeightedSamplingIterator[T] =
new WeightedSamplingIterator(s)
object WeightedSamplingTraversableOnce {
implicit def traversableOnceImplicitConversion[T](s: TraversableOnce[(T, Double)]): WeightedSamplingTraversableOnce[T] =
new WeightedSamplingTraversableOnce(s)
}

/**
Expand All @@ -47,6 +52,6 @@ object WeightedStream extends App {
"random" -> 3.0,
"sampling" -> 4.0,
"reservoir" -> 5.0
).iterator.sample(new ChaoSampling[String](2, new Random))
).sample(new ChaoSampling[String](2, new Random))
println(sample)
}

0 comments on commit fd70b5f

Please sign in to comment.