Skip to content

Instantly share code, notes, and snippets.

View ryanlecompte's full-sized avatar

Ryan LeCompte ryanlecompte

View GitHub Profile
> 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
@ryanlecompte
ryanlecompte / gist:7062566
Last active December 26, 2015 00:09
Benchmarking WorkArrayBuffer vs. WorkList for Akka issue #1789 (https://github.com/akka/akka/pull/1789)
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]
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))
@ryanlecompte
ryanlecompte / gist:6500902
Created September 9, 2013 20:15
groupAdjacent
@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 })
@ryanlecompte
ryanlecompte / gist:6313683
Last active October 21, 2020 12:32
sequential futures!
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
@ryanlecompte
ryanlecompte / gist:6223733
Created August 13, 2013 17:47
md5 timing
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
@ryanlecompte
ryanlecompte / gist:6085895
Created July 26, 2013 03:23
My solution to a Scala programming problem by codility
// 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 += _ }
@ryanlecompte
ryanlecompte / gist:6017746
Last active December 19, 2015 21:08
Finding the number of "monotonicity flips" in a Seq[Int] in Scala
// 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)
@ryanlecompte
ryanlecompte / gist:5988194
Last active December 19, 2015 16:59
Deeply copying arbitrarily-deep nested arrays via type classes! (thanks for the typeclass idea from Erik Osheim)
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
}
@ryanlecompte
ryanlecompte / gist:5971428
Created July 11, 2013 00:20
non-blocking filter of futures
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 } }