Skip to content

Instantly share code, notes, and snippets.

@wheaties
Last active August 31, 2015 18:55
Show Gist options
  • Select an option

  • Save wheaties/03ce4d48ead513c57015 to your computer and use it in GitHub Desktop.

Select an option

Save wheaties/03ce4d48ead513c57015 to your computer and use it in GitHub Desktop.
//historical chain of value.
import java.concurrent.ConcurrentSkipListMap
class History[Token, A, B](tk: => Token, prev: ConcurrentSkipListMap[Token, A], id: A => B){
def apply(f: A => A): (Token, B) ={
val token = tk
(token, id(update(token, f)))
}
def map[C](f: B => C) = new History(prev, id andThen f)
def flatMap[C](f: B => History[Token, A, C]) ={
val entry = prev.lastEntry().getValue()
f(id(entry))
}
protected def update(token: Token, f: A => A): A ={
val entry = prev.lastEntry().getValue()
val value = f(entry)
prev.put(token, value)
value
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment