juan_gandhi: (VP)
2016-04-07 11:43 am
Entry tags:

dealing with time

Here's the essence of cache as I wrote about earlier tonight.

trait TimeReader {

  def currentTime: Long = System.currentTimeMillis

  def flowOfTime = Iterator.iterate(currentTime){_ => currentTime}

  def interval(n:Long) = {
    flowOfTime.next // the first drop, may be stale
    val end = flowOfTime.next + n
    flowOfTime takeWhile (end >)
  }

  def interval(duration: Duration) = interval(duration.toMillis)
  //...


Note that you can override currentTime
for {
value <- readFromSource
t <- interval(60 seconds)
} useThisValue(value)


e.g.
for {i <- 1 to 10; t <- interval(1 second) } {Thread.sleep(1); println(s"$i @$t") }
juan_gandhi: (VP)
2013-11-27 01:53 pm

to beguile the time, be like the time

trait TimeReader {
  def currentTime: Long = System.currentTimeMillis
}


...
class DataSomething(val name:String, val number:Long...) extends TimeReader {...}


...
val sut = new DataSomething("Ivanov", 42,...) with MockTime