Skip to content

Instantly share code, notes, and snippets.

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
@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 _)
@dcsobral
dcsobral / WordCount.scala
Created November 25, 2011 13:00
WordCount
// 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) {
@dcsobral
dcsobral / gist:7890914
Created December 10, 2013 13:55
JSoup demo
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>
@dcsobral
dcsobral / Getter.scala
Last active December 30, 2015 21:49
Anternative findLinks for coursera Reactive
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")
}
@dcsobral
dcsobral / REPLinator.scala
Last active December 22, 2015 20:39
Macro that replaces an expression with a REPL-like (minus REPL's "toString") evaluation of the expression.
/**
* 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
@dcsobral
dcsobral / Printer.scala
Created September 10, 2013 19:15
Prints a string as if it was produced by REPL. However, it has none of the magic the REPL has for printing types like arrays, or using quotes for empty strings on collections, etc.
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
// 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
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))
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)