Skip to content

Instantly share code, notes, and snippets.

View kputnam's full-sized avatar
💭
I have 478 browser tabs open

kputnam kputnam

💭
I have 478 browser tabs open
View GitHub Profile
@kputnam
kputnam / bench.rb
Last active April 13, 2022 23:50
Object#cons and Object#snoc benchmarks
#!/usr/bin/env ruby
require "pp"
require "benchmark/ips"
require "memory_profiler"
GC.disable
def mem(label, &block)
result = MemoryProfiler::Reporter.report(&block)
print label.ljust(30); pp result.allocated_memory_by_class
@kputnam
kputnam / tricks.sh
Last active November 6, 2017 01:38
Bash script tricks
#!/bin/sh
# make sure we aren't running already
what=`basename $0`
for p in `ps h -o pid -C $what`; do
if [ $p != $$ ]; then
exit 0
fi
done
#!/bin/sh
# either pass a single URL or --input-file=one-url-per-line.txt
wget \
--tries=5 \
--server-response \
--save-headers \
--default-page=index.html \
--adjust-extension \
@kputnam
kputnam / example.hs
Created April 16, 2016 05:44
Covariant and contravariant functors
-- Covariate functor
class Fun f where
fun :: (a -> b) -> f a -> f b
-- Contravariate functor
class Con f where
con :: (b -> a) -> f a -> f b
-- Function parameterized over input type 'i'

Notes on Ruby's method refinement

The example below creates a refinement module which adds a method to instances of Integer. In the Test class, we bring it into scope by declaring using Refinements. Next, we create an instance method using def_delegators named Test#xxx that calls @k.xxx, which should resolve to the refinement method we added to Integer. Finally, we "manually" create essentially the same method, this time named yyy, but we define it "manually" without using def_delegators.

require "forwardable"

module Refinements
  refine Integer do
 def xxx
@kputnam
kputnam / help-wanted.pl
Last active November 4, 2015 06:25
Formats and prints recent StackOverflow careers postings to console
#!/usr/bin/perl
use URI::Escape;
my $url = 'http://careers.stackoverflow.com/jobs/feed?searchTerm=';
# Either use search terms given as command-line arguments, or use default terms
my $q = join('+', map { uri_escape($_); } (@ARGV > 0) ?
@ARGV : ('-.net', '-css', '-c#', '-android', '-php', '-ios', '-vb.net', '-excel' ));
# Read XML from child process
@kputnam
kputnam / +jQuery.js
Last active December 29, 2015 20:22
jQuery Bookmarklet
(function() {
var otherLib = (typeof($) == 'function');
if (typeof(jQuery) != 'undefined') {
alert('jQuery already defined');
return;
}
function getScript(url, callback) {
var script = document.createElement('script');
@kputnam
kputnam / Evaluation.hs
Created November 25, 2014 20:56
Evaluation of Predictive Ranking Algorithms
import Data.Ord
import Data.List
import Test.QuickCheck
-- | Quantify how "unsorted" a given list of elements is, by computing
-- at each position in the list: how many later elements are smaller?
--
-- e.g. cost "abc" == 0
-- cost "acb" == 1
-- cost "cba" == 2
@kputnam
kputnam / KMeans.hs
Last active August 29, 2015 14:08
Sequential and Parallel KMeans
{-# LANGUAGE ScopedTypeVariables #-}
module KMeans
( euclidean
, kMeans
, kMeansPar
, chunk
, random
) where