This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Find all files > 1M in or beneath the current directory | |
find . -size +1M -printf "%-10s %p\n" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
OlderNewer