Skip to content

Instantly share code, notes, and snippets.

@runarorama
runarorama / gist:2030578
Created March 13, 2012 18:40 — forked from mergeconflict/pimps.scala
typeclass fu
// reifies the assertion that type A is an instance of typeclass F
final class instance[A, F[_]](value: A, typeclass: F[A]) {
def apply[B](f: (F[A], A) => B): B = f(typeclass, value)
}
// provides an implicit conversion from any A to the above "instance" wrapper
object instance {
implicit def anyToInstance[A, F[_]](a: A)(implicit fa: F[A]) = new instance(a, fa)
}
object ScalazActorTest extends App {
import scalaz._
import Scalaz._
val running = new AtomicBoolean(false)
object Actor {
var count = 0
def act() {
val b = running.get()
object Util {
def BenchmarkCycles = 100000000
def time[Z](label: String)(f: => Z): Z = {
val startTime = System.currentTimeMillis
val result = f
val endTime = System.currentTimeMillis
def lineToOffer(line: String, headers: List[String]) : Validation[NonEmptyList[String], Offer] = {
({commaSplit(_)} andThen {extractFields(_: List[String], headers)} apply line) :->
{(v) => Offer(v._1, v._2, v._3, v._4)}
}
def commaSplit(l: String): String => List[String] = l.split(",").toList
def extractFields(x: List[String], headers: List[String]): Validation[NonEmptyList[String], (String, String, String, String)] = {
notEmpty(x(headers.indexOf("offer_name")), "No offer name specified on line:" + x.mkString(","))) <|***|> (
notEmpty(x(headers.indexOf("email")), "No email specified on line:" + x.mkString(",")),
@runarorama
runarorama / DBSkeisli.scala
Created October 17, 2011 04:54 — forked from corruptmemory/DBSkeisli.scala
Silly example of Kleisli composition of DB operations
/**
* A silly example using Kleisli composition of DB operations
* Based on an idea from Runar Bjarnason found here:
* https://groups.google.com/d/msg/scala-debate/xYlUlQAnkmE/FteqYKgo2zUJ
*
* Uses Scalaz7
*
* @author <a href="mailto:jim@corruptmemory.com">Jim Powers</a>
*/
object Monadic {
@runarorama
runarorama / higher-rank polymorphic function with type bounds.scala
Created August 11, 2011 14:55 — forked from mads-hartmann/higher-rank polymorphic function with type bounds.scala
Attempt to create a higher-rank polymorphic function with type bounds
/*
I want to use a higher-rank polymorphic function when transforming an AST to generalize the
'traversal' so it can be separated from the actual transformation of each node.
This snippet of code doesn't quite capture my use case but it provokes the same compile error
as I get here: https://gist.github.com/1139579
*/
trait ~>[F[_],G[_],C[_]] {
def apply[A](a: F[A])(implicit evidence: C[F[A]]): G[A]