Skip to content

Instantly share code, notes, and snippets.

View ianbishop's full-sized avatar

Ian Bishop ianbishop

View GitHub Profile
@ianbishop
ianbishop / gist:2650235
Created May 10, 2012 01:11
An example of Symbol#to_proc
a = ["1", "2", "3", "4", "5"]
a.map { |s| s.to_i } # Ruby 1.8
# => [1,2,3,4,5]
a.map(&:to_i) # Ruby 1.9
# => [1,2,3,4,5]
@ianbishop
ianbishop / gist:2656257
Created May 10, 2012 22:18
Symbol#to_proc .inject
a = [1,2,3,4,5]
# Sum
a.inject(&:+)
# => 15
# Product
a.inject(&:*)
# => 120
@ianbishop
ianbishop / gist:2656316
Created May 10, 2012 22:34
Symbol#to_proc map-reduce
# Get total sum of the sizes of each word
words = "Hello World, how are you today?".split(/\s/)
words.map(&:size).inject(&:+)
# => 26
# Validate numerous fields
fields = ["foo", "bar", "tomato", "tomatoes", nil]
valid = !fields.map(&:nil?).inject(&:|)
@ianbishop
ianbishop / gist:2656416
Created May 10, 2012 22:49
Symbol#to_proc ridiculousness
# Generate a poker hand
faces = (2..10).to_a + %w(J Q K A)
suits = %w(S C H A)
suits.product(faces).sample(5).map(&:join)
# => ["S7", "HA", "S8", "C4", "C8"]
@ianbishop
ianbishop / gist:2656455
Created May 10, 2012 22:59
Symbol#to_proc benchmark
$ ruby toproc.rb
Rehearsal --------------------------------------------------
Method invoke 1.240000 0.000000 1.240000 ( 1.246622)
Symbol.to_proc 1.550000 0.000000 1.550000 ( 1.553462)
----------------------------------------- total: 2.790000sec
user system total real
Method invoke 1.260000 0.000000 1.260000 ( 1.262633)
Symbol.to_proc 1.420000 0.000000 1.420000 ( 1.424335)
@ianbishop
ianbishop / gist:3129935
Created July 17, 2012 15:04
am i evil?
require 'crawlers/yellow_pages_crawler'
class YellowPages
DEFAULT_PAGE_LENGTH = 100
DEFAULT_CONCURRENT_THREADS = 10
def initialize(api_key, sandbox_enabled)
@client = YellowApi.new(:apikey => api_key, :sandbox_enabled => sandbox_enabled)
end
@ianbishop
ianbishop / gist:3136361
Created July 18, 2012 13:57
yellow page email scrp
class YellowPagesCrawler < Pioneer::Base
attr_reader :locations
def initialize(opts = {})
@locations_business = opts.fetch(:locations, {})
@locations = @locations_business.keys
super
end
def processing(req)
@ianbishop
ianbishop / Billy.cs
Created November 15, 2012 03:02
Doing Thangs
public static class DispatcherHelper
{
public static void DelayInvoke(this Dispatcher dispatcher, TimeSpan ts, Action action)
{
DispatcherTimer delayTimer = new DispatcherTimer();
delayTimer.Interval = ts;
delayTimer.Tick += (s, e) =>
{
delayTimer.Stop();
action();
@ianbishop
ianbishop / goose.rb
Created December 28, 2012 21:32
gus
def some_function(a, b, &block)
v = []
(a .. b).each do |c|
v << block.call(a, c)
end
v
end
some_function(0, 10) do |a, c|
[c, a]
# BFS
def bfs(source, target)
q = []
visited = {}
q << source
while !q.empty?
current = q.shift
visited[current] = true