Skip to content

Instantly share code, notes, and snippets.

View moserrya's full-sized avatar

Ryan Moser moserrya

View GitHub Profile
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!
(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"))
(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)
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}
@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))))))
(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))))
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
@moserrya
moserrya / shifted_sorted.rb
Created April 22, 2013 22:39
Find the smallest number in a shifted sorted array that runs in log(n) time
def find_index(ary, index = 0)
midpt = ary.length / 2
if midpt <= 1
return ary.first < ary.last ? index : index + 1
end
left, right = ary[0...midpt], ary[midpt..-1]
if left.last > left.first && ary.last < ary.first
index += midpt
find_index(right, index)
else
@moserrya
moserrya / rearrange.rb
Created April 19, 2013 23:19
Rearrange the elements of one array according to an index in another array without using additional space in memory. Note: This solution works but destroys the positions array in the process!
characters = [ 'o', 'h', 'e', 'd', 'n', 'r', 'y', 'g', 'a', 'b', 'e' ]
positions = [ 1, 0, 4, 2, 6, 9, 8, 3, 7, 10, 5]
def rearrange(characters, positions)
positions.length.times do |i|
positions[i] = characters[positions[i]]
end
positions.each_with_index {|e, i| characters[i] = e}
end
@moserrya
moserrya / justify.js
Last active December 16, 2015 10:58
Justify strings to a given width. Works in-place without breaking the string into an array.
function justify(str, width) {
var whitespace = ' ';
while (str.length < width) {
var oldLength = str.length;
var spacing = new RegExp("(\\w)" + whitespace + "(\\w)");
str = str.replace(spacing, '$1 ' + whitespace + '$2');
if (oldLength === str.length) {
whitespace += ' ';
}
}