Skip to content

Instantly share code, notes, and snippets.

View jhurliman's full-sized avatar
🐨

John Hurliman jhurliman

🐨
View GitHub Profile
@jhurliman
jhurliman / include.js
Created March 16, 2012 23:07
Asynchronous JavaScript Loader
// Async Loader v1.0.0
var include = function(scripts, done) {
if (typeof scripts === 'string') scripts = [scripts];
var loading = function() { scripts.pop() && !scripts.length && done && done.call(); };
scripts.forEach(function (script) {
var el = document.createElement('script');
el.addEventListener('load', loading), el.async = true, el.src = script;
document.head.appendChild(el);
});
return arguments.callee;
@jhurliman
jhurliman / mutex.js
Created July 14, 2012 21:17 — forked from elarkin/Mutex.js
A simple mutex library for node.js
var EventEmitter = require('events').EventEmitter;
module.exports = function() {
var queue = new EventEmitter();
var locked = false;
this.lock = function lock(fn) {
if (locked) {
queue.once('ready', function() { lock(fn); });
} else {
@jhurliman
jhurliman / stacktrace.php
Created July 20, 2012 00:03
Pretty print stack trace in PHP
function stackTrace() {
$stack = debug_backtrace();
$output = 'Stack trace:' . PHP_EOL;
$stackLen = count($stack);
for ($i = 1; $i < $stackLen; $i++) {
$entry = $stack[$i];
$func = $entry['function'] . '(';
$argsLen = count($entry['args']);
@jhurliman
jhurliman / getcctype.js
Last active December 11, 2015 08:58
Detect credit card type based on the number
function getCCType(ccnumber) {
var visaRE = /^4[0-9]{12}(?:[0-9]{3})?$/;
var mastercardRE = /^5[1-5][0-9]{14}$/;
var amexRE = /^3[47][0-9]{13}$/;
var discoverRE = /^6(?:011|5[0-9]{2})[0-9]{12}$/;
var dinersclubRE = /^3(?:0[0-5]|[68][0-9])[0-9]{11}$/;
var jcbRE = /^(?:2131|1800|35\d{3})\d{11}$/;
ccnumber = (ccnumber || '').replace(/[ -]/g, '');
if (ccnumber.match(visaRE)) return 'visa';
@jhurliman
jhurliman / bitArray.js
Created March 18, 2013 22:27
Store/retrieve bits in JavaScript number arrays
function BitArray(options) {
var USABLE_BITS = 32;
if (options.data) {
this.data = options.data;
} else if (options.bits) {
var count = Math.ceil(options.bits / USABLE_BITS);
this.data = new Array(count);
for (var i = 0; i < count; i++)
public synchronized float[] solveWithDeviceMemory() {
// Allocate native (device) memory for the input data
CLBuffer<Float> bufIn = _context.createFloatBuffer(CLMem.Usage.Input, _input.length);
// Allocate native (device) memory for the output data
CLBuffer<Float> bufOut = _context.createFloatBuffer(CLMem.Usage.Output, _input.length);
// Map input/output buffers for implicit copy
Pointer<Float> ptrIn = bufIn.map(_queue, CLMem.MapFlags.Write);
Pointer<Float> ptrOut = bufOut.map(_queue, CLMem.MapFlags.Read);
@jhurliman
jhurliman / fbdump.py
Created June 3, 2013 03:54
Dump recent Facebook profile info, likes, photos, and posts to JSON text files
import urllib2
import json
FB_TOKEN = '...'
MAX_PAGES = 10
def scrape_page(page):
all_data = []
url = ('https://graph.facebook.com' + page + '?limit=50&access_token=' +
@jhurliman
jhurliman / cloudformation.yaml
Created July 16, 2013 22:19
Run CloudFormation via Ansible
- name: CloudFormation deployment
hosts: local
connection: local
user: root
gather_facts: false
tags:
- initialize
tasks:
@jhurliman
jhurliman / readCSV.js
Last active December 26, 2015 11:29
Asynchronously iterate over a CSV file one line at a time
function readCSV(filename, eachCallback, doneCallback) {
var error;
var reader = require('readline').createInterface({
input: require('fs').createReadStream(filename),
output: null,
terminal: false
});
reader.on('line', function(line) {
@jhurliman
jhurliman / convolve.js
Last active February 6, 2023 12:39
1D convolution
/**
* Copyright 2017 John Hurliman <jhurliman.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*