Skip to content

Instantly share code, notes, and snippets.

View listrophy's full-sized avatar

Brad Grzesiak listrophy

View GitHub Profile
@listrophy
listrophy / composeAndApply.swift
Last active August 29, 2015 14:02
composeAndApply in swift
func composeAndApply<T>(funcs: (T -> T)[], val: T) -> T {
return funcs.reduce(val, combine: { (memo, aFunc) in aFunc(memo) })
}
func square(val: Int) -> Int {
return val * val
}
func double(val: Int) -> Int {
return val * 2
@listrophy
listrophy / Mutator.swift
Created January 7, 2015 00:42
Quick does not instantiate new objects for each test
class Mutator {
var mutated: Bool
init() {
mutated = false
}
func mutate() {
mutated = true
}
}
@listrophy
listrophy / rpsls.rb
Created February 21, 2015 20:11
Rock, Paper, Scissors, Lizard, Spock
# Exactly 140 characters! :)
# We rescue StandardError and re-raise a RuntimeError
# because that's nicer than getting NoMethodError or TypeError
def play a, b
t = %w(scissors paper rock lizard spock)
['tie', b, a, b, a][(t.index(a)-t.index(b)) % 5]
rescue StandardError
raise 'invalid'
end
@listrophy
listrophy / unwrap.swift
Last active August 29, 2015 14:23
if-let => try
func unwrap<T>(wrapped: T?) throws -> T {
guard let unwrapped = wrapped else {
throw NSError(domain: "custom", code: 1, userInfo: nil)
}
return unwrapped
}
parse_git_branch(){
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
parse_home_dir(){
pwd -P | sed -e "s/^$(echo $HOME | sed -e 's/\//\\\//g')/~/" -e 's/~\/Development/~\/dev/'
}
PS1="\$(parse_home_dir)\$(parse_git_branch) $ "
@listrophy
listrophy / gist:44189
Created January 7, 2009 05:39
The Birthday Problem!
lower_bound = (ARGV.shift || 2).to_i
upper_bound = (ARGV.shift || 35).to_i
iterations = (ARGV.shift || 20_000).to_i
(lower_bound..upper_bound).each do |person_count|
matches = 0
iterations.times do
matches += 1 if (1..person_count).map{|p| rand(365)}.uniq!
end
puts "Person count: %3d Success: %3.1f" %
' MACRO NAME:
' AddQuantity
' AUTHOR:
' Bradley Grzesiak
' FUNCTION:
' Increments the quantity of one or many dimensions in a drawing
' INSTALL:
' Create a "New Macro Button" via Tools->Customize->Commands->Macro in a toolbar.
' It is highly recommended that you assign a keystroke to this macro. I use "q"
' COMPATIBILITY:
@listrophy
listrophy / index.html.haml_spec.rb
Created March 21, 2009 18:23
users.size == 0... i think it should be 2
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
describe "/users/index.html.haml" do
include UsersHelper
fixtures :users
before(:each) do
assigns[:users] = users
print "\nuser size: #{users.size}\n"
end
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe StoriesController do
def mock_story(stubs={})
@mock_story ||= mock_model(Story, stubs)
end
def mock_errors(stubs={})
unless @mock_errors
@mock_errors = mock(:errors, stubs)
@mock_errors.should_receive(:on).with(:text).and_return("error on :text")
Shoes.setup do
gem 'activeresource'
end
require 'activeresource'
class Person < ActiveResource::Base
self.site = 'http://0.0.0.0:3000'
end