juan_gandhi: (Default)
isFinite tells us whether we can cache it; maybe it's coming from a predicate that checks the size, or something.


class Cache[K,V](isFinite: Boolean, build: K => V):
  private val cache: mutable.Map[K, V] = mutable.Map[K, V]()

  private val app: K => V = if (isFinite) (k: K) => cache.getOrElseUpdate(k, build(k)) else (k: K) => build(k)
  
  inline def apply(k: K): V = app(k)

juan_gandhi: (VP)
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)
I'm totally excited with this idea; it looks like either nobody got it, or it is totally obvious.
It was not obvious to me until last night.
juan_gandhi: (Default)
Posted just one small class, Caching.
here

Already posted the first version of Caching here; seems like it requires a blog entry with explanations... or no? It contains a couple of tricks and one questionable solution...

Profile

juan_gandhi: (Default)
Juan-Carlos Gandhi

November 2025

S M T W T F S
       1
23456 7 8
9 1011 12 1314 15
16171819 20 2122
23 24 252627 2829
30      

Syndicate

RSS Atom

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Nov. 29th, 2025 10:53 am
Powered by Dreamwidth Studios