Skip to content

Instantly share code, notes, and snippets.

import scalaz._
import Scalaz.{merge => _, _}
import scalaz.concurrent._
import scalaz.stream._
import scala.concurrent.duration._
implicit val defaultScheduler = scalaz.stream.DefaultScheduler
val flag = async.signal[Boolean]
val reset = Process.eval(flag set true).drain
import sbt._
import Keys._
object SinksBuild extends Build {
// Custom keys -- things don't work well if they are on .sbt files
lazy val scalazVersion = settingKey[String]("Scalaz Version")
lazy val scodecVersion = settingKey[String]("Scodec Version")
lazy val unfilteredVersion = settingKey[String]("Unfiltered Version")
lazy val scalacheckVersion = settingKey[String]("Scalacheck Version")
lazy val specs2Version = settingKey[String]("Specs2 Version")
// javassist is used by logback, optionally. Like logback, don't pollute upstream dependencies.
"ch.qos.logback" % "logback-classic" % "1.1.1" % "optional",
import sbt.Keys._
import sbt._
import java.nio.file.Files
object TempDir extends AutoPlugin {
object autoImport {
lazy val tempDir = taskKey[File]("Sets target.temp.directory Java property. Creates the directory if needed.")
}
import autoImport._
@dcsobral
dcsobral / prompt.sbt
Created May 28, 2011 01:04
SBT prompt with project name and current git branch
shellPrompt <<= name(name => { state: State =>
object devnull extends ProcessLogger {
def info(s: => String) {}
def error(s: => String) { }
def buffer[T](f: => T): T = f
}
val current = """\*\s+(\w+)""".r
def gitBranches = ("git branch --no-color" lines_! devnull mkString)
"%s:%s>" format (
name,
@dcsobral
dcsobral / sieve.scala
Created July 5, 2011 11:52
Sieve of Eratosthenes One-Liner
(n: Int) => (2 to n) |> (r => r.foldLeft(r.toSet)((ps, x) => if (ps(x)) ps -- (x * x to n by x) else ps))
// Forward-pipe courtesy of Scalaz, or look here: http://stevegilham.blogspot.com/2009/01/pipe-operator-in-scala.html
@dcsobral
dcsobral / gist:1074264
Created July 10, 2011 04:26
error: forward reference extends over definition of value next
def iterationForCoin(stream: Stream[Int], coin: Int) = {
val (lower, higher) = stream splitAt coin
val next: Stream[Int] = lower #::: (higher zip next map { case (a, b) => a + b })
next.toList
}
val coins = List(1, 2, 5, 10, 20, 50, 100, 200)
coins.foldLeft(1 #:: Stream.fill(200)(0))(iterationForCoin).last
@dcsobral
dcsobral / DynamicImpl.scala
Created July 25, 2011 12:10 — forked from jorgeortiz85/DynamicImpl.scala
Method calls & XML traversal with Scala's new Dynamic type
class DynamicImpl(x: AnyRef) extends Dynamic {
def _select_(name: String): DynamicImpl = {
new DynamicImpl(x.getClass.getMethod(name).invoke(x))
}
def _invoke_(name: String)(args: Any*) = {
new DynamicImpl(x.getClass.getMethod(name, args.map(_.asInstanceOf[AnyRef].getClass) : _*).invoke(x, args.map(_.asInstanceOf[AnyRef]) : _*))
}
def typed[T] = x.asInstanceOf[T]
override def toString = "Dynamic(" + x.toString + ")"
}
@dcsobral
dcsobral / gist:1120811
Created August 2, 2011 18:11
Existential _ vs Higher-kind _
scala> def f[A[_] <: Seq[_]](f: A[Int]) = f.head
f: [A[_] <: Seq[_]](f: A[Int])A
scala> def f[A[_] <: Seq[t] forSome { type t }](f: A[Int]) = f.head
f: [A[_] <: Seq[_]](f: A[Int])A
scala> def f[A[t] <: Seq[_] forSome { type t}](f: A[Int]) = f.head
f: [A[t] <: Seq[_] forSome { type t }](f: A[Int])A
@dcsobral
dcsobral / gist:1121235
Created August 2, 2011 21:10
Lower priority implicits?
scala> :paste
// Entering paste mode (ctrl-D to finish)
class T[+A, +B]
trait LowerPriorityImplicits {
case object No extends T[Nothing, Nothing]
implicit val im = No
}
object T extends LowerPriorityImplicits {
implicit def toT[A, B](implicit ev: A =:= B) = new T[A, B]