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 4, 2018
1 parent 1b3b389 commit d5d4eb3
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 25 deletions.
39 changes: 29 additions & 10 deletions demo-scala/src/UnweightedStream.scala
Original file line number Diff line number Diff line change
@@ -1,25 +1,44 @@
import java.util.Random

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

import scala.collection.JavaConverters._

/**
* Unweighted random sampling using functional constructs.
* Extension of the [[Iterator]] with the <code>sample</code> method.
*
* @param it the source iterator
* @tparam T the element type
*/
object UnweightedStream extends App {
val sample = (0 until 20)
.foldLeft(construct[Int](5, new Random()))(foldOperation)
.sample().asScala.toList

def construct[T](sample: Int, random: Random): RandomSampling[T] =
new WatermanSampling[T](sample, random)

def foldOperation[T] =
class SamplingIterator[T](val it: Iterator[T]) {
private val foldOperation =
(rs: RandomSampling[T], i: T) => {
rs.feed(i)
rs
}

/**
* Samples this iterator using the provided algorithm.
*
* @param rs the sampling algorithm
* @return a [[List]] containing the sampled elements
*/
def sample(rs: RandomSampling[T]): List[T] = it.foldLeft(rs)(foldOperation).sample().asScala.toList
}

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

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

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

import scala.collection.JavaConverters._

/**
* Extension of the [[Iterator]] with the <code>sample</code> method.
*
* @param it the source iterator
* @tparam T the element type
*/
class WeightedSamplingIterator[T](val it: Iterator[(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.
*
* @param wrs the sampling algorithm
* @return a [[List]] containing the sampled elements
*/
def sample(wrs: WeightedRandomSampling[T]): List[T] = it.foldLeft(wrs)(foldOperation).sample().asScala.toList
}

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

/**
* Weighted random sampling using functional constructs.
*/
object WeightedStream extends App {
val map = Map(
val sample = Map(
"collection" -> 1.0,
"algorithms" -> 2.0,
"java" -> 2.0,
"random" -> 3.0,
"sampling" -> 4.0,
"reservoir" -> 5.0
)
val sample = map
.foldLeft(construct[String](2, new Random()))(foldOperation)
.sample().asScala.toList

def construct[T](sample: Int, random: Random): WeightedRandomSampling[T] =
new ChaoSampling[T](sample, random)

def foldOperation[T] =
(wrs: WeightedRandomSampling[T], i: (T, Double)) => {
wrs.feed(i._1, i._2)
wrs
}

).iterator.sample(new ChaoSampling[String](2, new Random))
println(sample)
}

0 comments on commit d5d4eb3

Please sign in to comment.