Skip to content

Instantly share code, notes, and snippets.

Ryan LeCompte ryanlecompte

Block or report user

Report or block ryanlecompte

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@ryanlecompte
ryanlecompte / gist:ec2d9f9e23e09c08903c
Last active Sep 9, 2019
Magnet/Typeclass pattern example
View gist:ec2d9f9e23e09c08903c
// 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 Aug 29, 2015
Lazy Monoid Map
View gist:11335327
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 Mar 24, 2014
lazily recurse files in a root directory
View gist:9745983
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 Aug 29, 2015
Immutable binary search trees
View gist:9319804
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 Feb 5, 2014
Option.collect, similar to Future.collect
View gist:8818143
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
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 Dec 27, 2015
Immutable PrefixMap
View gist:7287415
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 Oct 31, 2013
::: generally better for List[A]
View gist:7257010
➜ 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 Oct 25, 2013
future flatmapping with no stack blowing
View gist:7149289
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 Oct 24, 2013
scala 2.10 type erasure
View gist:7140499
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"))
You can’t perform that action at this time.