Scala:How Many Steps Beyond Java?

R. Clayton

Monmouth County ACM • 7 June 2008


Outline

What is Scala?

Scala vs. Java

History

Scala High Points

Talk Objectives

What’s a Wiki?

What’s a Wiki?

What’s a Wiki?

Wiki Use Case

Analysis

Server-Side Implementations

Common Gateway Interface

CGI Example

Wiki URL

Java main()

class Wiki {
  public static void main(String args[]) {

    final Map<String, String> 
      input = getInput(args);
    final String page = input.get("page");

    if (input.get("action").equals("edit"))
      doEdit(page);
    else
      doSubmit(page, input.get("pagesource");
    }
  }
  // and so on
}

Scala main()

import java.util.Map
import java.util.HashMap

object Wiki {
  def main(args: Array[String]) {

    val input = getInput(args)
    val page = input.get("page")

    if (input.get("action").equals("edit"))
      doEdit(page)
    else
      doSubmit(page, input.get("pagesource")
    }
  // and so on
  }

Scala main() Observations

Java Action Methods

class Wiki {

  // blah blah blah 

  private static void
  doEdit(String pageName) {
    sendCGIResponse(makeEditPage(pageName));
    }

  private static void
  doSubmit(String pageName, String pageSource) {
    writeWikiPage(pageName, pageSource);
    sendCGIResponse(makeWikiPage(pageName));
    }

  // blah blah blah 

Scala Action Methods

object Wiki {

  // blah blah blah 

  private def 
  doEdit(pageName: String) {
    sendCGIResponse(makeEditPage(pageName))
    }

  private def 
  doSubmit(pageName: String, pageSource: String) {
    writeWikiPage(pageName, pageSource)
    sendCGIResponse(makeWikiPage(pageName))
    }

  // blah blah blah 

Scala Method Observations

Java Page Classes

class EditWikiPage
extends HTMLPage
implements SafeString {

  EditWikiPage(String fn) { super(fn); }

  EditWikiPage() { this(""); }

  public String
  decontaminate(String str, Sanitizer sfun) {
    // blah blah blah
    }

  // blah blah blah

  }

Scala Page Classes

class EditPageWiki(fn: String)
extends HTMLPage with SafeString {

  private val filename = fn

  def this() { this("") }

  // blah blah blah

  }

Scala Class Observations

String Scrubbing

Java Interfaces

interface SafeString {

  // Return the string scrubbed of the 
  // dangerous constructs defined in the 
  // given sanitizer.

  String decontaminate(
    String str, Sanitizer sfun);
  }

interface Sanitizer {
  String scrub(String str);
  }

Scala Traits

trait SafeString {
  def decontaminate(
    str: String, sfun: Sanitizer) {
    // blah blah blah
    }
  }


abstract class Sanitizer {
  def scrub(str: String): String
  }

Scala Traits Observations

Java Functional Parameters

abstract class HTMLPage {

  void process(TagTransformer f) {
    List<HTMLTag> newPage = 
      new ArrayList<HTMLTag>();
    for (HTMLTag tag: page) {
      final HTMLTag newTag = f.munge(tag);
      if (newTag != null)
        newPage.add(newTag);
      }
    page = newPage;
    }
  }

interface TagTransformer {
  HTMLTag munge(HTMLTag tag);
  }

Example Use

page = 
  page.transform(
    new TagTransformer() {
      public HTMLTag munge(HTMLTag tag) {
	return tag.isTag("script") 
               ? null 
               : tag;
	}
      }
    );

Scala Functional parameters

abstract class HTMLPage {

  def process(munge: HTMLTag => HTMLTag) {
    var newPage: List[HTMLTag] = List()
    for (tag <- page) {
      val newTag = munge(tag)
      if (newTag != null)
        newPage = newTag :: newPage
      }
    page = newPage
    }
  }

Functional Parameter Observations

Example Use

Observations

Scala For Comprehensions

def process(munge: HTMLTag => HTMLTag) {
  page = for (t <- page) yield munge(t)
  page = for (t <- page if t != null) yield t
  }

def process(munge: HTMLTag => HTMLTag) {
  page =
    for (t <- (for (t <- page) yield munge(t)) 
         if t != null) yield t
  }

Other Scala Data Structures

Other Scala Function Features

Summary

Personal Conclusions

Resources


This page last modified on 5 June 2008.

Creative
    Commons License