This file contains 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
require 'extensions/all' | |
class String | |
def peel() | |
return nil if self.length.zero? | |
self.strip | |
end | |
end | |
class Seeker | |
attr_accessor :origin, :destination, :includes, :query |
This file contains 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
data = (1..1_000).to_a | |
measure "sum" do | |
data.sum { |n| n } | |
end | |
measure "inject" do | |
data.inject(0) { |sum, element| sum + element} | |
end |
This file contains 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
# copied from @solidsnack | |
nethack on | |
vbell on | |
defutf8 on | |
defscrollback 16384 | |
backtick 1 1 1 date -u +%Y-%m-%dT%TZ | |
logtstamp on |
This file contains 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
(ns sicp | |
(:use clojure.test)) | |
(defn square [num] (* num num)) | |
(defn sum-of-squares [x y] (+ (square x) (square y))) | |
; assume x != y != z | |
(defn sum-of-larger-squares [x y z] | |
(cond | |
(and (< x y) (< x z));x is the smallest | |
(sum-of-squares y z) |
This file contains 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
var palindrome = function(center){ | |
var first = center.slice(0,1); | |
var last = center.slice(-1); | |
var mid = center.slice(1,-1); | |
if(mid == "") { print("true"); return;} | |
if(first == last) { palindrome(mid); } | |
else { print("false"); } |
This file contains 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
CREATE OR REPLACE FUNCTION lock_head() RETURNS SETOF jobs AS $$ | |
DECLARE | |
unlocked integer; | |
job jobs%rowtype; | |
BEGIN | |
SELECT id INTO unlocked | |
FROM jobs |
This file contains 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
QUERY PLAN | |
-------------------------------------------------------------------------------------------------------------------------------- | |
Limit (cost=21.03..21.04 rows=1 width=10) (actual time=0.063..0.063 rows=1 loops=1) | |
-> LockRows (cost=21.03..21.11 rows=6 width=10) (actual time=0.062..0.062 rows=1 loops=1) | |
-> Sort (cost=21.03..21.05 rows=6 width=10) (actual time=0.046..0.046 rows=1 loops=1) | |
Sort Key: id | |
Sort Method: quicksort Memory: 25kB | |
-> Seq Scan on queue_classic_jobs (cost=0.00..21.00 rows=6 width=10) (actual time=0.035..0.036 rows=1 loops=1) | |
Filter: (locked_at IS NULL) | |
Total runtime: 0.105 ms |
This file contains 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
module Memstat | |
extend self | |
def puts | |
"memory=#{real_mem_size} ruby_objects=#{num_objects}" | |
end | |
def real_mem_size | |
mb = `ps -o rsz #{$$}`.split("\n")[1].to_f / 1024.0 | |
mb = mb.round(-2) # nearest 100 (i.e. 60.round(-2) == 100) |
This file contains 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
module QC | |
class Worker | |
MAX_LOCK_ATTEMPTS = ENV["QC_MAX_LOCK_ATTEMPTS"] || 5 | |
def initialize | |
@running = true | |
@queue = QC::Queue.new(ENV["QUEUE"]) | |
handle_signals | |
end |
This file contains 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 lock_job | |
attempts = 0 | |
job = nil | |
until job | |
job = @queue.dequeue | |
if job.nil? | |
attempts += 1 | |
if attempts < MAX_LOCK_ATTEMPTS | |
sleep(2**attempts) | |
next |