Skip to content

Instantly share code, notes, and snippets.

@piotrga
piotrga / gist:1520363
Created December 26, 2011 01:37
Matrix multiplication with parallel collections
override def multiply(m1: Array[Array[Double]], m2: Array[Array[Double]]) : Array[Array[Double]] = {
val res = Array.ofDim[Double](m1.length, m2(0).length)
val M1_COLS = m1(0).length
val M1_ROWS = m1.length
val M2_COLS = m2(0).length
@inline def singleThreadedMultiplicationFAST(start_row:Int, end_row:Int) {
var col, i = 0
var sum = 0.0
var row = start_row
@piotrga
piotrga / gist:1520404
Created December 26, 2011 02:20
Matrix multiplication with parallel collections and idiomatic scala
def multiThreadedIdiomatic(m1:Seq[Array[Double]], m2:Array[Array[Double]] ) ={
val res = Array.fill(m1.length, m2(0).length)(0.0)
for(row <- (0 until m1.length).par;
col <- (0 until m2(0).length).par;
i <- 0 until m1(0).length){
res(row)(col) += m1(row)(i) * m2(i)(col)
}
res
}
@piotrga
piotrga / gist:1528130
Created December 28, 2011 14:30
Camel integration
object Camel{
lazy val context = new DefaultCamelContext
def addRoute(routeBuilder: RouteBuilder) {context addRoutes routeBuilder}
def start { context start }
}
trait Consum{ self: Actor =>
val camel = Camel
val consumer = self
def from(route:String) = camel.addRoute(new RouteBuilder(){
@piotrga
piotrga / gist:1533450
Created December 29, 2011 10:43
Pimping the String
implicit def toRichString(s:String) : RichString = new RichString(s)
class RichString(s: String){
def saveAs(fileName: String) = write(fileName, s)
private[this] def write(fileName: String, content: String) {
val f = new FileWriter(fileName)
f.write(content)
f.close()
}
@piotrga
piotrga / gist:1533459
Created December 29, 2011 10:44
SaveAs
"My string I want to save".saveAs("some/file/name.txt")
@piotrga
piotrga / gist:1533463
Created December 29, 2011 10:45
save to file
val f = new FileWriter("some/file/name.txt")
f.write("My string I want to save")
f.close()
@piotrga
piotrga / gist:1547297
Created January 1, 2012 13:12
mockito in scala
package akka.camel
import akka.actor._
import org.scalatest.FlatSpec
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.mock.MockitoSugar
import org.mockito.Mockito._
import org.mockito.Matchers.{eq => the, any}
class ConsumerScalaTest extends FlatSpec with ShouldMatchers with MockitoSugar{
@piotrga
piotrga / gist:1570689
Created January 6, 2012 13:48
Mockito fun 1
import org.mockito.Mockito._
import org.scalatest.mock.MockitoSugar
import org.mockito.Matchers
object MockitoFun1 extends App with MockitoSugar{
trait X{
def x
}
@piotrga
piotrga / gist:1578160
Created January 8, 2012 12:15
tryAll
object ErrorUtils{
/**
* Executes a block and returns the result wrapped by Right class, or exception wrapped by Left class.
*/
def either[T](block:() => T) : Either[Throwable,T] = try {Right(block())} catch {case e => Left(e)}
/**
* Executes all blocks in order and collects exceptions. It guarantees to execute all blocks, even if some of them fail.
* It throws a BlockException, if any number of blocks fail. BlockException contains a list of thrown exceptions.
*
@piotrga
piotrga / gist:1659645
Created January 23, 2012 00:35
ProducerRegistry
private[camel] case class RegisterProducer(actorRef: ActorRef, endpointUri: String)
/**
* Watches the end of life of <code>Producer</code>s.
* Removes a <code>Producer</code> from the <code>ProducerRegistry</code> when it is <code>Terminated</code>,
* which in turn stops the <code>SendProcessor</code>.
*/
private[camel] class CamelProducerIdempotentRegistry(camelContext: CamelContext) extends Actor {
private val camelObjects = new HashMap[ActorRef, (Endpoint,SendProcessor)]()