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

Scala example source code file (Memoisable.scala)

This example Scala source code file (Memoisable.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, a, any, anyref, anyref, defaultmemoisable, in, memoisable, memoisable, out, success, t, x, x

The Scala Memoisable.scala source code

// -----------------------------------------------------------------------------
//
//  Scalax - The Scala Community Library
//  Copyright (c) 2005-8 The Scalax Project. All rights reserved.
//
//  The primary distribution site is http://scalax.scalaforge.org/
//
//  This software is released under the terms of the Revised BSD License.
//  There is NO WARRANTY.  See the file LICENSE for the full text.
//
// -----------------------------------------------------------------------------

package scala.tools.scalap
package scalax
package rules

import scala.collection.mutable.HashMap

trait MemoisableRules extends Rules {
  def memo[In <: Memoisable, Out, A, X](key : AnyRef)(toRule : => In => Result[Out, A, X]) = {
    lazy val rule = toRule
    from[In] { in => in.memo(key, rule(in)) }
  }
  
  override def ruleWithName[In, Out, A, X](name : String, f : In => rules.Result[Out, A, X]) = super.ruleWithName(name, (in : In) => in match {
      case s : Memoisable => s.memo(name, f(in))
      case _ => f(in)
    })
}

trait Memoisable {
  def memo[A](key : AnyRef, a : => A) : A
}


object DefaultMemoisable {
  var debug = false
}

trait DefaultMemoisable extends Memoisable {
  protected val map = new HashMap[AnyRef, Any]

  def memo[A](key : AnyRef, a : => A) = {
    map.getOrElseUpdate(key, compute(key, a)).asInstanceOf[A]
  }
  
  protected def compute[A](key : AnyRef, a : => A): Any = a match {
    case success : Success[_, _] => onSuccess(key, success); success
    case other => 
      if(DefaultMemoisable.debug) println(key + " -> " + other) 
      other
  }
  
  protected def onSuccess[S, T](key : AnyRef,  result : Success[S, T])  { 
    val Success(out, t) = result
    if(DefaultMemoisable.debug) println(key + " -> " + t + " (" + out + ")") 
  }
}



Other Scala examples (source code examples)

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