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

Scala example source code file (t3242.scala)

This example Scala source code file (t3242.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, array, benchmarka, exception, int, int, m, m, ok, test, unit, vector, vector

The Scala t3242.scala source code

object Test {

 def benchmarkA(num: Int) {

   type A = Int

   def updateM[M[_]](ms: M[A], update: (M[A], A)=>M[A]): M[A] = {
     var is = ms
     for (i <- 0 until num) is = update(is, i)
     is
   }

   //
   def vectorAppend: Vector[A] = updateM[Vector](Vector(), (as, a)=>{
     val v = (as :+ a)
     //println("==>append:    i: "+i1+", v: "+v)
     v
   })
   // this will crash, Vector bug!
   def vectorRemove(vec: Vector[A]): Vector[A] = updateM[Vector](vec, (as, a)=>{
     val v = (as filterNot{ _ == a})
     //val v = (is filter{ _ != i})
     //println("==>remove:    i: "+a)
     v
   })

   val ct = vectorAppend
   println(" append [num: "+num+"] vec")
   vectorRemove(ct)
   println(" remove [num: "+num+"] vec")
 } // BenchmarkA

 def comparison(num: Int): Unit = {
   for (i <- 1 until 5) benchmarkA(num*i)
   println(">> comparison done, num: "+num);
 }

 def main(args: Array[String]): Unit = {
   try {
     //createBenchmarkA(23).testRun

     comparison(200) // OK
     comparison(2000) // this will crach

   } catch {
     case e: Exception => e.printStackTrace()
   }
 }
}

Other Scala examples (source code examples)

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