Skip to content

Instantly share code, notes, and snippets.

View travisbrown's full-sized avatar
😎
In hiding

Travis Brown travisbrown

😎
In hiding
View GitHub Profile
import scalaz._, Scalaz._
trait Baz extends Foo {
def r = List(1).liftM[T]
}
@travisbrown
travisbrown / Typecase.scala
Created March 2, 2014 17:08
Breaking parametricity sucks!
package org.brianmckenna.wartremover
package warts
object Typecase extends WartTraverser {
def apply(u: WartUniverse): u.Traverser = {
import u.universe._
val typedFinder = new Traverser {
override def traverse(tree: Tree) {
tree match {
import scalaz._, Scalaz._, concurrent.{ Future, Task }
import argonaut._, Argonaut._
object ArgonautDemo extends JsonDemo {
val entitySum = DecodeJson(c =>
entityNames.traverseU(c.get[JsonArray](_).map(_.size)).map(_.sum)
)
val resultReads = DecodeJson(c =>
c.get[Json]("delete").map(_ => Result.deletion) ||| (
import shapeless._
trait ZipWith[HF, AL <: HList, BL <: HList] extends DepFn2[AL, BL] {
type Out <: HList
}
object ZipWith {
def apply[HF, AL <: HList, BL <: HList](implicit
zw: ZipWith[HF, AL, BL]
): Aux[HF, AL, BL, zw.Out] = zw
import scalaz._, Scalaz._, concurrent.Future
def slowInt(i: Int) = { Thread.sleep(200); i }
def slowAdd(x: Int, y: Int) = { Thread.sleep(100); x + y }
def futures = (1 to 20).map(i => Future(slowInt(i)))
def timeFuture[A](fn: => Future[A]) = {
val t0 = System.currentTimeMillis
val a = fn.run
println((System.currentTimeMillis - t0) / 1000.0 + "s")
package argonaut
import argonaut._, Argonaut._
import scala.language.experimental.macros
import scalaz._, Scalaz._
import shapeless._
object AutoCodecJson {
implicit def deriveEncodeJson[T] = macro GenericMacros.deriveProductInstance[EncodeJson, T]
implicit def deriveDecodeJson[T] = macro GenericMacros.deriveProductInstance[DecodeJson, T]
import scala.concurrent.{ ExecutionContext, Future }
import shapeless._, ops.tuple.{ IsComposite, LeftFolder, Prepend }
object zipAndAdd extends Poly2 {
implicit def only[B, A](implicit p: Prepend[B, Tuple1[A]], executor: ExecutionContext) =
at[Future[B], Future[A]] {
(fb, fa) => fb.zip(fa).map { case (b, a) => p(b, Tuple1(a)) }
}
}
@travisbrown
travisbrown / trampolined-state.scala
Created May 31, 2014 13:40
Trampolining the state monad (doesn't work)
import scalaz._, Scalaz._
def setS(i: Int): State[List[Int], Unit] = modify(i :: _)
val s = (1 to 10000).foldLeft(state[List[Int], Unit](()).lift[Free.Trampoline]) {
case (st, i) => st.flatMap(_ => setS(i).lift[Free.Trampoline])
}
s(Nil)
scala> :paste
// Entering paste mode (ctrl-D to finish)
import scala.language.dynamics
import scala.language.experimental.macros
import scala.reflect.macros.whitebox
object implicitly extends Dynamic {
def apply[T](implicit t: T): T {} = t
sealed trait A {
type Typeclass[T] <: { def apply(): Unit }
}
case class B() extends A {
trait Typeclass[T] { def apply(): Unit }
object Typeclass {
implicit val intClass: Typeclass[Int] = new Typeclass[Int] {
def apply() { println("B.Int") }