Skip to content

Instantly share code, notes, and snippets.

@foogoof
foogoof / rspec should be readable.rb
Created October 16, 2010 05:56
Code sample written for a 5 minute talk on RSpec
describe "NIC-U" do
context "Fall conference" do
it "should be readable" do
"readable".should == "readable"
end
it { "readable".should == "readable"}
context "readable" do
it { should == "readable" }
class Foo
attr_reader :name
def initialize(name)
@name = name
end
def method_missing(name, *args, &block)
return super unless name.to_s =~ /^go_/
puts "Defining #{name}"
method = lambda { puts "hey #{@name}@#{object_id}, #{name.to_s.tr '_', ' '}" }
(let [fibs (iterate (fn [[a b]] [b (+ a b)]) [0 1])
just-the-fibs (take-while #(> 4e6 (apply max %)) fibs)]
(reduce (fn [sum [_ r]] (+ sum (if (even? r) r 0)))
0
just-the-fibs))
(defn nth-val [first-val step val-count]
(+ first-val (* step (dec val-count))))
(defn arithmetic-series [first-val step val-count]
(let [last-val (nth-val first-val step val-count)]
(* val-count
(/ (+ first-val last-val) 2))))
(defn arithmetic-series-of-multiples-less-than [ceiling]
(fn [val]
(defstruct chopstick :id :holder)
(defstruct philosopher :name :left-stick :right-stick)
(defn make-chopstick [id]
(struct chopstick id (ref "")))
(defn set-table [philosophers]
(let [stick-count (max 2 (count philosophers))
stick-seq (map make-chopstick (range stick-count))
stick-cycle (cycle (take stick-count stick-seq))]