View gist:ec2d9f9e23e09c08903c
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
// Magnet pattern documented here: http://spray.io/blog/2012-12-13-the-magnet-pattern/ | |
object TypeclassExample { | |
// first, define the typeclass | |
trait Plotter[A] { | |
def plot: Unit | |
} | |
// this typeclass instance knows how to plot collections | |
class IterablePlotter[A: Numeric](it: Iterable[A]) extends Plotter[Iterable[A]] { |
View gist:11335327
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 com.twitter.algebird.Monoid | |
import scala.collection.immutable.MapLike | |
/** | |
* LazyMonoidMap is an immutable lazy map that wraps a collection of maps | |
* that all have the same types of keys/values. Values must have a corresponding | |
* monoid so that the values can be appropriately summed for a particular key that | |
* exists in more than one underlying map. This data structure is useful for | |
* those situations where you already have a collection of maps loaded in | |
* memory and you don't want to create extra garbage by eagerly merging them |
View gist:9745983
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 java.io.File | |
/** | |
* Iterate all files in the given directory recursively. | |
* @param root the root directory to traverse | |
* @return an Iterator[File] of traversed files | |
*/ | |
def listFiles(root: File): Iterator[File] = { | |
def rec(files: List[File]): Stream[File] = { | |
files match { |
View gist:9319804
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
sealed trait BST[+K, +V] | |
object BST { | |
def apply[K: Ordering, V](k: K, v: V): Node[K, V] = Node(k, v, Empty, Empty) | |
case object Empty extends BST[Nothing, Nothing] | |
case class Node[K, V](key: K, value: V, left: BST[K, V], right: BST[K, V])(implicit ord: Ordering[K]) extends BST[K, V] { | |
def get(item: K): Option[V] = search(item, this) | |
def +(kv: (K, V)): Node[K, V] = insert(kv._1, kv._2, this) |
View gist:8818143
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
implicit class RichOption(underlying: Option.type) { | |
def collect[A](opts: Seq[Option[A]]): Option[Seq[A]] = { | |
@tailrec def rec(left: Seq[Option[A]], acc: Seq[A]): Option[Seq[A]] = { | |
left match { | |
case Seq(Some(v), tail @ _*) => rec(tail, acc :+ v) | |
case Seq(None, _*) => None | |
case _ => Option(acc) | |
} | |
} | |
rec(opts, Vector.empty) |
View gist:7479343
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> val th = new ichi.bench.Thyme | |
th: ichi.bench.Thyme = ichi.bench.Thyme@105e4712 | |
scala> val upperRx = "[A-Z]".r | |
upperRx: scala.util.matching.Regex = [A-Z] | |
scala> val lc = "alllowercase" | |
lc: String = alllowercase | |
scala> val uc1 = "Ryanlecompte" |
View gist:7287415
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.collection.generic.CanBuildFrom | |
import scala.collection.immutable.MapLike | |
import scala.collection.mutable | |
/** | |
* Immutable version of the PrefixMap demonstrated in the Scala collections | |
* guide here: http://www.scala-lang.org/docu/files/collections-api/collections-impl_6.html | |
* | |
* This version also has a smarter remove method (doesn't leave behind dead nodes with empty values) | |
*/ |
View gist:7257010
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-2.10.1 bin/scala -cp "/Users/ryan/dev/research/thyme/Thyme.jar" | |
Welcome to Scala version 2.10.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_45). | |
Type in expressions to have them evaluated. | |
Type :help for more information. | |
scala> val th = new ichi.bench.Thyme | |
th: ichi.bench.Thyme = ichi.bench.Thyme@786f9982 | |
scala> val l1 = List.range(1,1000) |
View gist:7149289
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 loop(f: Future[Int]): Future[Int] = { | |
| f.flatMap { i => | |
| if (i == 1e6) Future.successful(i) | |
| else loop(future(i + 1)) | |
| } | |
| } | |
scala> val f = loop(future(1)) |
View gist:7140499
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> class Foo[+A,+B](a: A, b: B) | |
scala> def hi[A: ClassTag, B: ClassTag](f: Foo[Any,Any]) = f match { case _: Foo[Int,Int] => println("hi"); case _ => println("bye") } | |
<console>:9: warning: non-variable type argument Int in type pattern Foo[Int,Int] is unchecked since it is eliminated by erasure | |
def hi[A: ClassTag,B: ClassTag](f: Foo[Any,Any]) = f match { case _: Foo[Int,Int] => println("hi"); case _ => println("bye") } | |
^ | |
hi: [A, B](f: Foo[Any,Any])(implicit evidence$1: scala.reflect.ClassTag[A], implicit evidence$2: scala.reflect.ClassTag[B])Unit | |
scala> hi(new Foo("a","b")) |
NewerOlder