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) |
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) |
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 { |
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 |
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
# 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 = "" |
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
>> 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 |
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
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 |
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
def m1 | |
commands = {:exit => Proc.new { break }} | |
loop do | |
commands[:exit].call | |
end | |
puts "Done!" | |
end | |
m1 |
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
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 |
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
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 |
OlderNewer