Skip to content

Instantly share code, notes, and snippets.

@bradfordw
bradfordw / .gitignore
Created November 30, 2009 22:40
A n00bs crack at an evolutionary algorithm in Erlang
*.beam
# mongo_template.rb
# fork of Ben Scofield's Rails MongoMapper Template (http://gist.github.com/181842)
#
# To use:
# rails project_name -m http://gist.github.com/gists/219223.txt
# remove unneeded defaults
run "rm public/index.html"
run "rm public/images/rails.png"
run "rm public/javascripts/controls.js"
-module(ann).
-define(SIG,fun(X) -> 1 / (1 + math:exp(-X)) end).
-define(SIG_DERIV,fun(X) -> math:exp(-X) / (1 + math:exp(-2 * X)) end).
-compile(export_all).
dp(X,Y) ->
lists:foldl(fun(E,Sum) -> E + Sum end, 0,
vector_map(fun(EX,EY) -> EX * EY end, X, Y)).
Getting a non replicated mongo instance's operation log for indexing:
db.oplog.$main.find({ ts: { t: {gte: last_processed_time } } }).sort({$natural:1});
3> {_,I5,I6,H5,H6,O} = ann:run(),I5 ! {pass, 1.3}, I6 ! {pass, 1.8}.
NewInput: [{<0.47.0>,0.5},{<0.46.0>,1.3}]
NewInput: [{<0.47.0>,0.5},{<0.46.0>,1.3}]
{pass,1.8}
Output: 0.7109495026250039
Output: 0.7109495026250039
Sens: [<0.50.0>]
Sens: [<0.50.0>]
NewInput: [{<0.47.0>,1.8},{<0.46.0>,1.3}]
NewInput: [{<0.47.0>,1.8},{<0.46.0>,1.3}]
@bradfordw
bradfordw / simple_paging_for_any_orm.rb
Created January 8, 2010 15:41
I think (hope) this will setup some "nice" paging for just about any orm/collection out there.
# This is based on the styles for the table paging found meow - http://pilu.github.com/web-app-theme/
# total - total number of entries - Foo.count()
# current_page - the current page you are on
# per_page - how many Foo's you want displayed per page.
# uri - the URI to append "?page=#{page}" to, e.g. "/foos"
def display_paging_links(total,current_page,per_page,uri)
previous_page = current_page <= 1 ? 1 : current_page - 1
pages = (total / per_page.to_f).ceil
next_page = current_page < pages ? (current_page + 1) : current_page
#eeeew, don't use this on anything "big"
# self here is representative of Model
def create_slug
return if self.name.blank?
tail, int = "", 2
initial = convert_to_slug(self.name)
if Model.find_by_slug(initial).nil?
self.slug = initial
else
tail = "-#{int}"
def convert_to_slug(str)
if defined?(ActiveSupport::Inflector.parameterize)
ActiveSupport::Inflector.parameterize(str).to_s
else
ActiveSupport::Multibyte::Handlers::UTF8Handler.
normalize(str,:d).split(//u).reject { |e| e.length > 1 }.join.strip.gsub(/[^a-z0-9]+/i, '-').downcase.gsub(/-+$/, '')
end
end
/*
Remove rage-a-hol inducing keywords from HN
*/
(function() {
var keywords = ["python","django"]; //add as you see fit.
var cleanse = [];
var a = document.getElementsByTagName('a');
for (var i = 0; i < a.length; i++) {
for (var j = 0; j < keywords.length; j++) {
if (a[i].innerHTML.match(new RegExp(keywords[j], "gim"))) {
require 'PP'
class QueryParser
attr_accessor :terms
def initialize(terms = {})
raise NotImplementedError unless terms.is_a? Hash
@terms = terms
end
def parse