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

Scala example source code file (pc-conversions.scala)

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

array, geniterable, geniterable, genseq, gentraversable, int, int, p, parallel, seq, test, test, v, v

The Scala pc-conversions.scala source code



import collection._


// test conversions between collections
object Test {
  
  def main(args: Array[String]) {
    testConversions
  }
  
  def testConversions {
    // seq conversions
    assertSeq(parallel.mutable.ParArray(1, 2, 3))
    assertSeq(parallel.mutable.ParHashMap(1 -> 2, 2 -> 3))
    assertSeq(parallel.mutable.ParHashSet(1, 2, 3))
    assertSeq(parallel.immutable.ParRange(1, 50, 1, false))
    assertSeq(parallel.immutable.ParHashMap(1 -> 2, 2 -> 4))
    assertSeq(parallel.immutable.ParHashSet(1, 2, 3))
    
    // par conversions
    assertPar(Array(1, 2, 3))
    assertPar(mutable.ArrayBuffer(1, 2, 3))
    assertPar(mutable.ArraySeq(1, 2, 3))
    assertPar(mutable.WrappedArray.make[Int](Array(1, 2, 3)))
    assertPar(mutable.HashMap(1 -> 1, 2 -> 2))
    assertPar(mutable.HashSet(1, 2, 3))
    assertPar(immutable.Range(1, 50, 1))
    assertPar(immutable.HashMap(1 -> 1, 2 -> 2))
    assertPar(immutable.HashSet(1, 2, 3))
    
    // par.to* and to*.par tests
    assertToPar(List(1 -> 1, 2 -> 2, 3 -> 3))
    assertToPar(Stream(1 -> 1, 2 -> 2))
    assertToPar(Array(1 -> 1, 2 -> 2))
    assertToPar(mutable.PriorityQueue(1 -> 1, 2 -> 2, 3 -> 3))
    assertToPar(mutable.ArrayBuffer(1 -> 1, 2 -> 2))
    assertToPar(mutable.ArraySeq(1 -> 3))
    assertToPar(mutable.WrappedArray.make[(Int, Int)](Array(1 -> 3)))
    assertToPar(mutable.HashMap(1 -> 3))
    assertToPar(mutable.HashSet(1 -> 3))
    assertToPar(immutable.HashMap(1 -> 3))
    assertToPar(immutable.HashSet(1 -> 3))
    assertToPar(parallel.mutable.ParArray(1 -> 1, 2 -> 2, 3 -> 3))
    assertToPar(parallel.mutable.ParHashMap(1 -> 2))
    assertToPar(parallel.mutable.ParHashSet(1 -> 2))
    assertToPar(parallel.immutable.ParHashMap(1 -> 2))
    assertToPar(parallel.immutable.ParHashSet(1 -> 3))
    
    assertToParWoMap(immutable.Range(1, 10, 2))
    
    // seq and par again conversions)
    assertSeqPar(parallel.mutable.ParArray(1, 2, 3))
  }
  
  def assertSeqPar[T](pc: parallel.ParIterable[T]) = pc.seq.par == pc
  
  def assertSeq[T](pc: parallel.ParIterable[T]) = assert(pc.seq == pc)
  
  def assertPar[T, P <: Parallel](xs: GenIterable[T]) = assert(xs == xs.par)
  
  def assertToPar[K, V](xs: GenTraversable[(K, V)]) {
    xs match {
      case _: Seq[_] =>
        assert(xs.toIterable.par == xs)
        assert(xs.par.toIterable == xs)
      case _ =>
    }
    
    assert(xs.toSeq.par == xs.toSeq)
    assert(xs.par.toSeq == xs.toSeq)
    
    assert(xs.toSet.par == xs.toSet)
    assert(xs.par.toSet == xs.toSet)
    
    assert(xs.toMap.par == xs.toMap)
    assert(xs.par.toMap == xs.toMap)
  }
  
  def assertToParWoMap[T](xs: GenSeq[T]) {
    assert(xs.toIterable.par == xs.toIterable)
    assert(xs.par.toIterable == xs.toIterable)
    
    assert(xs.toSeq.par == xs.toSeq)
    assert(xs.par.toSeq == xs.toSeq)
    
    assert(xs.toSet.par == xs.toSet)
    assert(xs.par.toSet == xs.toSet)
  }
  
}

Other Scala examples (source code examples)

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