This file contains hidden or 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
(use '(incanter core charts io latex)) | |
(defn stirling [#^Integer n] | |
(* (Math/sqrt (* 2 n Math/PI)) | |
(Math/pow n n) | |
(Math/pow Math/E (* -1 n)))) | |
(defn fact [#^Integer n] | |
(reduce * (range 2 (inc n)))) |
This file contains hidden or 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
class OptionalSessionStore < SqlSessionStore | |
cattr_accessor :enabled_session_class | |
cattr_accessor :disabled | |
class << self | |
def disabled=(v) | |
if self.disabled != v | |
@@disabled = v | |
self.toggle_session_class |
This file contains hidden or 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
before_filter :toggle_session | |
def toggle_session | |
# Disable the session if we spy a bot (or it was | |
# disabled elsewhere), enable otherwise. | |
if @disable_session.nil? | |
@disable_session = UrbanspoonUtil.is_megatron?(request.user_agent) | |
end | |
OptionalSessionStore.disabled = @disable_session | |
end |
This file contains hidden or 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
# NoSession acts like a session, but doesn't get stored anywhere. Sneaky. | |
class NoSession < AbstractSession | |
class << self | |
def find_session(session_id); end | |
def create_session(session_id, data={}) | |
new(session_id, data) | |
end | |
def delete_all(condition=nil); end | |
end |
This file contains hidden or 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
10.times do | |
r = Restaurant.find(:all, :conditions => ['id IN (?)', ids]) | |
Restaurant.add_cuisines(r) | |
r.map(&:cuisines).flatten.map(&:id) | |
end | |
# benchmarked 10 times | |
user system total real | |
3.070000 0.040000 3.110000 ( 3.148139) | |
3.040000 0.030000 3.070000 ( 3.133394) |
This file contains hidden or 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 Restaurant.add_cuisines(restaurants) | |
return [] if restaurants.empty? | |
ids = [] | |
id_map = {} | |
restaurants.each do |r| | |
id_map[r[:id]] = r | |
ids << r[:id] | |
# make sure ActiveRecord won't try to load cuisines again | |
r.cuisines.loaded |
This file contains hidden or 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
r.map(&:cuisines).flatten.map(&:object_id).uniq.length | |
=> 2001 | |
r.map(&:cuisines).flatten.map(&:id).uniq.length | |
=> 73 |
This file contains hidden or 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
10.times do | |
r = Restaurant.find(:all, :include => [:cuisines], :conditions => ['id IN (?)', ids]) | |
r.map(&:cuisines).flatten.map(&:id) | |
end | |
# benchmarked 10 times | |
user system total real | |
5.540000 0.010000 5.550000 ( 5.553716) | |
5.560000 0.000000 5.560000 ( 5.566566) | |
5.670000 0.010000 5.680000 ( 5.687871) |
This file contains hidden or 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
10.times do | |
r = Restaurant.find(:all, :conditions => ['id IN (?)', ids]) | |
r.map(&:cuisines).flatten.map(&:id) | |
end | |
# benchmarked 10 times | |
user system total real | |
10.170000 0.470000 10.640000 ( 11.809886) | |
9.630000 0.410000 10.040000 ( 11.076710) | |
11.220000 0.520000 11.740000 ( 12.819519) |
This file contains hidden or 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
class Restaurant < ActiveRecord::Base | |
has_many :restaurant_cuisines, :dependent => :delete_all | |
has_many :cuisines, :through => :restaurant_cuisines | |
end | |
class Cuisine < ActiveRecord::Base | |
has_many :restaurant_cuisines, :dependent => :delete_all | |
has_many :restaurants, :through => :restaurant_cuisines | |
end |