Skip to content

Instantly share code, notes, and snippets.

@lancegatlin
Created May 3, 2016 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lancegatlin/7aa2c72dfed93794d1e9fc09d3524495 to your computer and use it in GitHub Desktop.
Save lancegatlin/7aa2c72dfed93794d1e9fc09d3524495 to your computer and use it in GitHub Desktop.
package object {
implicit class VarTraversablePML[M[AA] <: Traversable[AA],A](val self: Var[M[A]]) extends AnyVal {
def ++=(more: M[A]) : Unit = {
self.toOption match {
case Some(ma) =>
self := ma.++[A,M[A]](more)
case None =>
self := more
}
}
}
implicit class VarSetPML[A](val self: Var[Set[A]]) extends AnyVal {
def contains(a: A) : Boolean = {
self.toOption match {
case Some(ma) => ma.contains(a)
case None => false
}
}
def +=(more: A) : Unit = {
self.toOption match {
case Some(ma) =>
self := ma + more
case None =>
self := Set(more)
}
}
}
implicit class VarSeqPML[M[AA] <: Seq[AA],A](val self: Var[M[A]]) extends AnyVal {
def +=(more: A)(implicit cbf:CanBuildFrom[Nothing,A,M[A]]) : Unit = {
self.toOption match {
case Some(ma) =>
self := ma.:+[A,M[A]](more)
case None =>
val builder = cbf()
builder += more
self := builder.result()
}
}
}
}
import scala.languageFeature.higherKinds
trait Var[A] {
def isSet : Boolean
def :=(a: A) : Unit
def unset() : Unit
def toOption: Option[A]
def assumeSetAndGet : A
}
object Var {
class Set[A](
var value: Option[A] = None
) extends Var[A] {
override def isSet = value.isDefined
override def :=(a: A): Unit =
value = Some(a)
override def unset(): Unit =
value = None
override def toOption: Option[A] = value
override def assumeSetAndGet: A = value.get
}
def Unset[A] = new Set[A]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment