This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + ")" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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] = { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 _) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
OlderNewer