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

Scala example source code file (fringe.scala)

This example Scala source code file (fringe.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

channel, comparefringe, computefringe, computefringe, continue, equal, equal, extract, leaf, node, node, none, tree, tree

The Scala fringe.scala source code

package examples.actors

import scala.actors.Actor._
import scala.actors.{Channel, OutputChannel}

/**
 @author Philipp Haller
 @version 1.1, 09/21/2007
 */
object fringe extends Application {

  abstract class Tree
  case class Node(left: Tree, right: Tree) extends Tree
  case class Leaf(v: Int) extends Tree

  case class CompareFringe(t1: Tree, t2: Tree)
  case class ComputeFringe(t1: Tree, atoms: OutputChannel[Option[Leaf]])
  case class Equal(atom1: Option[Leaf], atom2: Option[Leaf])
  case class Extract(tree: Tree)

  val comparator = actor {
    val extractor1 = actor(extractorBehavior())
    val extractor2 = actor(extractorBehavior())
    val ch1 = new Channel[Option[Leaf]]
    val ch2 = new Channel[Option[Leaf]]
    loop {
      react {
        case CompareFringe(tree1, tree2) =>
          extractor1 ! ComputeFringe(tree1, ch1)
          extractor2 ! ComputeFringe(tree2, ch2)
          self ! Equal(ch1.?, ch2.?)

        case Equal(atom1, atom2) =>
          if (atom1 == atom2) atom1 match {
            case None =>
              println("same fringe")
              exit()
            case _ =>
              self ! Equal(ch1.?, ch2.?)
          } else {
            println("fringes differ")
            exit()
          }
      }
    }
  }

  val extractorBehavior = () => {
    var output: OutputChannel[Option[Leaf]] = null
    loop {
      react {
        case ComputeFringe(tree, leafSink) =>
          output = leafSink
          self ! Extract(tree)

        case Extract(tree) => tree match {
          case atom @ Leaf(_) =>
            output ! Some(atom)
            sender ! 'Continue

          case Node(left, right) =>
            val outer = self
            val outerCont = sender
            val cont = actor {
              react {
                case 'Continue =>
                  outer.send(Extract(right), outerCont)
              }
            }
            self.send(Extract(left), cont)
        }

        case 'Continue =>
          output ! None
          exit()
      }
    }
  }

  comparator ! CompareFringe(Node(Leaf(5), Node(Leaf(7), Leaf(3))),
                Node(Leaf(5), Node(Leaf(7), Leaf(3))))
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala fringe.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.