Skip to content

Instantly share code, notes, and snippets.

View dacr's full-sized avatar
🕷️

Crosson David dacr

🕷️
View GitHub Profile
@dacr
dacr / Primes.sc
Last active August 29, 2015 13:55
Generic primes generator
package experiments
object Primes {
class Generator[NUM](implicit numops: Integral[NUM]) {
import annotation.tailrec
import numops._
private val two = one + one
@dacr
dacr / ActorsPrimesGenerator.scala
Last active August 29, 2015 13:56
Akka actors based, generic prime calculation parallel algorithm
package fr.janalyse.primes
import akka.actor._
import ActorDSL._
import akka.routing.SmallestMailboxRouter
import scala.collection.immutable.Queue
class ActorsPrimesGenerator[NUM](
name: String = "DefaultPrimesGeneratorSystem",
startFrom: NUM = 2,
def addLibraryPath(pathToAdd: String) {
import java.lang.reflect.Field
val usrPathsField: Field = classOf[ClassLoader].getDeclaredField("usr_paths")
usrPathsField.setAccessible(true)
val paths = usrPathsField.get(null).asInstanceOf[Array[String]]
if (!paths.contains(pathToAdd)) {
usrPathsField.set(null, paths :+ pathToAdd)
}
def res2file(from: String, dest: File) {
import scalax.io._
val in = Resource.fromInputStream(getClass().getClassLoader.getResourceAsStream(from))
val output: Output = Resource.fromFile(dest)
output.write(in.bytes)
}
scala> def f(x:Int) = {println(x); x+1}
f: (x: Int)Int
scala> val bad = Stream(f(1), f(2), f(3), f(4))
1
2
3
4
bad: scala.collection.immutable.Stream[Int] = Stream(2, ?)
scala> def testA:Boolean = false
testA: Boolean
scala> def testB:Boolean=true
testB: Boolean
scala> def testC:Boolean={println("oups"); false}
testC: Boolean
scala> if ((testA#::testB#::testC#::Stream.empty).exists(_ == true)) println("OK")
scala> def from1:Option[String]=None
from1: Option[String]
scala> def from2:Option[String]=Some("found")
from2: Option[String]
scala> def from3:Option[String]={println("oups"); Some("found again")}
from3: Option[String]
scala> val alts=from1#::from2#::from3#::Stream.empty
scala> if (from1.isDefined) from1 else
| if (from2.isDefined) from2 else
| if (from3.isDefined) from3 else
| Some("Default Value")
res5: Option[String] = Some(found)
scala> from1 match {
| case x if x.isDefined => x
| case _ =>
| from2 match {
@dacr
dacr / gist:9e7d034f8ffa72f7be5f
Created January 21, 2015 22:59
Akka actors primes computation
class ActorsPrimesGenerator[NUM](
handler: CheckedValue[NUM] => Unit = ActorsPrimesGenerator.mkPrintHandler[NUM](),
name: String = "DefaultActordBasedPrimesGeneratorSystem",
startFrom: NUM = 2,
primeNth: NUM = 1,
notPrimeNth: NUM = 1)(implicit numops: Integral[NUM]) extends PrimesDefinitions[NUM] with Shutdownable {
import numops._
/*
* To minimize the amount of messages used for back pressure regulation
@dacr
dacr / gist:3af132b5ed23b47c00a2
Created January 21, 2015 23:03
Akka-stream actors primes computation
class StreamBasedPrimesGenerator[NUM](
handler: CheckedValue[NUM] => Unit = StreamBasedPrimesGenerator.defaultHandlerFactory[NUM](),
name: String = "DefaultStreamBasedPrimesGeneratorSystem",
startFrom: NUM = 2,
primeNth: NUM = 1,
notPrimeNth: NUM = 1)(implicit numops: Integral[NUM]) extends PrimesDefinitions[NUM] with Shutdownable {
import numops._
implicit val system = ActorSystem(name)
import system.dispatcher