Skip to content

Instantly share code, notes, and snippets.

View gclaramunt's full-sized avatar

Gabriel Claramunt gclaramunt

View GitHub Profile
@bwmcadams
bwmcadams / Casbah_Field_Limits.scala
Created January 3, 2011 20:06
Select a limited set of fields with Casbah
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)
@folone
folone / SCombinator.scala
Created June 30, 2011 07:19
Y-Combinator in Scala
/**
* <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]
@viktorklang
viktorklang / ScalaEnum.scala
Created June 30, 2011 23:12
DIY Scala Enums (with optional exhaustiveness checking)
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
@raichoo
raichoo / gist:1114605
Created July 29, 2011 20:01
Multiple dispatch experiment
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
@debasishg
debasishg / gist:1261857
Created October 4, 2011 14:58
a brief rant about |@| in scalaz
/**
* |@| 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)
@corruptmemory
corruptmemory / DBSkeisli.scala
Created October 17, 2011 02:43
Silly example of Kleisli composition of DB operations
/**
* 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:jim@corruptmemory.com">Jim Powers</a>
*/
object Monadic {
@jboner
jboner / latency.txt
Last active July 26, 2024 04:31
Latency Numbers Every Programmer Should Know
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
@viktorklang
viktorklang / linearize.scala
Created August 14, 2012 10:02
A Linearized version of Sequence
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]] = {
@mpilquist
mpilquist / gist:3428731
Created August 22, 2012 19:46
Example of trampolining iteratees with scalaz 7
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
@robinsloan
robinsloan / langoliers.rb
Last active June 25, 2024 17:12
The Langoliers, a tweet deletion script
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"