Skip to content

Instantly share code, notes, and snippets.

View michaeltwofish's full-sized avatar

Michael C. Harris michaeltwofish

  • South Australia, Australia
View GitHub Profile
bson (1.2.0)
bson_ext (1.2.0)
cucumber (0.10.0)
gherkin (2.3.2)
mechanize (1.0.0)
mongo (1.2.0)
mysql (2.8.1)
rake (0.8.7)
rdoc (2.5.8)
selenium-client (1.2.18)
// Handle attaching children
$('#unattached_children a.add').click(function() {
var icons = $(this).parent();
var child_type = // work out what the name of the child is
// Update label text
$('#attached_children>label').html('Attached '+child_type);
// Move the item to the attached items list
$('#AttachedItems .list').append($(this).closest('.item'));
$('#attached_children .list .item:last-child').append('<input name="'+child_type+'[]" type="hidden" value="'+$('#attached_children .list .item:last-child').attr('id')+'" />');
@michaeltwofish
michaeltwofish / 3.rb
Created February 20, 2011 07:18
Euler Problem 3
num = 600851475143
# Brute force
def is_prime?(n)
limit = Math.sqrt(n).round
(2..limit).each do |i|
return false if n % i == 0
end
return true
end
// Create a finder for clusters not linked to 'object'
static::finder('unlinked', function($self, $params, $chain) {
$object = $params['options']['object'];
unset($params['options']['object']);
// @todo Shouldn't make a direct call to getClusters, but should instead
// have a protected array of things that can be linked
$ids = $object->getClusters()->map(function($cluster) {
return $cluster->id;
}, array('collect' => false));
$clusters_list = Cluster::all(array('conditions' =>array(
'id' => array('!=' => implode(',', $form_clusters->map(function($cluster) {
return $cluster->id;
}, array('collect' => false)))),
)));
@michaeltwofish
michaeltwofish / gist:828802
Created February 16, 2011 03:25
Euler problem 2
N = 4000000
# Brute force
def fib(num)
if num < 2
num
else
fib(num - 2) + fib(num - 1)
end
end
@michaeltwofish
michaeltwofish / gist:825434
Created February 14, 2011 03:15
Euler problem 1
# Brute force
sum = 0
(1..999).each do |num|
sum = sum + num if (num % 3 == 0 || num % 5 == 0)
end
puts "Brute force: #{sum}"
# More rubyish, but still brute force
sum = (1..999).inject(0) {|sum,num| (num % 3 == 0 || num % 5 == 0) ? sum + num : sum}
puts "More rubyish but still brute force: #{sum}"
public function testMap() {
$model = $this->_model;
$model::config(array('connection' => false));
$collection = new DocumentSet(compact('model'));
$collection->map(function($data) {return $data;})
$this->assertEqual($model, $collection->model()); // passes because map() doesn't run
}
/**
* Applies a callback to a copy of all data in the collection
* and returns the result.
*
* Overriden to load any data that has not yet been loaded.
*
* @param callback $filter The filter to apply.
* @param array $options The available options are:
* - `'collect'`: If `true`, the results will be returned wrapped
* in a new Collection object or subclass.
Exception thrown in -::lithium\data\source\database\adapter\{closure}() on line 342:
SELECT * FROM `units` AS `Unit` WHERE `name` = 'Unit 1' AND `0` = 'id != 1' LIMIT 1;: Unknown column '0' in 'where clause'
Trace:
lithium\core\Object::_filter(), line 245
lithium\data\source\database\adapter\MySql::_execute(), line 343
lithium\core\Object::invokeMethod(), line 169
lithium\data\source\{closure}, line 265
lithium\core\Object::_filter(), line 245
lithium\data\source\Database::read(), line 287
lithium\data\{closure}, line 455