Skip to content

Instantly share code, notes, and snippets.

View leedm777's full-sized avatar

David M. Lee leedm777

View GitHub Profile
@leedm777
leedm777 / future.scala
Created August 1, 2012 23:06
Snippets for Akka Future's talk
// You've gotta start somewhere
import akka.dispatch.Future
implicit val system = akka.actor.ActorSystem("MySystem")
// lame
val WHY_YOU_TAKE_SO_LONG: Seq[Kitten] =
grep(theWorld, kittens)
// awesome
val allKittens: Future[Seq[Kitten]] =
@leedm777
leedm777 / tap.cpp
Created June 22, 2012 19:42
tap implementation for C++11
/** tap implementation for C++11 */
template<typename T, typename F>
T tap(T &&obj, F const &fn) {
fn(obj);
return obj;
}
// usage
tap(new User(params), [](User *u) {
if (u->isValid()) {
@leedm777
leedm777 / fib.scala
Created May 7, 2012 19:34
fibonacci sequence, in scala
val fib: Stream[BigInt] = {
def nextFib(a: BigInt, b: BigInt): Stream[BigInt] = {
Stream.cons(a + b, nextFib(b, a + b))
}
Stream.cons(0, Stream.cons(1, nextFib(0, 1)))
}
object ZzubZzif {
val fizzbuzz: PartialFunction[Int, String] = {
case x if x % 15 == 0 => "fizzbuzz"
case x if x % 5 == 0 => "buzz"
case x if x % 3 == 0 => "fizz"
}
// I'm actually embarrassed that this is the best I can do
def zzubzzif(seq: Seq[String]): Seq[Int] = {
val fizzbuzzIndexed = new PartialFunction[(Int, Int), (String, Int)] {
@leedm777
leedm777 / decolor.sh
Created March 16, 2012 15:36
Runs a program, stripping ANSI color codes from stdout and stderr
#!/bin/bash
#
# Runs a program, stripping ANSI color codes from stdout and stderr
#
# detect which sed to use
if test -z ${SED}; then
if cat /dev/null | sed -r '' 2> /dev/null ; then
SED=sed
@leedm777
leedm777 / maps.scala
Created March 13, 2012 14:23 — forked from jbrechtel/maps.scala
Scala maps
//create a map
val cars = Map("james" -> "BMW", "ike" -> "Infiniti", "stephen" -> "Buick")
//create a map from an Array
//Scala does not provide a convenience method to do this... :(
val carArray = Array("james", "BMW", "ike", "Infiniti", "stephen", "Buick")
val cars = carArray.grouped(2).toList.map(c => (c.head,c.last)).toMap
//mapping over a map
//transform all string keys to symbols (interned strings)
@leedm777
leedm777 / traits.scala
Created March 13, 2012 13:42 — forked from jbrechtel/traits.scala
Traits in Scala
trait Searchable {
def search(query: SearchQuery): Seq[SearchResults]
}
trait PaginationWithNominalType {
this: Searchable =>
def page(query: SearchQuery, pageSize: Int, pageNum: Int) = {
search(query).grouped(pageSize).toList(pageNum)
}
}
@leedm777
leedm777 / dmr.c
Created October 13, 2011 04:39
Goodbye, dmr.
#include <stdio.h>
int main() {
printf("Goodbye, dmr.\n");
return 0;
}
@leedm777
leedm777 / fizzbuzz.scala
Created July 5, 2011 15:33
FizzBuzz in Scala
// FizzBuzz in Scala
// http://www.codinghorror.com/blog/2007/02/why-cant-programmers-program.html
1 to 100 map {
case x if x % 15 == 0 => "FizzBuzz"
case x if x % 3 == 0 => "Fizz"
case x if x % 5 == 0 => "Buzz"
case x => x
} foreach (println _)
@leedm777
leedm777 / update-check-commit-authors.sh
Created November 12, 2010 18:51
Compares git commit authors against a whitelist
#!/bin/bash
#
# Copyright 2010 David M. Lee, II <[email protected]>
#
# This git update hook compares the author emails from commits with a
# whitelist stored in ${GIT_DIR}/author-whitelist. If any commit has an
# author that is not whitelisted, the offending author's email is displayed
# then the update is rejected.
#