canonical list operations, as in ML;
authorwenzelm
Thu Apr 24 00:29:55 2014 +0200 (2014-04-24)
changeset 566862386d1a3ca8f
parent 56685 535d59d4ed12
child 56687 7fb98325722a
canonical list operations, as in ML;
avoid odd mutable data structures;
src/Pure/System/event_bus.scala
src/Pure/library.scala
     1.1 --- a/src/Pure/System/event_bus.scala	Thu Apr 24 00:27:06 2014 +0200
     1.2 +++ b/src/Pure/System/event_bus.scala	Thu Apr 24 00:29:55 2014 +0200
     1.3 @@ -9,29 +9,24 @@
     1.4  
     1.5  
     1.6  import scala.actors.Actor, Actor._
     1.7 -import scala.collection.mutable.ListBuffer
     1.8  
     1.9  
    1.10  class Event_Bus[Event]
    1.11  {
    1.12    /* receivers */
    1.13  
    1.14 -  private val receivers = new ListBuffer[Actor]
    1.15 +  private val receivers = Synchronized(List.empty[Actor])
    1.16  
    1.17 -  def += (r: Actor) { synchronized { receivers += r } }
    1.18 -  def + (r: Actor): Event_Bus[Event] = { this += r; this }
    1.19 +  def += (r: Actor) { receivers >> (rs => Library.insert(r, rs)) }
    1.20  
    1.21    def += (f: Event => Unit) {
    1.22      this += actor { loop { react { case x => f(x.asInstanceOf[Event]) } } }
    1.23    }
    1.24  
    1.25 -  def + (f: Event => Unit): Event_Bus[Event] = { this += f; this }
    1.26 -
    1.27 -  def -= (r: Actor) { synchronized { receivers -= r } }
    1.28 -  def - (r: Actor) = { this -= r; this }
    1.29 +  def -= (r: Actor) { receivers >> (rs => Library.remove(r, rs)) }
    1.30  
    1.31  
    1.32    /* event invocation */
    1.33  
    1.34 -  def event(x: Event) { synchronized { receivers.foreach(_ ! x) } }
    1.35 +  def event(x: Event) { receivers().reverseIterator.foreach(_ ! x) }
    1.36  }
     2.1 --- a/src/Pure/library.scala	Thu Apr 24 00:27:06 2014 +0200
     2.2 +++ b/src/Pure/library.scala	Thu Apr 24 00:29:55 2014 +0200
     2.3 @@ -153,6 +153,14 @@
     2.4    }
     2.5  
     2.6  
     2.7 +  /* canonical list operations */
     2.8 +
     2.9 +  def member[A, B](x: B, xs: List[A]): Boolean = xs.exists(_ == x)
    2.10 +  def insert[A](x: A, xs: List[A]): List[A] = if (member(x, xs)) xs else x :: xs
    2.11 +  def remove[A, B](x: B, xs: List[A]): List[A] = if (member(x, xs)) xs.filterNot(_ == x) else xs
    2.12 +  def update[A](x: A, xs: List[A]): List[A] = x :: remove(x, xs)
    2.13 +
    2.14 +
    2.15    /* Java futures */
    2.16  
    2.17    def future_value[A](x: A) = new JFuture[A]