JAVA
List<Integer> even = new ArrayList<Integer>();
for (String num : numbersAsStrings) {
try {
int parsedInt = Integer.parseInt(num);
if (parsedInt % 2 == 0) {
ints.add(parsedInt);
}
JAVA
List<Integer> even = new ArrayList<Integer>();
for (String num : numbersAsStrings) {
try {
int parsedInt = Integer.parseInt(num);
if (parsedInt % 2 == 0) {
ints.add(parsedInt);
}
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 = |
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] { |
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) |
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) | |
} |
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) | |
/** |
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] |
#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)
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). |
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) | |
} |