Skip to content

Instantly share code, notes, and snippets.

View leedm777's full-sized avatar

David M. Lee leedm777

View GitHub Profile
/** Cute little function to take a random sample of a sequence. */
def randomSample(s:Seq[_]) = s filter { _ => util.Random.nextBoolean }
@leedm777
leedm777 / collatz.clj
Created March 9, 2010 14:57
Collatz algorithm in Scala and Clojure
(defn
#^{:doc "Clojure code to compute a Collatz sequence."}
collatz [n]
(if (= n 1)
'(1)
(cons n
(collatz (if (= (mod n 2) 0)
(/ n 2)
(+ (* n 3) 1))))))
@leedm777
leedm777 / mcpp-fix-stpcpy.patch
Created November 8, 2010 21:57
Homebrew patch. Fixes MCPP compilation on Mac.
stpcpy is a #define on Mac OS X. Trying to define it as an extern is invalid.
diff -ur mcpp-2.7.2-orig/src/internal.H mcpp-2.7.2/src/internal.H
--- mcpp-2.7.2-orig/src/internal.H 2008-08-27 08:01:16.000000000 -0500
+++ mcpp-2.7.2/src/internal.H 2010-11-08 15:53:38.000000000 -0600
@@ -557,6 +557,6 @@
#endif
#endif
-#if HOST_HAVE_STPCPY
@leedm777
leedm777 / ssh.config
Created November 9, 2010 21:04
My typical SSH config file
# This goes in ~/.ssh/config.
# ssh is picky about file permissions, so be sure to
# chmod 700 ~/.ssh
# chmod 600 ~/.ssh/config
#
# Forward X11 and ssh-agent to trusted hosts. Be sure to only do this for
# *trusted* machines, otherwise you may fall victim to identity theft.
# See ssh man page for details.
#
@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 <leedm777@yahoo.com>
#
# 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.
#
@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 / dmr.c
Created October 13, 2011 04:39
Goodbye, dmr.
#include <stdio.h>
int main() {
printf("Goodbye, dmr.\n");
return 0;
}
@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 / 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 / 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