Skip to content

Instantly share code, notes, and snippets.

View timperrett's full-sized avatar
🚧

Timothy Perrett timperrett

🚧
View GitHub Profile
@timperrett
timperrett / gist:3436334
Created August 23, 2012 12:51 — forked from chrislewis/gist:3416494
reader monad for java.util.Properties example
/* Start by creating a reader for some fake yet conceivable type for executing SQL queries. */
val dbReader: Reader[Properties, JdbcExecutor] =
for {
driver <- read[String]("db.driver")
uri <- read[String]("db.uri")
user <- read[String]("db.username")
password <- read[String]("db.password")
name <- read[String]("db.pool.name")
minCons <- read[Int]("db.pool.minConnections")
using System;
namespace Config {
sealed public class Configuration {
public readonly string hostname;
public readonly int port;
public readonly string outfile;
public Configuration(string hostname, int port, string outfile) {
@timperrett
timperrett / latency.txt
Created May 31, 2012 09:29 — forked from jboner/latency.txt
Latency numbers every programmer should know
By Jeff Dean (http://research.google.com/people/jeff/):
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns
Mutex lock/unlock 25 ns
Main memory reference 100 ns
Compress 1K bytes with Zippy 3,000 ns
Send 2K bytes over 1 Gbps network 20,000 ns
Read 1 MB sequentially from memory 250,000 ns
..
@:``#,
#`````+
,@+@``````@+@
#````````````@
`````````````@
#````````````@
.@+;+++++++@@
..
@:``#,
#`````+
,@+@``````@+@
#````````````@
`````````````@
#````````````@
.@+;+++++++@@
@,'`
@timperrett
timperrett / gist:1810800
Created February 12, 2012 20:53 — forked from teigen/gist:1435102
sbt multi setting example
lazy val yodawg = TaskKey[String]("yo-dawg", "I heard you liked settings, so I put a setting in your setting")
yodawg <<= Project.app(moduleName :^: version :^: isSnapshot :^: KNil){
case a :+: b :+: c :+: HNil => task(List(a, b, c).mkString("~"))
}
>show yo-dawg
[info] web~2.0-SNAPSHOT~true
// source: https://github.com/harrah/xsbt/blob/0.11/main/Structure.scala#L443
trait Cake {
type Config
def config: Config
}
trait FooConfig {
// ...
}
trait FooComponent extends Cake {
// these are the data types
trait Tense
case object Past extends Tense
case object Present extends Tense
case object Future extends Tense
trait Subject {
protected val usage: Map[Tense, String]
def apply(tense: Tense): String = usage(tense)
}
scala> mongo("projects").group(
| MongoDBObject(), // condition
| MongoDBObject(), // key
| MongoDBObject("count" -> 0, "contributors" -> 0), //initial
| "function(doc, out){ out.count++; out.contributors+=doc.contributorCount;}" //,
| //"function(out){ out.average = out.contributors / out.count; }"
| )
res2: scala.collection.mutable.Iterable[com.mongodb.DBObject] = ArrayBuffer({ "count" : 3.0 , "contributors" : 59.0})
scala> mongo("projects").group(
@timperrett
timperrett / Aggregator.scala
Created September 4, 2011 15:54 — forked from remeniuk/Aggregator.scala
Scatter-Gather with Akka Dataflow
class Aggregator(recipients: Iterable[ActorRef]) extends Actor{
def receive = {
case msg @ Message(text) =>
println("Started processing message `%s`" format(text))
val result = Promise[String]()
val promises = List.fill(recipients.size)(Promise[String]())
recipients.zip(promises).map{case (recipient, promise) =>