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

Scala example source code file (stringbuilder.scala)

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

app, int, int, javastringbuilder, klm, klm, list, qzv, sb, scalastringbuilder, scalastringbuilder, string, string, stringbuilder

The Scala stringbuilder.scala source code

object Test extends App {
  val str = "ABCDEFGHIJKLMABCDEFGHIJKLM"
  val surrogateStr = "an old Turkic letter: \uD803\uDC22"
  
  type SB = { 
    def indexOf(str: String): Int
    def indexOf(str: String, fromIndex: Int): Int
    def lastIndexOf(str: String): Int
    def lastIndexOf(str: String, fromIndex: Int): Int
  }
  
  import scala.collection.mutable.{ StringBuilder => ScalaStringBuilder }
  import java.lang.{ StringBuilder => JavaStringBuilder }
  
  val sbScala = new ScalaStringBuilder() append str
  val sbJava = new JavaStringBuilder() append str
  val sbs: List[SB] = List[SB](sbScala, sbJava)
  
  def sameAnswers(f: (SB) => Int) = assert(f(sbScala) == f(sbJava))

  sameAnswers(_.indexOf(""))
  sameAnswers(_.indexOf("G"))
  sameAnswers(_.indexOf("ABC"))
  sameAnswers(_.indexOf("KLM"))
  sameAnswers(_.indexOf("QZV"))
  sameAnswers(_.indexOf("LMABC"))
  sameAnswers(_.lastIndexOf(""))
  sameAnswers(_.lastIndexOf("M"))
  sameAnswers(_.lastIndexOf("ABCDEFG"))
  sameAnswers(_.lastIndexOf("KLM"))
  sameAnswers(_.lastIndexOf("QZV"))
  sameAnswers(_.lastIndexOf("GHI", 22))
  sameAnswers(_.lastIndexOf("KLM", 22))
  
  // testing that the "reverse" implementation avoids reversing surrogate pairs
  val jsb = new JavaStringBuilder(surrogateStr) reverse
  val ssb = new ScalaStringBuilder(surrogateStr) reverseContents ;
  
  assert(jsb.toString == ssb.toString)
}

Other Scala examples (source code examples)

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

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

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 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.