Skip to content

Instantly share code, notes, and snippets.

View ryanlecompte's full-sized avatar

Ryan LeCompte ryanlecompte

View GitHub Profile
@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: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: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: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:891728
Created March 29, 2011 02:52
BUG with Ruby 1.9.2 (MRI)
# The following fails with a failure to call "x=" private method
String.send(:attr_accessor, :x)
s = ""
s.x = 100
# The following works:
class String
self.send(:attr_accessor, :x)
end
s = ""
>> module M
>> x = 100
>> define_method(:x=) {|v| x = v }
>> define_method(:x) { x }
>> end
=> #<Proc:0x0000010102dfd0@(irb):4 (lambda)>
>> class A
>> include M
>> end
=> A
@ryanlecompte
ryanlecompte / gist:904220
Created April 5, 2011 18:42
simple image retriever example
require 'open-uri'
url = 'http://www.hobotraveler.com/uploaded_images/207-249-tiger-nut-efio-togo-food-798405.jpg'
f = File.open('image.jpg', 'w')
open(url) { |io| f.puts(io.read) }
f.close
def m1
commands = {:exit => Proc.new { break }}
loop do
commands[:exit].call
end
puts "Done!"
end
m1
ruby-1.9.2-head :001 > module M1
ruby-1.9.2-head :002?> def self.m1
ruby-1.9.2-head :003?> "self.m1 in M1"
ruby-1.9.2-head :004?> end
ruby-1.9.2-head :005?> end
=> nil
ruby-1.9.2-head :006 > module M2
ruby-1.9.2-head :007?> include M1
ruby-1.9.2-head :008?> def self.m2
ruby-1.9.2-head :009?> m1
@ryanlecompte
ryanlecompte / gist:952316
Created May 2, 2011 20:40
Example of flattening scopes
module M
def self.included(base)
base.class_eval do
singleton_class = (class << self; self; end)
singleton_class.send(:define_method, :foo) do
puts "in foo! base is #{base}"
end
end
end
end