Created
May 3, 2016 20:35
-
-
Save lancegatlin/7aa2c72dfed93794d1e9fc09d3524495 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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