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

Scala example source code file (ReducePrime.scala)

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

companion, havingresult, int, int, intops, intwrap, intwrap, intwrapcreator, list, reduceprime, reduceprime, resettable, string, string

The Scala ReducePrime.scala source code

package scala.collection.parallel.benchmarks.parallel_array


import scala.collection.parallel.benchmarks._


object IntWrapCreator extends (Int => IntWrap) {
  def apply(idx: Int) = new IntWrap(shiftaround(idx))
  def shiftaround(idx: Int) = idx * 40192 + 717
}

case class IntWrap(val num: Int)

object IntOps {
  val op = (a: IntWrap, b: IntWrap) => primereduce(a, b)
  val reducer = new extra166y.Ops.Reducer[IntWrap] {
    def op(a: IntWrap, b: IntWrap) = primereduce(a, b)
  }
  
  def primereduce(a: IntWrap, b: IntWrap) = {
    val check = (checkPrime(a.num), checkPrime(b.num))
    if (a.num > b.num) a else b
  }
  
  def checkPrime(n: Int) = {
    var isPrime = true
    var i = 2
    val until = scala.math.sqrt(n).toInt + 1
    while (i < until) {
      if (n % i == 0) isPrime = false
      i += 1
    }
    isPrime
  }
}

class ReducePrime(sz: Int, p: Int, what: String)
extends Resettable[IntWrap](sz, p, what, IntWrapCreator, new Array[Any](_), classOf[IntWrap])
with HavingResult[IntWrap] {
  def companion = ReducePrime
  
  def runseq = runresult = sequentialReduce(IntOps.op, sz, new IntWrap(0))
  def runpar = runresult = pa.reduce(IntOps.op)
  def runjsr = runresult = jsrarr.reduce(IntOps.reducer, new IntWrap(0))
  override def comparisonMap = collection.Map("jsr" -> runjsr _)
}

object ReducePrime extends Companion {
  def benchName = "reduce-prime";
  def apply(sz: Int, p: Int, what: String) = new ReducePrime(sz, p, what)
  override def comparisons = List("jsr")
  override def defaultSize = 100
}












Other Scala examples (source code examples)

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