This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** the original Deferred (similar to jQuery) based implementation with lots of mutation */ | |
| public Promise<MyData> getMyDataAsync(String id) { | |
| // create a new Deferred instance to represent this operation | |
| final Deferred<MyData> deferred = Deferred.create(); | |
| // perform an async operation to get mydata, typically with a lower-level async api | |
| rawDataClient.get(id) | |
| // handle success case | |
| .done(new Effect<Map<String, Object>>() { | |
| public void handle(Map<String, Object> value) { | |
| // convert the result into the target type and resolve the deferred |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import scalaz.{ Monoid, Monad, Comonad, Traverse, Applicative } | |
| import com.twitter.util._ | |
| trait FutureInstances { | |
| implicit def futureMonoid[A](implicit A: Monoid[A]) = new Monoid[Future[A]] { | |
| override def zero: Future[A] = Future(A.zero) | |
| override def append(f1: Future[A], f2: => Future[A]): Future[A] = |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Map; | |
| import java.util.Map.Entry; | |
| import com.google.common.collect.ImmutableMap; | |
| import com.google.common.collect.ImmutableMap.Builder; | |
| import com.google.common.collect.MapDifference; | |
| import com.google.common.collect.MapDifference.ValueDifference; | |
| import com.google.common.collect.Maps; | |
| public final class Maps { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| object Maps { | |
| import scalaz._, syntax.semigroup._ | |
| def unionWithKey[A, K](f: (K, A, A) => A): (Map[K, A], Map[K, A]) => Map[K, A] = | |
| (m1, m2) => | |
| (m1 -- m2.keySet) ++ m2.map { | |
| case (k, v) => k -> (m1 get k map { f(k, v, _) } getOrElse v) | |
| } | |
| def unionWith[K, A: Semigroup]: (Map[K, A], Map[K, A]) => Map[K, A] = |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| final case class Schrödinger[+A, +B](private a: => A, private b: => B) extends Either[A, B] { | |
| private lazy val actual : Either[A, B] = | |
| if (new Random().nextBoolean()) | |
| Left(a) | |
| else | |
| Right(b) | |
| def isLeft = actual.isLeft | |
| def isRight = actual.isRight | |
| def left = actual.left |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------------------------------------------- | |
| // Framework | |
| // ------------------------------------------------------------------------------------------------- | |
| /** Simple typeclass for turning Strings into things */ | |
| trait Mapper[A] extends (String => A) | |
| /** define all the Mapper typeclass instances and generators we need */ | |
| object Mapper { | |
| implicit val StringMapper = new Mapper[String] { | |
| def apply(s: String) = s |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| object Loan { | |
| trait Close[A] { | |
| def close(a: A): Unit | |
| } | |
| object Close { | |
| private def closeQ[A: Close](a: A): Boolean = | |
| try { implicitly[Close[A]].close(a); true } | |
| catch { case _: Exception => false } | |
| implicit def Tuple2Close[A: Close, B: Close] = new Close[(A, B)] { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package whatever | |
| import com.typesafe.config.{ Config, ConfigFactory, ConfigObject, ConfigValue } | |
| import org.joda.time.DateTime | |
| trait Factory[A] { | |
| def create(c: Configuration): A | |
| } | |
| /** |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * A simple implementation of a Banker's Queue http://www.cs.cmu.edu/~rwh/theses/okasaki.pdf 3.4.2. | |
| * | |
| * Note that this implementation differs slightly from the one described in the original | |
| * paper in that the reverse operation is performed only when the out/front list is empty. | |
| * This is done purely to simplify this example implementation. | |
| */ | |
| sealed trait BankersQueue[+A] { | |
| /** The head element of the queue, or an exception if empty. */ | |
| def head: A |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| object ZipWith { | |
| implicit def pimpToZipWith[A, CC[A] <: Iterable[A]](cc: CC[A]) = new ZipWith(cc) | |
| class ZipWith[A, CC[A] <: Iterable[A]](cc: CC[A]) { | |
| import collection.generic.CanBuildFrom | |
| def zipWith[B](f: A => B)(implicit cbf: CanBuildFrom[CC[A], (A, B), CC[(A, B)]]): CC[(A, B)] = { | |
| val builder = cbf() | |
| cc foreach { a => builder += (a -> f(a)) } | |
| builder.result |