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
import com.mongodb.casbah.Imports._ | |
val conn = MongoConnection() | |
val db = conn("test") | |
val coll = db("foo") | |
for (x <- coll.find(MongoDBObject("_id" -> new ObjectId("4cd2d357c575954934c5a975")), | |
MongoDBObject("x" -> 1))) println(x) |
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
/** | |
* <b>Fixed Point Combinator is:</b> | |
* Y = λf.(λx.f (x x)) (λx.f (x x)) | |
* | |
* <b>Proof of correctness:</b> | |
* Y g = (λf . (λx . f (x x)) (λx . f (x x))) g (by definition of Y) | |
* = (λx . g (x x)) (λx . g (x x)) (β-reduction of λf: applied main function to g) | |
* = (λy . g (y y)) (λx . g (x x)) (α-conversion: renamed bound variable) | |
* = g ((λx . g (x x)) (λx . g (x x))) (β-reduction of λy: applied left function to right function) | |
* = g (Y g) (by second equality) [1] |
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
trait Enum { //DIY enum type | |
import java.util.concurrent.atomic.AtomicReference //Concurrency paranoia | |
type EnumVal <: Value //This is a type that needs to be found in the implementing class | |
private val _values = new AtomicReference(Vector[EnumVal]()) //Stores our enum values | |
//Adds an EnumVal to our storage, uses CCAS to make sure it's thread safe, returns the ordinal | |
private final def addEnumVal(newVal: EnumVal): Int = { import _values.{get, compareAndSet => CAS} | |
val oldVec = get |
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 MMethod[M, A1, A2, R](f: (A1, A2) => R) { | |
def apply(a1: A1, a2: A2): R = f(a1, a2) | |
} | |
class MultiMethod { | |
def apply[A1, A2, R](a1: A1, a2: A2) | |
(implicit mm: MMethod[this.type, A1, A2, R]): R = | |
mm(a1, a2) | |
// use a singleton type to attach the resulting implicit to our multimethod |
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
/** | |
* |@| is a helper function that helps you accumulate applicative functors. It gives you an ApplicativeBuilder (it's part of | |
* the implementation though) through which you accumulate your applicatives just as you would using the Builder pattern of | |
* the GoF in the OO world. Once you have all the applicatives you can pass it a function that will be applied to all the | |
* values that you have accumulated so far. e.g. | |
*/ | |
scala> (1.some |@| 2.some) apply {_ + _} | |
res1: Option[Int] = Some(3) |
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
/** | |
* A silly example using Kleisli composition of DB operations | |
* Based on an idea from Runar Bjarnason found here: | |
* https://groups.google.com/d/msg/scala-debate/xYlUlQAnkmE/FteqYKgo2zUJ | |
* | |
* Uses Scalaz7 | |
* | |
* @author <a href="mailto:[email protected]">Jim Powers</a> | |
*/ | |
object Monadic { |
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
Latency Comparison Numbers (~2012) | |
---------------------------------- | |
L1 cache reference 0.5 ns | |
Branch mispredict 5 ns | |
L2 cache reference 7 ns 14x L1 cache | |
Mutex lock/unlock 25 ns | |
Main memory reference 100 ns 20x L2 cache, 200x L1 cache | |
Compress 1K bytes with Zippy 3,000 ns 3 us | |
Send 1K bytes over 1 Gbps network 10,000 ns 10 us | |
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD |
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
import scala.concurrent._ | |
import scala.collection.mutable.Builder | |
import scala.collection.generic.CanBuildFrom | |
import language.higherKinds | |
/** | |
* Linearize asynchrnously applies a given function in-order to a sequence of values, producing a Future with the result of the function applications. | |
* Execution of subsequent entries will be aborted if an exception is thrown in the application of the function. | |
*/ | |
def linearize[T, U, C[T] <: Traversable[T]](s: C[T])(f: T => U)(implicit cbf: CanBuildFrom[C[T], U, C[U]], e: ExecutionContext): Future[C[U]] = { |
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
import scalaz._ | |
import Scalaz._ | |
import scalaz.iteratee._ | |
import EnumeratorT._ | |
def counter[A, F[+_]: Pointed] = | |
IterateeT.fold[A, F, Int](0) { (acc: Int, a: A) => acc + 1 } | |
def count[A, F[+_]: Monad](e: EnumeratorT[A, F]) = | |
(counter[A, F] &= e).run |
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 "twitter" | |
require "json" | |
# things you must configure | |
TWITTER_USER = "your_username" | |
MAX_AGE_IN_DAYS = 1 # anything older than this is deleted | |
# get these from dev.twitter.com | |
CONSUMER_KEY = "your_consumer_key" |
OlderNewer