Skip to content

Instantly share code, notes, and snippets.

@nickjacob
nickjacob / parallel.sh
Last active April 11, 2022 22:06
bash parallelization template
#!/bin/bash
# Parallelize the execution of a command
######
CHUNK_COUNT=0
parallel_configure () {
if [ $# -eq 0 ]; then
echo "usage: <tmp dir> <tmp prefix> <num chunks>"
fi
@nickjacob
nickjacob / anagram.js
Created June 24, 2013 23:27
just simple how 2 find anagrams
// space complexity: O(n) -- the hashmap
// time complexity: ~O(2n); iterate both strings once
// worse case: O(2n)
function areAnagrams(a, b) {
var len = a.length, chars = {}, i = 0;
if (len !== b.length) return false;
for(; i < len; i++) {
chars[a[i].toLowerCase()] = true;
}
@nickjacob
nickjacob / framekiller.js
Created June 25, 2013 17:04
really basic framekiller
;(function (window) {
if (window.top !== window.self) {
killFrame();
}
var _frame_ = "<html><head><title>Not Allowed</title>\
</head><body><h1>We don't allow This</h1></body></html>";
function killFrame() {
document.open();
@nickjacob
nickjacob / is_a.js
Created July 1, 2013 23:01
rly simple self-defining test function snippet
/* Just define a simple is_a fn and populate it with functions
* that correspond to the primary JS objects; interface is
*
* is_a.array([]) >> true
*/
;(function (window, undefined){
var upFirst = /^(\w)/,
toS = Object.prototype.toString,
builtIns = ['Array', 'Object', 'Number', 'String', 'Window', 'RegExp', 'Function'];
@nickjacob
nickjacob / mini_tpl.js
Last active December 26, 2015 04:29
Newer version of my mini templating
;(function (window, document, undefined) {
var m = window.micro = {},
whiteSpaceRxp = /[\r\n\t\s]/g,
quotesRxp = /['"]/g,
attrRxp = /<%=\s*([\w_\$\.]+)\s*%>/ig;
// Convert template to use consistent quotes, remove unnecessary whitespace
// returns: the normalized template string
function normalizeTemplate(tpl) {
return tpl.replace(whiteSpaceRxp, ' ').replace(quotesRxp, "'");
@nickjacob
nickjacob / safe_eval.js
Created November 13, 2013 00:45
an idea for a safe eval function
function safe_eval(code, ctx) {
return (new Function (["window", "undefined"], "try { " + code + " } catch (e){ return e; }"))(ctx);
}
function restrict (base, perm) {
var out = {}, k = null,
READ = 'read',
WRITE = 'write',
RW = 'readwrite';
@nickjacob
nickjacob / label-output.sh
Created January 24, 2014 05:44
bash function to prepend text to output of script (example use in here too)
function label_stream()
{
perl -sne '$|=1; print "[$cmd]\t$_"' -- -cmd="$1"
}
# example use
ls -lafh . | label_stream "ls"
# output
[ls] drwxr-xr-x 6 nickjacob staff 204B Nov 11 2012 Processing/
@nickjacob
nickjacob / coinbase-price
Created January 28, 2014 20:46
check and update buy/sell price on coinbase
#!/usr/bin/env ruby
# get coinbase price
###
require 'coinbase'
require 'optparse'
# your API key - https://coinbase.com/docs/api/authentication
COINBASE_API = ''
UP_ARROW = "\u25B2".encode('utf-8')
DOWN_ARROW = "\u25BC".encode('utf-8')
@nickjacob
nickjacob / leveldb_merge.pl
Created March 13, 2014 08:07
merge a series of level dbs to provide an output db of a specified length
#!/usr/bin/env perl
use strict;
use warnings;
use Tie::LevelDB;
sub foreach_key {
my ($db_name, $block) = @_;
my $db = new Tie::LevelDB::DB($db_name);
my $it = $db->NewIterator;
@nickjacob
nickjacob / upgrade_starcluster.sh
Created March 22, 2014 23:39
script to safely upgrade starcluster & dependencies
#!/usr/bin/env bash
# upgrade starcluster
####
mute_run ()
{
$* >/dev/null 2>&1
}
cecho ()
{