Skip to content

Instantly share code, notes, and snippets.

@Mortimerp9
Mortimerp9 / TimeTable.scala
Created January 30, 2014 00:36
Store twitter Time in Slick 2 tables.
import scala.slick.driver....simple._
import com.twitter.util.Time
class TimeTable(tag: Tag) extends Table[(Int, Time)](tag, "timetable") {
implicit val twitterTimeMap = MappedColumnType.base[Time, Long](
{ time => time.inMillis },
{ millis => Time.fromMilliseconds(millis) }
)
@Mortimerp9
Mortimerp9 / ScalaJson.scala
Created January 11, 2014 14:38
Jackson, only extract a single field from a json inputstream/string in scala, without parsing everything. I often deserialize whole objects with jackson to only access a field out of the json. This doesn't seem very efficient, Jackson provides a streaming API that can be used to make it a bit faster.
import com.fasterxml.jackson.core._
object ScalaJson {
lazy val factory = new JsonFactory()
implicit class JsonString(json: String) {
def jsonField(name: String) = ScalaJson.getFieldOnly(name, json)
}
case object IdentifyTimeout
class MyActor extends Actor {
var driver: ActorRef = null
val identifyId = //get a unique identifyId for that driverUrl
override def preStart() {
val driverSel = context.actorSelection(driverUrl)
driverSel ! Identify(identifyId)
class MyActor extends Actor {
var driver: ActorRef = null
val identifyId = //get a unique identifyId for that driverUrl
override def preStart() {
val driverSel = context.actorSelection(driverUrl)
driverSel ! Identify(identifyId)
}
@Mortimerp9
Mortimerp9 / gist:6952000
Created October 12, 2013 16:33
An alternative Factory pattern in Scala to compare with the one presented in [Design Patterns in Scala](http://pavelfatin.com/design-patterns-in-scala/). This one involves a bit more boilerplate, but will not compile if you ask for an unknown type and is easier to extend.
trait Animal
private class Dog extends Animal
private class Cat extends Animal
trait Wanna
case object WannaCat extends Wanna
case object WannaDog extends Wanna
trait AnimalFactory[Wanna] {
def create(): Animal
@Mortimerp9
Mortimerp9 / gist:6428577
Last active December 22, 2015 06:08
Because in scala `Try` is a kind of `Future` that doesn't run on a separate thread
import scala.util.{Failure, Success, Try}
import scala.concurrent.future
implicit def tryToFuture[T](t: Try[T]): Future[T] = t match {
case Success(s) => Future.successful(s)
case Failure(t) => Future.failed(t)
}
@Mortimerp9
Mortimerp9 / AkkaCompat.scala
Last active December 22, 2015 06:08
Because sometimes you are running an "old" version of Akka with a Scala version providing its own Future (>2.9.3, >2.10), then you need to juggle between one and the other. There are implicits to convert Akka and Scala Futures one to the other
import scala.concurrent.duration.{Duration => ScalaDuration}
import scala.concurrent.{ExecutionContext => ScalaExecutionContext, Future => ScalaFuture, Promise => ScalaPromise}
import scala.util.{Success, Failure}
import akka.dispatch.{ExecutionContext => AkkaExecutionContext, Future => AkkaFuture, Promise => AkkaPromise}
import akka.util.{Duration => AkkaDuration}
/**
* Some compatibility implicit conversions to deal with akka as if it might already be a newer version
* (using scala interfaces instead of akka).
@Mortimerp9
Mortimerp9 / Memoizer.md
Created May 31, 2013 18:22
As an exercise, I was trying to implement a memoization of single parameter functions with a bounded cache size; a very simple approach with a First In First Out strategy but with a good concurrent behaviour. It's probably not perfect, let me know what you think of it.

#Bounded Memoizer

As an exercise, I was trying to implement a memoization of single parameter functions with a bounded cache size; a very simple approach with a First In First Out strategy. It's probably not perfect, let me know what you think of it.

The specs are that the memoizer should be concurrent and kick out the "oldest" computation. For instance, with a maximum size of 2 slots in the cache:

  foo(0) //computes and cache the result for 0
  foo(1) //computes and cache the result for 1
  foo(0) // returns the cached result
 foo(2) // computes and cache the result for 2 and kicks out the result for 0)
@Mortimerp9
Mortimerp9 / gist:5649109
Created May 25, 2013 13:50
"cheap" implementation of an immutable IndexedSeq backed by an array. `b` looks like an Array according to the types, but can't be modified nor cast back to a mutable Array.
import scala.reflect.ClassTag
import scala.collection.mutable.WrappedArray
import scala.collection.mutable.ArrayLike
def ASeq[T](elt: T*)(implicit ct: ClassTag[T]): IndexedSeq[T] = {
val a = elt.toArray.clone
a.deep.asInstanceOf[IndexedSeq[T]]
}
val a = Array(1,2,3) //> a : Array[Int] = Array(1, 2, 3)
@Mortimerp9
Mortimerp9 / Retry.scala
Last active July 3, 2022 22:35
A retry implementation for Scala, a bit of explanations here: http://pierreandrews.net/posts/retry-fail-scala.html
import scala.concurrent.Await
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import scala.concurrent.blocking
import scala.concurrent.duration.Deadline
import scala.concurrent.duration.Duration
import scala.concurrent.duration.DurationInt
import scala.concurrent.duration.DurationLong
import scala.concurrent.future
import scala.concurrent.promise