Skip to content

Instantly share code, notes, and snippets.

@piotrga
piotrga / gist:7779478
Last active December 30, 2015 04:59
Naming test data for better error messages
class DatesTest extends FreeSpec with Matchers{
import java.util.Date
import scala.concurrent.duration._
implicit class RichDate(d:Date){
def -(duration: Duration) = new Date(d.getTime - duration.toMillis)
def +(duration: Duration) = new Date(d.getTime + duration.toMillis)
def as(name: String) = new Date(d.getTime){
override def toString = s"[$name]"
}
@piotrga
piotrga / gist:5928581
Created July 4, 2013 15:20
Scala 2.10 ReflectionSugars
trait ReflectionSugars{
import scala.reflect.runtime.{universe => ru}
private lazy val universeMirror = ru.runtimeMirror(getClass.getClassLoader)
def companionOf[T](implicit tt: ru.TypeTag[T]) = {
val companionMirror = universeMirror.reflectModule(ru.typeOf[T].typeSymbol.companionSymbol.asModule)
companionMirror.instance
}
}
@piotrga
piotrga / Json.scala
Last active April 4, 2018 07:42
The simplest dynamically typed json parsing with Dynamic in Scala 2.10
import util.parsing.json.JSON
import io.Source
import scala.language.dynamics
object Example extends App{
val json = """{
"name" : "Adam Slodowy",
"active": "true",
"roles" : [ "teacher", "admin" ],
@piotrga
piotrga / Http.scala
Created March 24, 2013 20:00
The simplest Async Scala Http Client for 2.10
import concurrent.{Await, ExecutionContext, Future}
import java.net.{HttpURLConnection, URL}
import io.Source
import concurrent.duration.FiniteDuration
case class HttpResponse(code : Int, body: String, headers: Map[String, List[String]])
case class RequestBody(body: String, contentType: String = "text/plain")
object RequestBody{
@piotrga
piotrga / gist:4044645
Created November 9, 2012 09:07
Amazon DyanmoDB client WTF?
if (clientConfiguration.getMaxErrorRetry() == ClientConfiguration.DEFAULT_MAX_RETRIES) {
log.debug("Overriding default max error retry value to: " + 10);
clientConfiguration.setMaxErrorRetry(10);
}
@piotrga
piotrga / gist:2869168
Created June 4, 2012 15:53
KMeans in Octave
function [cent, idx, sizes, distortion]=km(data, k, maxIter)
N = size(data,1);
COL = size(data,2);
cent = data(randperm(N)(1:k), : );
#cent = rand(k, COL) * 0.5 + 0.5
for iter = 1 : maxIter
fprintf("Iteration %d\n", iter);
dist = [];
@piotrga
piotrga / gist:2423885
Created April 19, 2012 20:17
akka-camel producer/consumer
import akka.camel.{Consumer, CamelMessage}
import akka.actor.{Props, ActorSystem}
import akka.util.Timeout
import akka.util.duration._
import akka.pattern.ask
import scaladays2012.{Email, EmailerConfig, Emailer}
class HttpConsumerExample extends Consumer{
def endpointUri = "jetty://http://0.0.0.0:1111/demo"
@piotrga
piotrga / gist:1986175
Created March 6, 2012 13:07
Atomic Sugar
import annotation.tailrec
import java.util.concurrent.atomic.AtomicReference
object Atomic {
def apply[T]( obj : T) = new Atomic(new AtomicReference(obj))
implicit def toAtomic[T]( ref : AtomicReference[T]) : Atomic[T] = new Atomic(ref)
}
class Atomic[T](val atomic : AtomicReference[T]) {
@tailrec
@piotrga
piotrga / gist:1864064
Created February 19, 2012 14:29
Performance test
package com.XXX.connector
import testdsl.connector.ConnectorAccountDsl
import testdsl.facebook.FacebookAccountDsl
import util.Random
import akka.actor.{Scheduler, Actor}
import java.util.concurrent.TimeUnit
import org.junit.Test
@piotrga
piotrga / gist:1844652
Created February 16, 2012 12:55
Try with result
object Try {
/**
* Tries to execute body.
*
* Example below tries to start template and if it's unsuccessful it stops context.
* <pre> Try(template.start()) otherwise context.stop() </pre>
*
* @param body block of code to execute.
* @return Ok, if no exception is thrown by body.