Skip to content

Instantly share code, notes, and snippets.

View ianbishop's full-sized avatar

Ian Bishop ianbishop

View GitHub Profile
(def superset {:a 1 :b 2 :c 5})
(def subset {:a 1 :b 2})
(-> superset
(select-keys (keys subset))
(= subset))
=> true
(def superset {:a 9 :b 2 :c 5})
@ianbishop
ianbishop / buckets.java
Last active December 12, 2015 02:28
Buckets!!
import java.util.*;
public class BucketTest {
static Random gen = new Random(System.currentTimeMillis());
static int MAX_LENGTH = 64;
public static void main(String[] args) {
int[] buckets = new int[Integer.parseInt(args[0])];
# BFS
def bfs(source, target)
q = []
visited = {}
q << source
while !q.empty?
current = q.shift
visited[current] = true
@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]
@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 / 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 / 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: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: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: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(&:|)