Skip to content

Instantly share code, notes, and snippets.

View dschobel's full-sized avatar

Daniel Schobel dschobel

View GitHub Profile
@dschobel
dschobel / State.scala
Created June 6, 2015 06:41
calculating fibonacci numbers and manipulating a stack with the State monad
// no external dependencies, just :paste the entire gist into a scala 2.11 repl
// scala> import State._
// scala> StackExample.computed.run(List.empty)
// #result of push(1), push(2), push(3), pop()
// res0: (List[Int], Option[Int]) = (List(2, 1),Some(3))
// #naive fibonacci impl
// scala> time(FibExample.fib(42))
// took 1204 ms
@dschobel
dschobel / build.sbt
Last active August 29, 2015 14:16
basic build.sbt
lazy val root = (project in file(".")).
settings(
name := "My Project",
version := "1.0",
scalaVersion := "2.11.6"
)
libraryDependencies += "com.twitter" % "util-core_2.11" % "6.23.0"
libraryDependencies += "org.scalatest" %% "scalatest" % "2.2.1" % "test"
@dschobel
dschobel / LoggingFilter
Created October 23, 2014 20:55
finagle filter in java
import com.twitter.finagle.Service;
import com.twitter.finagle.SimpleFilter;
import com.twitter.util.Future;
public class MyThriftRequest { public int Attribute = 123; }
public class LoggingFilter extends SimpleFilter<MyThriftRequest, MyThriftResponse> {
private static final Logger LOG = Logger.getLogger(LoggingFilter.class);

Keybase proof

I hereby claim:

  • I am dschobel on github.
  • I am dschobel (https://keybase.io/dschobel) on keybase.
  • I have a public key whose fingerprint is E704 9E0F 00B0 4423 FEE1 9E4E 167C 1C57 BB23 3D77

To claim this, I am signing this object:

@dschobel
dschobel / Volatile.java
Created December 31, 2013 02:22
concurrent java code which should expose a visibility bug
public class Volatile {
private static boolean ready;
private static int number;
public static class ReaderThread extends Thread {
public void run() {
while(!ready)
{
Thread.yield();
}
@dschobel
dschobel / streams.scm
Created February 24, 2013 17:55
streams in scheme
(define (stream-maker seed fx)
(letrec ([f (lambda(x) (cons x (lambda () (fx x))))])
(lambda () (f seed))))
(define naturals (stream-maker 1 (lambda (x) (+ x 1))))
@dschobel
dschobel / gather_futures.scala
Created February 15, 2013 00:14
gathering Scala futures
def gather_futures[A](xs: Seq[Future[A]]): Future[Seq[A]]= {
def combine[A,B,C](f1: Future[A], f2: Future[B])(f: (A,B) => C): Future[C] ={
for(a <- f1; b <- f2)
yield f(a,b)
}
xs.foldLeft(Future{Seq[A]()}){(acc: Future[Seq[A]],x: Future[A]) => combine(x,acc)((a: A,b: Seq[A]) => b ++ Seq(a))}
}
@dschobel
dschobel / gist:4690653
Created February 1, 2013 10:58
counting inversions in sml in O(n*log(n)) time
fun count_and_sort [] = ([],0)
| count_and_sort [x] = ([x],0)
| count_and_sort xs =
let
fun count_and_sort_split_inversions([],ys,acc,sum) = (List.rev(acc) @ ys,sum)
| count_and_sort_split_inversions(xs,[],acc,sum) = (List.rev(acc) @ xs,sum)
| count_and_sort_split_inversions(x :: xs, y :: ys,acc,sum) =
if x < y
then count_and_sort_split_inversions(xs, y :: ys, x :: acc, sum)
else count_and_sort_split_inversions(x :: xs, ys, y :: acc, sum + List.length(x :: xs))
@dschobel
dschobel / eratosthenes.scala
Created December 11, 2012 22:54
sieve of eratosthenes in scala
def from(n: Int): Stream[Int] = n #:: from(n+1)
def sieve(nums: Stream[Int]): Stream[Int] = nums.head #:: sieve(nums.tail.filterNot(_ % nums.head == 0))
def primes: Stream[Int] = sieve(from(2))
@dschobel
dschobel / gist:4015298
Created November 5, 2012 04:22
insertionsort in scala
val r = new scala.util.Random()
val data = List.fill(10)(r.nextInt(100))
def isort(xs: List[Int]): List[Int] = {
def insert(x: Int, xs: List[Int]): List[Int] = xs match {
case List() => List(x)
case y :: ys => if (x < y) x :: y :: ys else y :: insert(x, ys)
}