alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (twoPlaceBuffer.scala)

This example Scala source code file (twoPlaceBuffer.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

a, b0, b0, b1, b1, b2, chan, chan, consumer, impl, oneplacebuffer, producer, unit, unit

The Scala twoPlaceBuffer.scala source code

package examples.pilib

import scala.concurrent.pilib._

/** Two-place buffer specification and implementation. */
object twoPlaceBuffer extends Application {

  /**
   * Specification.
   */
  def Spec[A](in: Chan[A], out: Chan[A]) {

    def B0: Unit = choice (
      in * (x => B1(x))
    )

    def B1(x: A): Unit = choice (
      out(x) * (B0),
      in * (y => B2(x, y)) 
    )

    def B2(x: A, y: A): Unit = choice (
      out(x) * (B1(y))
    )

    B0
  }

  /**
   * Implementation using two one-place buffers.
   */
  def Impl[A](in: Chan[A], out: Chan[A]) {
    ///- ... complete here ...
    // one-place buffer
    def OnePlaceBuffer[A](in: Chan[A], out: Chan[A]) {
      def B0: Unit = choice ( in * (x => B1(x)) )
      def B1(x: A): Unit = choice ( out(x) * (B0))
      B0
    }
    val hidden = new Chan[A]
    spawn < OnePlaceBuffer(in, hidden) | OnePlaceBuffer(hidden, out) >
    ///+
  }

  val random = new util.Random()

  def Producer(n: Int, in: Chan[String]) {
    Thread.sleep(random.nextInt(1000))
    val msg = "" + n
    choice (in(msg) * {})
    Producer(n + 1, in)
  }

  def Consumer(out: Chan[String]) {
    Thread.sleep(random.nextInt(1000))
    choice (out * { msg => () })
    Consumer(out)
  }

  val in = new Chan[String]
  in.attach(s => println("put " + s))
  val out = new Chan[String]
  out.attach(s => println("get " + s))
  //spawn < Producer(0, in) | Consumer(out) | Spec(in, out) >
  spawn < Producer(0, in) | Consumer(out) | Impl(in, out) >

}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala twoPlaceBuffer.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.