Skip to content

Instantly share code, notes, and snippets.

View moserrya's full-sized avatar

Ryan Moser moserrya

View GitHub Profile
source :rubygems
# We are not loading Active Record, nor Active Resources etc.
# We can do this in any app by simply replacing the rails gem
# by the parts we want to use.
gem "actionpack", "~> 3.2"
gem "railties", "~> 3.2"
gem "tzinfo"
# Let's use thin
(defn to-fizzword [number]
(let [fizzword (str (if (zero? (mod number 3)) "Fizz")
(if (zero? (mod number 5)) "Buzz"))]
(if (empty? fizzword) number fizzword)))
(defn fizzbuzz [largest]
(map to-fizzword (range 1 (inc largest))))
@moserrya
moserrya / 177.clj
Last active December 28, 2015 22:59
4Clojure #97: Pascal's triangle
(fn [string]
(let [br (re-seq #"[()\[\]{}]" string)]
(loop [brackets br acc []]
(if (empty? brackets)
true
(if-let [lb (#{"(" "[" "{"} (first brackets))]
(recur (rest brackets) (conj acc lb))
(if (= (peek acc) ({")" "(" "}" "{" "[" "]"} (first brackets)))
(recur (rest brackets) (pop acc))
false))))))
juxt = ->(*fns) do
->(*args) do
fns.map do |fn|
args.reduce {|acc, e| fn.to_proc.call acc, e}
end
end
end
max = ->(a, b) {a > b ? a : b}
min = ->(a, b) {a < b ? a : b}
(ns shuffle)
(defn gcd [a b] (if (zero? b) a (recur b (mod a b))))
(defn lcm [a b] (/ (* a b) (gcd a b)))
(defn- new-position [deck-size cut-size initial-position]
(let [cards-shuffled (* 2 cut-size)
offset (- deck-size cards-shuffled)]
(cond
(<= initial-position cut-size) (+ (dec (* 2 initial-position)) offset)
(ns erdos-c.core
[:require [yokogiri.core :refer :all]])
(def client (make-client :javascript false))
(defn page [uri]
(get-page client (str "http://en.wikipedia.org" uri)))
(defn link-snippets [uri]
(xpath (page uri) "//a"))
item = Item.find 328
item.transaction do
item.feelings.create!(size: 'huge')
item.update_attributes!(state: "sold")
end
# item.update_attributes! fails a validation, which we rescue. new feeling that we created is rightly rolled back
item.save #saves the feelings record that was rolled back as part of our transaction!