Skip to content

Instantly share code, notes, and snippets.

def nth(n:Int, lst: List[Int]): Int = {
if (n == 0) {
return lst.head
}
lst match {
case head :: tail => nth(n - 1, tail)
case Nil => throw new NoSuchElementException
}
}
def length(lst: List[Int]): Int = {
lst match {
case Nil => 0
case head :: tail => 1 + length(tail)
}
}
length(List(1, 1, 2, 3, 5, 8))
def flatten(lst: List[Any]): List[Any] = {
lst match {
case Nil => List[Any]()
case (head:List[Any]) :: body => flatten(head) ::: flatten(body)
case head :: body => head :: flatten(body)
}
}
flatten(List(List(1, 1), 2, List(3, List(5, 8))))
// My common scenario is that I need to compare a single Patient to a set of Donors, basically performing the equiavalent to Enumerable#all? in Ruby.
// To fully understand this example, realize that the NodeData object contains a single Patient and one or more Donors. When looping, we're comparing one NodeData's Patient with a separate NodeData's list of Donors.
// Using the following iterator
bool KpdDriver::donorIterator(NodeData& recip_nd, NodeData& donor_nd, bool (kpd::drivers::KpdDriver::* pt2func)(Patient, Donor)) {
Patient p = recip_nd.patient;
std::list<Donor>::const_iterator donor_iter;
bool ret = false;
# Custom shoulda macro example -- validating POST data in a controller
# The macro -- note that it references a create_user() function. This function is included beneath the macro
def should_require(att)
should "require #{att}" do
create_user(att => nil)
assert assigns(:user).errors.on(att)
assert_response :success
assert_template 'new'
1) Ensure apache is enabled
2) Edit /etc/apache2/users/<yourusername>.conf
NameVirtualHost *:80
<VirtualHost *:80>
ServerName something.localhost
DocumentRoot /where/your/shit/is
</VirtualHost>
3) Add this to /etc/hosts
127.0.0.1 something.localhost
4) sudo apachectl restart
require 'mocha'
# Used such that the named_scope chain will return a count of 0 to allow us to test "empty" messages in views
def mock_empty_named_scope(model, named_scope)
# For any instance of model, stub the named_scope and have it return an object that responds to :count with a value of 0
model.any_instance.stubs(named_scope).returns(mock(:count => 0))
end
@jtrupiano
jtrupiano / gist:127636
Created June 11, 2009 01:14
Shell Script to Upgrade Ruby Enterprise Edition while Maintaining Directory Naming Sanity
#!/bin/sh
# Author: John Trupiano
# Script to upgrade an REE installation on a hot server and maintain sane directory names
if [ "$(whoami)" != "root" ]; then
echo "You need to be root to run this!"
exit 2
fi
RF_RELEASE=58677
# Find all files > 1M in or beneath the current directory
find . -size +1M -printf "%-10s %p\n"
@jtrupiano
jtrupiano / gist:208916
Created October 13, 2009 02:33
A rack middleware for defining and applying rewrite rules. In many cases you can get away with rack-rewrite instead of writing Apache mod_rewrite rules.
# This is actually available as a gem: gem install rack-rewrite
# Full source code including tests is on github: http://github.com/jtrupiano/rack-rewrite
module Rack
# A rack middleware for defining and applying rewrite rules. In many cases you
# can get away with rack-rewrite instead of writing Apache mod_rewrite rules.
class Rewrite
def initialize(app, &rule_block)
@app = app
@rule_set = RuleSet.new