Skip to content

Instantly share code, notes, and snippets.

@Mortimerp9
Mortimerp9 / gist:4985630
Last active December 13, 2015 22:39
Java vs Scala list operation from Raúl Raja Martínez + dealing with an exception

JAVA

List<Integer> even = new ArrayList<Integer>();
for (String num : numbersAsStrings) {
 try {
  int parsedInt = Integer.parseInt(num);
  if (parsedInt % 2 == 0) {
    ints.add(parsedInt);
 }
@Mortimerp9
Mortimerp9 / ConfigString.scala
Created March 26, 2013 20:38
Shortcuts for configuring Play2 Applications in scala
package utils
import scala.concurrent.duration.FiniteDuration
import scala.concurrent.duration._
import play.api.Application
import collection.JavaConversions._
object ConfigString {
implicit class ConfigStr(s: String) {
def configOrElse(default: FiniteDuration)(implicit app: Application): FiniteDuration =
@Mortimerp9
Mortimerp9 / asSoonAsPossibleEnumeratee
Created March 31, 2013 02:24
an experiment in pushing the next Future that is ready in an enumerator to serve results as they are computed with Play Framework.
import scala.concurrent._
import scala.concurrent.{ Future, Await }
import scala.util.Random
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import scala.util.{ Try, Success, Failure }
import scala.concurrent.duration._
import play.api.libs.iteratee._
def takeOrDeadline[E](count: Int, deadline: Deadline): Enumeratee[E, E] = new Enumeratee.CheckDone[E, E] {
@Mortimerp9
Mortimerp9 / gist:5364769
Created April 11, 2013 16:13
example custom matcher
trait ID
case class SequentialID(id: Int) extends ID
case class OtherID(id: Int) extends ID
case class User(val id: ID, val b: Int, val c: Int)
object UserId {
def unapply(w: User) = w match {
case u @ User(SequentialID(id), _, _) => Some(u, id)
@Mortimerp9
Mortimerp9 / variantworksheet.scala
Last active December 16, 2015 04:39
Sample code for the following post https://coderwall.com/p/pdrz7q
object test {
//the companion object, with functions to help in creating readers
object Reader {
implicit def reader[C, R](block: C => R) = Reader[C, R](block)
def pure[From, To](a: To) = Reader((c: From) => a)
}
@Mortimerp9
Mortimerp9 / ReaderM.scala
Created April 16, 2013 23:20
a monad transformer for Reader presented in this gist: https://gist.github.com/Mortimerp9/5400194 This is a companion code for the following post: https://coderwall.com/p/ibrhta
import scala.concurrent.Future
import scala.concurrent.ExecutionContext
/**
* a ReaderM is a tool for Readers containing other monads, such as Reader[_, Option[_]] or Reader[_, Future[_]]
*/
case class ReaderM[-C, A, M[+A]](val read: Reader[C, M[A]])(implicit canMap: CanMap[A, _, M]) {
def apply(conn: C): M[A] = read(conn)
/**
@Mortimerp9
Mortimerp9 / CanMap.scala
Created April 16, 2013 22:27
A generic implementation of Reader with a naive adhoc implementation of a Monad typeclass. This is a companiong code for this post: https://coderwall.com/p/-egcfq
import scala.collection._
import scala.collection.generic._
import scala.concurrent.{ Future, ExecutionContext }
/**
* a Typeclass representing a class that can map and flatMap (collections, Option, Future..).
* effectively, it's a Monad without enforcing the axioms of a Monad.
*/
trait CanMap[A, B, M[_]] {
def map(l: M[A])(f: A => B): M[B]
@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 / 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 / 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)
}