Skip to content

Instantly share code, notes, and snippets.

View jedws's full-sized avatar

Jed Wesley-Smith jedws

View GitHub Profile
@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
}
/**
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 / 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
@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 / 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 / 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 / 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 / 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
package com.fayi.ftp
object CSVParser {
val ADDRESS = "ftp://mirrors.kernel.org/gnu/README.DESCRIPTIONS"
val ADDRESS2 = "ftp://ftp.gnu.org/README"
def main(args: Array[String]) {
def parse(s: Stream[String]) = {
val cells = (_: String).split(",")
trait Higher[F[_]]
trait Box[A]
object Box {
implicit def HigherBox = new Higher[Box] {}
}
object Foo {
val box = implicitly[Higher[Box]] // compiles fine !!!