This file contains hidden or 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
> console | |
Welcome to Scala version 2.10.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_51). | |
Type in expressions to have them evaluated. | |
Type :help for more information. | |
scala> import com.twitter.logging.Logger | |
import com.twitter.logging.Logger | |
scala> Logger.OFF | |
java.lang.NoClassDefFoundError: scala/collection/GenTraversableLike$class |
This file contains hidden or 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.collection.mutable | |
import scala.annotation.tailrec | |
class Entry[T](val ref: Option[T], val permanent: Boolean) { | |
var next: Entry[T] = null | |
var isDeleted = false | |
} | |
object WorkList { | |
def empty[T] = new WorkList[T] |
This file contains hidden or 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> val items = 0 to 10 | |
scala> val numGroups = 4 | |
scala> items.zipWithIndex.foldLeft(Vector.fill(numGroups)(Vector.empty[Int])) { case (acc, (item, idx)) => val i = idx % acc.size; acc.updated(i, acc(i) :+ item) } | |
Vector(Vector(0, 4, 8), Vector(1, 5, 9), Vector(2, 6, 10), Vector(3, 7)) |
This file contains hidden or 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
@tailrec | |
final def groupAdjacent[A,B]( | |
items: Seq[(A,B)], | |
acc: Seq[(A, Seq[B])] = Vector.empty | |
): Seq[(A, Seq[B])] = { | |
items match { | |
case Seq() => acc | |
case Seq(a, _*) => | |
val (same, remaining) = items.span { case (e1, _) => a._1 == e1 } | |
val updated = acc :+ (a._1, same.map { _._2 }) |
This file contains hidden or 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 performSequentially[A](items: Seq[A])(f: A => Future[Unit]): Future[Unit] = { | |
items.headOption match { | |
case Some(nextItem) => | |
val fut = f(nextItem) | |
fut.flatMap { _ => | |
// successful, let's move on to the next! | |
performSequentially(items.tail)(f) | |
} | |
case None => | |
// nothing left to process |
This file contains hidden or 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
val text = scala.util.Random.alphanumeric.take(200).mkString | |
scala> th.pbench { DigestUtils.md5Hex(text.getBytes("UTF-8")) } | |
Benchmark (5242860 calls in 7.201 s) | |
Time: 1.231 us 95% CI 1.192 us - 1.270 us (n=20) | |
Garbage: 91.17 ns (n=244 sweeps measured) | |
res8: String = ae4c94bd6787e2ba4b5402fefaf71d7d |
This file contains hidden or 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
// my solution to the sample codility problem for finding | |
// any equilibrium index in an array of ints with up to | |
// 10 million entries. Description of problem is here: | |
// http://blog.codility.com/2011/03/solutions-for-task-equi.html | |
def solution(a: Array[Int]): Int = { | |
var i = 0 | |
var current = BigInt(0) | |
var remaining = BigInt(0) | |
a.foreach { remaining += _ } |
This file contains hidden or 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
// Finds the monotonicity breaks in a numerical sequence. | |
def findMonotonicityBreaks(items: Seq[Int]): Int = { | |
def comp(op: Symbol, a: Int, b: Int) = if (op == '<=) a <= b else a >= b | |
def flip(op: Symbol) = if (op == '<=) '>= else '<= | |
@tailrec | |
def detect(pairs: Seq[(Int, Int)], op: Symbol, breaks: Int): Int = { | |
pairs match { | |
case Seq() => breaks | |
case Seq((a, b), rest @ _*) if comp(op, a, b) => detect(rest, op, breaks) |
This file contains hidden or 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
trait Nested[A] { | |
def length(a: A): Int | |
def clone(a: A): A | |
} | |
object Nested { | |
implicit def nested[A] = new Nested[A] { | |
def length(a: A): Int = 1 | |
def clone(a: A): A = a | |
} |
This file contains hidden or 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> val nums = 0 to 10 | |
res17: scala.collection.immutable.Range.Inclusive = Range(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) | |
scala> def lookup(id: Int): Future[Option[Int]] = future { if (Random.nextBoolean) Some(5) else None } | |
lookup: (id: Int)scala.concurrent.Future[Option[Int]] | |
scala> val collapsed = Future.sequence(nums.map { n => lookup(n).map { (n, _) } }) | |
reduced: scala.concurrent.Future[scala.collection.immutable.IndexedSeq[(Int, Option[Int])]] = scala.concurrent.impl.Promise$DefaultPromise@5f0900d2 | |
scala> val filtered = collapsed.map { results => results.collect { case (i, opt) if opt.isDefined => i } } |