Skip to content

Instantly share code, notes, and snippets.

View jedws's full-sized avatar

Jed Wesley-Smith jedws

View GitHub Profile
@jedws
jedws / promiseDeferred.java
Created October 11, 2012 23:10
Promise Map
/** 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
@jedws
jedws / FutureInstances.scala
Created September 28, 2012 04:04
FutureInstances for scalaz
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] =
@jedws
jedws / Maps.java
Created August 22, 2012 04:46
Java version of the unionWith algorithm – scala version here https://gist.github.com/3412529
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 {
@jedws
jedws / Maps.scala
Created August 21, 2012 06:05
simple scala unionWith
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] =
@jedws
jedws / Schrödinger.scala
Created June 4, 2012 21:55 — forked from jroper/Schrödinger.scala
Scala would be more fun if it had one of these...
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
@jedws
jedws / scalatronFramework.scala
Created May 9, 2012 10:32
a skeleton start to scalatron framework code
// -------------------------------------------------------------------------------------------------
// 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
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)] {
@jedws
jedws / Configuration.scala
Created March 28, 2012 04:27
config kit to make extracting configuration settings easy
package whatever
import com.typesafe.config.{ Config, ConfigFactory, ConfigObject, ConfigValue }
import org.joda.time.DateTime
trait Factory[A] {
def create(c: Configuration): A
}
/**
@jedws
jedws / BankersQueue.scala
Created January 14, 2012 23:27
Data Structures - A Persistent Queue
/**
* 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
@jedws
jedws / ZipWith.scala
Created September 28, 2011 03:03
ZipWith adds a zipWith(f: A => B): C[(A, B)] to any collection
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