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 |
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
// Based on Fantom's word count example here: http://blog.joda.org/2011/11/guide-to-evaluating-fantom.html | |
// I'm commenting the lines that need changing, and leaving a few of them uncommented, | |
// as they are the same | |
// class WordCount { | |
object WordCount { | |
// Void main(Str[] args) { | |
def main(args: Array[String]) { | |
if (args.size != 1) { |
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> Jsoup.parse("<B>bold, <I>bould italic, </b>italic, </i>normal text") | |
res0: org.jsoup.nodes.Document = | |
<html> | |
<head></head> | |
<body> | |
<b>bold, <i>bould italic, </i></b> | |
<i>italic, </i>normal text | |
</body> | |
</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 findLinks(body: String): Iterator[String] = { | |
val document = Jsoup.parse(body) | |
val links = document.select("a[href]") | |
for { | |
link <- links.iterator().asScala | |
} yield link.attr("href") | |
} |
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
/** | |
* Replaces an expression with a REPL-like evaluation of it. For example, | |
* the expression `2 + 2` would be replaced with this: | |
* | |
* {{{ | |
* scala> 2 + 2 | |
* Int: 4 | |
* }}} | |
* | |
* Clients of REPLinator *must* be compiled with -Yrangepos, until and unless |
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
object Printer { | |
import scala.reflect.runtime.currentMirror | |
import scala.tools.reflect.ToolBox | |
val toolBox = currentMirror.mkToolBox() | |
def print(s: String) = { | |
val tree = toolBox parse s | |
val typedTree = toolBox typeCheck tree | |
val tpe = typedTree.tpe.widen | |
val result = toolBox eval tree |
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
// At the external API level, use TypeTag | |
def extract[T: TypeTag](jsv: JsValue): T = | |
extractImpl[T](jsv, typeOf[T]) | |
// But use Type for everything else | |
def extractImpl[T](jsv: JsValue, tpe: Type): T = { | |
// no need for m | |
val ex = jsv match { | |
case JsNumber(n) => n | |
case JsString(s) => s |
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> type T = List[String] | |
defined type alias T | |
scala> val tpe = implicitly[TypeTag[T]].tpe.normalize | |
tpe: reflect.runtime.universe.Type = List[String] | |
scala> val tpeSymbol = tpe.typeSymbol.asType | |
tpeSymbol: reflect.runtime.universe.TypeSymbol = class List | |
scala> val args = tpeSymbol.typeParams.map(_.asType.toTypeIn(tpe)) |
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
package com.youdevise.lofty | |
import org.specs2.mutable._ | |
import scala.language.dynamics | |
trait State[S, A] { | |
val runState: Function[S, (S, A)] | |
def flatMap[B](f: A => Function[S, (S, B)]):Function[S, (S, B)] = (state:S) => { | |
val (newState, value) = runState(state) | |
f(value)(newState) |