Skip to content

Instantly share code, notes, and snippets.

Ryan Moser moserrya

Block or report user

Report or block moserrya

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View gist:c276d5d8a93f8a54bfee
item = Item.find 328
item.transaction do
item.feelings.create!(size: 'huge')
item.update_attributes!(state: "sold")
# item.update_attributes! fails a validation, which we rescue. new feeling that we created is rightly rolled back #saves the feelings record that was rolled back as part of our transaction!
View baconator.clj
(ns erdos-c.core
[:require [yokogiri.core :refer :all]])
(def client (make-client :javascript false))
(defn page [uri]
(get-page client (str "" uri)))
(defn link-snippets [uri]
(xpath (page uri) "//a"))
View cached_shuffle.clj
(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)]
(<= initial-position cut-size) (+ (dec (* 2 initial-position)) offset)
View juxtapose.rb
juxt = ->(*fns) do
->(*args) do do |fn|
args.reduce {|acc, e| acc, e}
max = ->(a, b) {a > b ? a : b}
min = ->(a, b) {a < b ? a : b}
moserrya / 177.clj
Last active Dec 28, 2015
4Clojure #97: Pascal's triangle
View 177.clj
(fn [string]
(let [br (re-seq #"[()\[\]{}]" string)]
(loop [brackets br acc []]
(if (empty? brackets)
(if-let [lb (#{"(" "[" "{"} (first brackets))]
(recur (rest brackets) (conj acc lb))
(if (= (peek acc) ({")" "(" "}" "{" "[" "]"} (first brackets)))
(recur (rest brackets) (pop acc))
View FizzBuzz.clj
(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))))
View Gemfile
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 / shifted_sorted.rb
Created Apr 22, 2013
Find the smallest number in a shifted sorted array that runs in log(n) time
View shifted_sorted.rb
def find_index(ary, index = 0)
midpt = ary.length / 2
if midpt <= 1
return ary.first < ary.last ? index : index + 1
left, right = ary[0...midpt], ary[midpt..-1]
if left.last > left.first && ary.last < ary.first
index += midpt
find_index(right, index)
moserrya / rearrange.rb
Created Apr 19, 2013
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!
View rearrange.rb
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]]
positions.each_with_index {|e, i| characters[i] = e}
moserrya / justify.js
Last active Dec 16, 2015
Justify strings to a given width. Works in-place without breaking the string into an array.
View justify.js
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 += ' ';
You can’t perform that action at this time.