Skip to content

Instantly share code, notes, and snippets.

@gfredericks
gfredericks / java_math_100.clj
Created September 27, 2014 13:20
The first 100 natural numbers expressed using only the java.lang.Math class
;; 0:
(-> Math/E Math/toRadians Math/round)
;; 1:
(-> Math/E Math/log Math/round)
;; 2:
(-> Math/E Math/sqrt Math/round)
;; 3:
(-> Math/E Math/round)
;; 4:
(-> Math/PI Math/ceil Math/round)
@amaia
amaia / gist:1115627
Created July 30, 2011 15:14
copy yml example files
ruby -e "Dir.glob('*.yml.example'){|x| system \"cp #{x} #{x.split('.')[0..-2].join('.')}\"}"
@moiristo
moiristo / gist:1245170
Created September 27, 2011 14:27
Rails3 way to redirect non-www domain to www domain
# Rails3 way to redirect non-www domain to www domain
# Single domain redirect
'example.com'.tap do |host|
constraints(:host => host) do
match '/(*path)', :to => redirect { |params, request| Addressable::URI.escape request.url.sub(host, "www.#{host}") }
end
end
@jpmckinney
jpmckinney / fingerprint.rb
Created November 17, 2011 21:46
Google Refine fingerprint clustering algorithm in Ruby
# blog post: http://blog.slashpoundbang.com/post/12938588984/google-refine-fingerprint-clustering-algorithm-in-ruby
# coding: utf-8
require 'unicode_utils/downcase'
class String
# Normalize spaces and fingerprint.
# http://code.google.com/p/google-refine/wiki/ClusteringInDepth
# http://code.google.com/p/google-refine/source/browse/trunk/main/src/com/google/refine/clustering/binning/FingerprintKeyer.java
def fingerprint
@weppos
weppos / capistrano_database_yml.rb
Created July 27, 2008 10:04
Provides a couple of tasks for creating the database.yml configuration file dynamically when deploy:setup is run.
#
# = Capistrano database.yml task
#
# Provides a couple of tasks for creating the database.yml
# configuration file dynamically when deploy:setup is run.
#
# Category:: Capistrano
# Package:: Database
# Author:: Simone Carletti <weppos@weppos.net>
# Copyright:: 2007-2010 The Authors
@tmcw
tmcw / floyd.js
Created August 23, 2012 13:02
Floyd's Algorithm for Random Subsets
function sample(list, m) {
var n = list.length;
if (m > n) return void console &&
console.log('list length must be > sample');
var sampleList = [];
for (var i = n - m; i < n; i++) {
var item = list[~~(Math.random() * i)];
if (sampleList.indexOf(item) !== -1)
sampleList.push(list[i]);
else
@BurntSushi
BurntSushi / 01-possible-generic-map.go
Last active May 14, 2019 16:49
Code from my blog post "Writing type parametric functions in Go."
// This is *not* valid Go!
func Map(f func(A) B, xs []A) []B {
ys := make([]B, len(xs))
for i, x := range xs {
ys[i] = f(x)
}
return ys
}
Map(func(x int) int { return x * x }, []int{1, 2, 3})
@max-mapper
max-mapper / index.js
Last active May 9, 2021 02:20
fast loading of a large dataset into leveldb
// data comes from here http://stat-computing.org/dataexpo/2009/the-data.html
// download 1994.csv.bz2 and unpack by running: cat 1994.csv.bz2 | bzip2 -d > 1994.csv
// 1994.csv should be ~5.2 million lines and 500MB
// importing all rows into leveldb took ~50 seconds on my machine
// there are two main techniques at work here:
// 1: never create JS objects, leave the data as binary the entire time (binary-split does this)
// 2: group lines into 16 MB batches, to take advantage of leveldbs batch API (byte-stream does this)
var level = require('level')
@ashleybot
ashleybot / D3HTMLtable.js
Created February 21, 2012 05:22
Using D3.js to present XML as HTML Table
d3.xml("/data/GoldenGate2012Results.xml", function(xml) {
var runners = d3.select(xml).selectAll("runner")[0];
var table = d3.select("#presentation").append("table").attr("class","grid");
var thead = table.append("thead");
thead.append("th").text("Gender");
thead.append("th").text("City");
thead.append("th").text("Time");
thead.append("th").text("Pace");
@sstephenson
sstephenson / gist:1120938
Created August 2, 2011 19:08
Quick guide to installing rbenv
# Clone rbenv into ~/.rbenv
git clone git@github.com:sstephenson/rbenv.git ~/.rbenv
# Add rbenv to your PATH
# NOTE: rbenv is *NOT* compatible with rvm, so you'll need to
# remove rvm from your profile if it's present. (This is because
# rvm overrides the `gem` command.)
echo 'export PATH="$HOME/.rbenv/bin:$HOME/.rbenv/shims:$PATH"' >> ~/.bash_profile
exec $SHELL