本文整理汇总了Scala中scala.util.DynamicVariable类的典型用法代码示例。如果您正苦于以下问题:Scala DynamicVariable类的具体用法?Scala DynamicVariable怎么用?Scala DynamicVariable使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DynamicVariable类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: TaskScheduler
//设置package包名称以及导入依赖的类
import java.util.concurrent._
import scala.util.DynamicVariable
package object common {
val forkJoinPool = new ForkJoinPool
abstract class TaskScheduler {
def schedule[T](body: => T): ForkJoinTask[T]
def parallel[A, B](taskA: => A, taskB: => B): (A, B) = {
val right = task {
taskB
}
val left = taskA
(left, right.join())
}
}
class DefaultTaskScheduler extends TaskScheduler {
def schedule[T](body: => T): ForkJoinTask[T] = {
val t = new RecursiveTask[T] {
def compute = body
}
Thread.currentThread match {
case wt: ForkJoinWorkerThread =>
t.fork()
case _ =>
forkJoinPool.execute(t)
}
t
}
}
val scheduler =
new DynamicVariable[TaskScheduler](new DefaultTaskScheduler)
def task[T](body: => T): ForkJoinTask[T] = {
scheduler.value.schedule(body)
}
def parallel[A, B](taskA: => A, taskB: => B): (A, B) = {
scheduler.value.parallel(taskA, taskB)
}
def parallel[A, B, C, D](taskA: => A, taskB: => B, taskC: => C, taskD: => D): (A, B, C, D) = {
val ta = task { taskA }
val tb = task { taskB }
val tc = task { taskC }
val td = taskD
(ta.join(), tb.join(), tc.join(), td)
}
}
示例2: Signal
//设置package包名称以及导入依赖的类
package observatory
import scala.util.DynamicVariable
class Signal[T](expr: => T) {
import Signal._
private var myExpr: () => T = _
private var myValue: T = _
private var observers: Set[Signal[_]] = Set()
private var observed: List[Signal[_]] = Nil
update(expr)
protected def computeValue(): Unit = {
for (sig <- observed)
sig.observers -= this
observed = Nil
val newValue = caller.withValue(this)(myExpr())
//if (myValue != newValue) {
myValue = newValue
val obs = observers
observers = Set()
obs.foreach(_.computeValue())
//}
}
protected def update(expr: => T): Unit = {
myExpr = () => expr
computeValue()
}
def apply() = {
observers += caller.value
assert(!caller.value.observers.contains(this), "cyclic signal definition")
caller.value.observed ::= this
myValue
}
}
class Var[T](expr: => T) extends Signal[T](expr) {
override def update(expr: => T): Unit = super.update(expr)
}
object Var {
def apply[T](expr: => T) = new Var(expr)
}
object NoSignal extends Signal[Nothing](???) {
override def computeValue() = ()
}
object Signal {
val caller = new DynamicVariable[Signal[_]](NoSignal)
def apply[T](expr: => T) = new Signal(expr)
}
示例3: TaskScheduler
//设置package包名称以及导入依赖的类
import java.util.concurrent._
import scala.util.DynamicVariable
package object common {
val forkJoinPool = new ForkJoinPool
abstract class TaskScheduler {
def schedule[T](body: => T): ForkJoinTask[T]
def parallel[A, B](taskA: => A, taskB: => B): (A, B) = {
val right = task {
taskB
}
val left = taskA
(left, right.join())
}
}
class DefaultTaskScheduler extends TaskScheduler {
def schedule[T](body: => T): ForkJoinTask[T] = {
val t = new RecursiveTask[T] {
def compute = body
}
Thread.currentThread match {
case wt: ForkJoinWorkerThread =>
t.fork()
case _ =>
forkJoinPool.execute(t)
}
t
}
}
val scheduler =
new DynamicVariable[TaskScheduler](new DefaultTaskScheduler)
def task[T](body: => T): ForkJoinTask[T] = {
scheduler.value.schedule(body)
}
def parallel[A, B](taskA: => A, taskB: => B): (A, B) = {
scheduler.value.parallel(taskA, taskB)
}
def parallel[A, B, C, D](taskA: => A, taskB: => B, taskC: => C, taskD: => D): (A, B, C, D) = {
val ta = task { taskA }
val tb = task { taskB }
val tc = task { taskC }
val td = taskD
(ta.join(), tb.join(), tc.join(), td)
}
}
示例4: Signal
//设置package包名称以及导入依赖的类
package calculator
import scala.util.DynamicVariable
class Signal[T](expr: => T) {
import Signal._
private var myExpr: () => T = _
private var myValue: T = _
private var observers: Set[Signal[_]] = Set()
private var observed: List[Signal[_]] = Nil
update(expr)
protected def computeValue(): Unit = {
for (sig <- observed)
sig.observers -= this
observed = Nil
val newValue = caller.withValue(this)(myExpr())
//if (myValue != newValue) {
myValue = newValue
val obs = observers
observers = Set()
obs.foreach(_.computeValue())
//}
}
protected def update(expr: => T): Unit = {
myExpr = () => expr
computeValue()
}
def apply() = {
observers += caller.value
assert(!caller.value.observers.contains(this), "cyclic signal definition")
caller.value.observed ::= this
myValue
}
}
class Var[T](expr: => T) extends Signal[T](expr) {
override def update(expr: => T): Unit = super.update(expr)
}
object Var {
def apply[T](expr: => T) = new Var(expr)
}
object NoSignal extends Signal[Nothing](???) {
override def computeValue() = ()
}
object Signal {
val caller = new DynamicVariable[Signal[_]](NoSignal)
def apply[T](expr: => T) = new Signal(expr)
}
示例5: TaskScheduler
//设置package包名称以及导入依赖的类
import java.util.concurrent._
import scala.util.DynamicVariable
package object common {
val forkJoinPool = new ForkJoinPool
abstract class TaskScheduler {
def schedule[T](body: => T): ForkJoinTask[T]
def parallel[A, B](taskA: => A, taskB: => B): (A, B) = {
val right = task {
taskB
}
val left = taskA
(left, right.join())
}
}
class DefaultTaskScheduler extends TaskScheduler {
def schedule[T](body: => T): ForkJoinTask[T] = {
val t = new RecursiveTask[T] {
def compute = body
}
forkJoinPool.execute(t)
t
}
}
val scheduler =
new DynamicVariable[TaskScheduler](new DefaultTaskScheduler)
def task[T](body: => T): ForkJoinTask[T] = {
scheduler.value.schedule(body)
}
def parallel[A, B](taskA: => A, taskB: => B): (A, B) = {
scheduler.value.parallel(taskA, taskB)
}
def parallel[A, B, C, D](taskA: => A, taskB: => B, taskC: => C, taskD: => D): (A, B, C, D) = {
val ta = task { taskA }
val tb = task { taskB }
val tc = task { taskC }
val td = taskD
(ta.join(), tb.join(), tc.join(), td)
}
}
示例6: Signal
//设置package包名称以及导入依赖的类
package calculator
import scala.util.DynamicVariable
class Signal[T](expr: => T) {
import Signal._
private var myExpr: () => T = _
private var myValue: T = _
private var observers: Set[Signal[_]] = Set()
private var observed: List[Signal[_]] = Nil
update(expr)
protected def computeValue(): Unit = {
for (sig <- observed) {
sig.observers -= this
}
observed = Nil
val newValue = caller.withValue(this)(myExpr())
//if (myValue != newValue) {
myValue = newValue
val obs = observers
observers = Set()
obs.foreach(_.computeValue())
//}
}
protected def update(expr: => T): Unit = {
myExpr = () => expr
computeValue()
}
def apply() = {
observers += caller.value
assert(!caller.value.observers.contains(this), "cyclic signal definition")
caller.value.observed ::= this
myValue
}
}
class Var[T](expr: => T) extends Signal[T](expr) {
override def update(expr: => T): Unit = super.update(expr)
}
object Var {
def apply[T](expr: => T) = new Var(expr)
}
object NoSignal extends Signal[Nothing](???) {
override def computeValue() = ()
}
object Signal {
val caller = new DynamicVariable[Signal[_]](NoSignal)
def apply[T](expr: => T) = new Signal(expr)
}
示例7: buffer
//设置package包名称以及导入依赖的类
package com.github.kmizu
import language.dynamics
import scala.xml._
import scala.util.DynamicVariable
package object xbuilder {
object % extends Dynamic {
private[this] val scope = new DynamicVariable[NodeBuffer](new NodeBuffer)
def buffer(content: => Any): NodeBuffer = {
scope.withValue(new NodeBuffer) {
content
scope.value
}
}
def applyDynamic(name: String)(content: => Any): Node = {
val evaledContent = content
if(name == "text" && evaledContent.isInstanceOf[String]) {
val text = Text(evaledContent.asInstanceOf[String])
scope.value += text
text
} else {
this.applyDynamicNamed(name)(Seq():_*)(content)
}
}
def selectDynamic(name: String): Node = applyDynamic(name)(null)
def applyDynamicNamed(name: String)(attributes: (String, String)*)(block: => Any): Elem = {
def createMetaData(attributeList: List[(String, String)]): MetaData = attributeList match {
case Nil => Null
case (key, value) :: xs => new UnprefixedAttribute(key, value, createMetaData(xs))
}
var attributeList : List[(String, String)] = attributes.toList.reverse
var metadata: MetaData = createMetaData(attributeList)
val newElement = scope.withValue(new NodeBuffer) {
block
Elem(null, name, metadata, TopScope, true, scope.value.toSeq:_*)
}
scope.value += newElement
newElement
}
}
}
示例8: Signal
//设置package包名称以及导入依赖的类
package week04.frp
import scala.util.DynamicVariable
class Signal[T](expr: => T) {
import Signal._
private var myExpr: () => T = _
private var myValue: T = _
private var observers: Set[Signal[_]] = Set()
private var observed: List[Signal[_]] = Nil
update(expr)
protected def computeValue(): Unit = {
for (sig <- observed)
sig.observers -= this
observed = Nil
val newValue = caller.withValue(this)(myExpr())
if (myValue != newValue) {
myValue = newValue
val obs = observers
observers = Set()
obs.foreach(_.computeValue())
}
}
protected def update(expr: => T): Unit = {
myExpr = () => expr
computeValue()
}
def apply() = {
observers += caller.value
assert(!caller.value.observers.contains(this), "cyclic signal definition") // s() = s() + 1
caller.value.observed ::= this
myValue
}
}
object NoSignal extends Signal[Nothing](???) {
override def computeValue() = ()
}
object Signal {
val caller = new DynamicVariable[Signal[_]](NoSignal)
def apply[T](expr: => T) = new Signal(expr)
}
示例9: Signal
//设置package包名称以及导入依赖的类
package week04
import scala.util.DynamicVariable
class Signal[T](expr: => T){
import Signal._
private var myExpr: ()=>T = _
private var myValue: T = _
private var observers: Set[Signal[_]] = Set()
update(expr)
protected def update(expr: => T):Unit ={
myExpr = () => expr
computeValue()
}
protected def computeValue():Unit ={
val newValue = caller.withValue(this)(myExpr())
if(myValue!=newValue){
myValue = newValue
val obs = observers
observers = Set()
obs.foreach(_.computeValue())
}
}
def apply(): T = {
observers += caller.value
assert(!caller.value.observers.contains(this), "cyclic redundancy error")
myValue
}
}
object NoSignal extends Signal[Nothing](???) {
override def computeValue() = ()
}
object Signal{
private val caller = new DynamicVariable[Signal[_]](NoSignal)
def apply[T](expr: => T) = new Signal(expr)
}
class Var[T](expr: => T) extends Signal[T](expr){
override def update(expr: => T): Unit = super.update(expr)
}
object Var {
def apply[T](expr: => T) = new Var(expr)
}
// observables
class StackableVariable[T](init: T) {
private var values: List[T] = List(init)
def value: T = values.head
def withValue[R](newValue: T)(op: => R): R = {
values = newValue :: values
try op finally values = values.tail
}
}