Skip to content

Instantly share code, notes, and snippets.

View ryanlecompte's full-sized avatar

Ryan LeCompte ryanlecompte

View GitHub Profile
@ryanlecompte
ryanlecompte / gist:ec2d9f9e23e09c08903c
Last active September 9, 2019 01:31
Magnet/Typeclass pattern example
// 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]] {
@ryanlecompte
ryanlecompte / gist:11335327
Last active August 29, 2015 14:00
Lazy Monoid Map
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
@ryanlecompte
ryanlecompte / gist:9745983
Created March 24, 2014 18:18
lazily recurse files in a root directory
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 {
@ryanlecompte
ryanlecompte / gist:9319804
Last active August 29, 2015 13:56
Immutable binary search trees
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)
@ryanlecompte
ryanlecompte / gist:8818143
Created February 5, 2014 06:02
Option.collect, similar to Future.collect
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)
@ryanlecompte
ryanlecompte / gist:7479343
Created November 15, 2013 04:58
thyme comparisons
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"
@ryanlecompte
ryanlecompte / gist:7287415
Last active December 27, 2015 07:18
Immutable PrefixMap
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)
*/
@ryanlecompte
ryanlecompte / gist:7257010
Created October 31, 2013 20:55
::: generally better for List[A]
➜ 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)
@ryanlecompte
ryanlecompte / gist:7149289
Created October 25, 2013 04:00
future flatmapping with no stack blowing
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))
@ryanlecompte
ryanlecompte / gist:7140499
Created October 24, 2013 16:33
scala 2.10 type erasure
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"))