Skip to content

Instantly share code, notes, and snippets.

View tlehman's full-sized avatar

Tobi Lehman tlehman

View GitHub Profile
@tlehman
tlehman / intomm.py
Created August 17, 2012 00:39
convert in to mm in triples
def mm(s):
in_to_mm = lambda i: i*25.4
delim = " x "
ss = s.replace(" ", "").lower().split(delim.replace(" ", ""))
dims_in = map(float, ss)
dims_mm = map(in_to_mm, dims_in)
return reduce(lambda l,r: l+delim+r, map(str, dims_mm),"")[len(delim):-1]
@tlehman
tlehman / Integrator.java
Created August 19, 2012 00:07
An integrator from Ben Fry's "Visualizing Data" (based on Hooke's law for springs)
class Integrator {
final float DAMPING = 0.5f;
final float ATTRACTION = 0.2f;
float value;
float vel;
float accel;
float force;
float mass = 1;
@tlehman
tlehman / lambda_str.py
Created September 6, 2012 17:30
use C#-like lambda expressions in python
def _(s):
return eval(("lambda " + s).replace("=>", ":"))
# >>> _("x => str(x)")(42) == "42"
True
@tlehman
tlehman / fizzbuzz_model.rb
Created September 9, 2012 18:51
Minimal UITableView datasource skeleton (with data)
class FizzBuzz
def initialize
@data = (1..100).map { |n|
out = ((n%3 == 0) ? "Fizz" : "") + ((n%5 == 0) ? "Buzz" : "")
(out == "") ? n.to_s : out
}
end
def tableView(tableView, cellForRowAtIndexPath: indexPath)
@reuse_identifier ||= "CELL_IDENTIFIER"
@tlehman
tlehman / bundle_isolate_i.sh
Created September 26, 2012 22:35
Bundle or Isolate, Nobody Knows!
i() {
if [[ -a tmp/isolate ]] ; then
echo "\$ rake isolate:sh[\"$*\"]"
rake isolate:sh["$*"]
else
if [[ -a Gemfile ]] ; then
echo "\$ bundle exec[$*]"
bundle exec $*
else
echo "You must be at the root of a rails app that uses either Bundler or Isolate."
@tlehman
tlehman / reduce.scala
Created October 3, 2012 04:43
reduce in scala
def reduce(f:(Int,Int)=>Int, xo:Int, lst: List[Int]): Int = {
if (lst.length == 1) f(xo, lst.head)
else f(reduce(f, xo, lst.tail), lst.head)
}
@tlehman
tlehman / stage.sh
Created October 3, 2012 18:39
staging for GS in bash
# a 'revision' is a branch, commit or tag
stage() {
CWD=`pwd`
DIR=${CWD##*/}
if [ "$*" == "" ]; then
echo "We need a revision to deploy. E.g. master"
return 1
fi
@tlehman
tlehman / pivotal_tracker_id.sh
Created October 13, 2012 00:12
PivotalTracker ID extract from branch name
git branch | grep -e "^\*" | sed 's/^\* //g' | sed 's/\-/ /g' | awk '{print $(NF)}'
@tlehman
tlehman / robot-baby.el
Created November 3, 2012 17:45
(and robot baby)
;; (quoted-from (short (story (title (and robot baby)))))
(setq line "(Implies (Non-literal-interpretation x) y) (Value (Obey x) (* 0.5 (Value (Obey y)))))")
"(Implies (Non-literal-interpretation x) y) (Value (Obey x) (* 0.5 (Value (Obey y)))))"
;; check if expr (list of chars of some expression) is balanced
(defun balanced (expr)
(defvar lparen (string-to-char "("))
(defvar rparen (string-to-char ")"))
(defun is-open (c) (eq lparen c))
@tlehman
tlehman / scale_half.py
Created November 16, 2012 00:57
Scale a number one half
import sys
input_number = sys.stdin.readline()
print(float(input_number)*0.5)