This file contains hidden or 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
object GOption { | |
def some[A](a: A): GOption[A] = new GOption[A] { | |
def cata[B](n: => B, s: A => B): B = sys.error("Implement me") | |
} | |
def none[A]: GOption[A] = new GOption[A] { | |
def cata[B](n: => B, s: A => B): B = sys.error("Implement me") | |
} | |
} | |
trait GOption[+A] { |
This file contains hidden or 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
object SalaryWorld extends App { | |
val rules = Seq( | |
('allowance, 1.2), | |
('bonus, 1.1), | |
('tax, 0.7), | |
('surcharge, 0.9) | |
) | |
def salary(config: Set[Symbol], base: Double) = |
This file contains hidden or 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
class FizzBuzz | |
private | |
@@pairs = [ | |
[3, "fizz"], | |
[5, "buzz"] | |
] | |
def self.strings_for(n) |
This file contains hidden or 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 'net/http' | |
def resolve(uri_str,limit=5) | |
$stderr.puts("Resolving #{uri_str}") | |
limit.times do |i| | |
uri = URI.parse(uri_str) | |
raise "No host given #{uri_str}" unless uri.host | |
http = Net::HTTP.new(uri.host, uri.port) | |
request = Net::HTTP::Head.new(uri.request_uri) | |
response = http.request(request) |
This file contains hidden or 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
class Array | |
# split array into groups up to n items, randomly choosing the size. | |
def split_random(n) | |
size == 0 ? self : (size == 1 ? [self] : self[1..-1].reduce([[self[0]]]){|ll, elt| (rand(n)==(n-1) || ll[-1].size==n) ? ll + [[elt]] : ll[0..-2] + [ll[-1] +[elt]]}) | |
end | |
end |
This file contains hidden or 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
class Array | |
def split_random(n) | |
def td(n) | |
[take(n),drop(n)] | |
end | |
def split_random_acc(n,l, acc) | |
head,tail = l.td(rand(n)+1) | |
return (acc + [head]) if tail==[] | |
split_random_acc(n, tail, acc + [head]) |
This file contains hidden or 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
/** | |
* From a file that contains | |
* doc_id w1 w2 w3 ... lines, separated by tabs | |
* return an inverted index Map of w -> Set(doc_id) | |
* | |
* @param filename well isn't it obvious | |
* @return Map[String,Set[String]] | |
*/ | |
import scala.collection.immutable.Map |
This file contains hidden or 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
/** | |
* From a file that contains | |
* doc_id w1 w2 w3 ... lines, separated by tabs | |
* return an inverted index Map of w -> Set(doc_id) | |
* | |
* @param filename well isn't it obvious | |
* @return Map[String,Set[String]] | |
*/ | |
import scala.collection.immutable.Map |
This file contains hidden or 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> new BasicDBObject().get("missing") => java.lang.Object = null | |
scala> new BasicDBObject().getBoolean("missing") => Boolean = false | |
scala> new BasicDBObject().getDouble("missing") throws java.lang.NullPointerException | |
scala> new BasicDBObject().getInt("missing") throws java.lang.NullPointerException | |
scala> new BasicDBObject().getLong("missing") throws java.lang.NullPointerException | |
scala> new BasicDBObject().getString("missing") => java.lang.String = null | |
This file contains hidden or 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 'rubygems' | |
require 'tweetstream' | |
require 'date' | |
TweetStream.configure do |config| | |
config.consumer_key = '<your key>' | |
config.consumer_secret = '<your secret>' | |
config.oauth_token = '<your token>' | |
config.oauth_token_secret = '<your token secret>' | |
config.auth_method = :oauth |