Skip to content

Instantly share code, notes, and snippets.

@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:1112401
Created July 28, 2011 19:55
Faster char split
// Since this link got passed around some, I'm going to paste here the
// versions made by sirthias. If you want to see the old ones,
// look up the history.
// Well, actually, these are non-pimped versions. See the comments to
// a link where they are provided as pimped methods on String.
import scala.annotation.tailrec
def fastSplit(s: String, delimiter: Char): List[String] = {
@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]
@dcsobral
dcsobral / gist:1125284
Created August 4, 2011 14:30
Generating strings from a regex
import scala.util.regexp._
import scala.util.automata._
object MyLang extends WordExp {
type _regexpT = RegExp
type _labelT = MyChar
case class MyChar(c:Char) extends Label
}
import MyLang._
object MyBerriSethi extends WordBerrySethi {
@dcsobral
dcsobral / BenchCode.scala
Created August 17, 2011 21:00
Benchmark code for algorithms in this Stack Overflow question regarding Java vs Scala performance: http://stackoverflow.com/q/7084212/53013
// Question at http://stackoverflow.com/q/7084212/53013
// Results at https://gist.github.com/1152764
import scala.collection.mutable.{ArrayBuffer, ListBuffer}
import scala.util.Random
object BenchCode extends scala.testing.Benchmark with Algorithms {
var xs: List[Int] = Nil
var acc = 0
var code: List[Int] => List[Int] = (identity _)
dcs@ayanami:~/tmp$ scala BenchCode 100000 5 100
Warming up Adrian, functional/indexOf
Warming up Adrian, functional/span
Warming up Adrian, imperative/ArrayBuffer
Warming up Adrian, imperative/ListBuffer
Warming up user unknown, recursive
Warming up user unknown, fold
Warming up paradigmatic, recursive
Warming up paradigmatic, fold
Warming up soc