Skip to content

Instantly share code, notes, and snippets.

View BFalkner's full-sized avatar

Brennan Falkner BFalkner

View GitHub Profile
private static readonly MediaTypeCollection mediaTypes = ((MediaTypesConfigSection)ConfigurationManager.GetSection("typeSection")).MediaTypes;
public static ActionResult With(this Controller controller, params Func<object, ActionResult>[] responses)
{
string format = controller.ControllerContext.RouteData.Values["format"] as string ?? "html";
var match = responses.First(
r => Lookup(r.Method.GetParameters()[0].Name, format));
MediaType type = mediaTypes[match.Method.GetParameters()[0].Name];
if (type != null)
controller.Response.ContentType = type.Name;
private static string EnumOptions<T>()
{
if (!typeof(T).IsEnum)
return string.Empty;
return Enum.GetNames(typeof (T))
.Select(s => string.Format("\'{0}\'", s))
.Aggregate((lhs, rhs) => string.Format("{0}, {1}", lhs, rhs));
}
@BFalkner
BFalkner / lazy.rb
Created December 8, 2010 04:32
A sample partial implementation of a lazy enumerable. See http://gist.github.com/441028 for example usage.
module Lazy
module LazyEnumerable; end
class Enumerator
include LazyEnumerable
def initialize(enumerable=nil, &block)
if enumerable
@enumeration_block = proc {|send| enumerable.each(&send) }
else
> LazyEnumerator.new([1, 2, 3]).
map{|el| puts "add #{el}"; el + 1}.
map{|el| puts "mul #{el}"; el * 2}.
each{|el| puts el}
add 1
mul 2
4
add 2
mul 3
6
def integer?
ActivePattern::Pattern.build do |value, pass|
if value.is_a? Integer
value
elsif value.is_a? String
/\A[-+]?\d+\z/ === value ? value.to_i : pass
else
pass
end
end
class Test
include Enumerable
def initialize(arr)
@arr = arr
end
def each(&block)
@arr.each do |el|
puts "start #{el}"
>> hash = {:blah.lte => 5}
=> {#<MongoMapper::FinderOperator:0x105113338 @field=:blah, @operator="$lte">=>5}
>> hash[:blah.lte]
=> nil
>> :blah.lte == :blah.lte
=> false
class RenameThingsToRecords < ActiveRecord::Migration
def self.up
command = OrderedHash.new
command['renameCollection'] = "#{MongoMapper.database.name}.things"
command['to'] = "#{MongoMapper.database.name}.records"
MongoMapper.connection.db('admin').command(command)
end
def self.down
command = OrderedHash.new

Notes

The original subs actually making a query with printing the query, so no real queries here either.

The 'transaction' from the original doesn't appear to do anything except close over the connection argument. I didn't implement any transactional functionality because the original didn't appear to have any.

It doesn't make sense to memoize the non-query stuff and always run the query which is going to be your slowest thing. It also doesn't make sense to memoize inserts which always need to run.

Output

def self.find_by_probability(user)
links = Link.find :all, :include => :votes
users = User.select { |u| u.votes.id }.to_a
links.map do |link|
link.probability = bayes(
link,
users_with_link(link, users),
proc{|e, h| common_links(e, user, h) / all_links(e, user, h) },
proc{|e| e.votes.size.to_f / links.size },