Skip to content

Instantly share code, notes, and snippets.

View jhurliman's full-sized avatar
🐨

John Hurliman jhurliman

🐨
View GitHub Profile
@jhurliman
jhurliman / hash-buckets.js
Created January 4, 2016 19:25
Demonstration of uniformly allocating integers into a fixed number of hash buckets
import * as math from 'mathjs'
const MINUTES_PER_DAY = 1440
const REFRESH_MINUTES = 1
const HASH_BUCKETS = MINUTES_PER_DAY / REFRESH_MINUTES
const MAX_IDS = 291554
main()
@jhurliman
jhurliman / hackernews-headlines.sh
Created January 15, 2016 20:13
XScreenSaver Apple II Emulator - Hacker News Headlines
curl -silent https://news.ycombinator.com/rss | grep -no '<item><title>[^<]*' | sed 's/<item><title>//'
@jhurliman
jhurliman / mapAsync.js
Created January 25, 2016 05:06
ES7 implementation of Array.map() and the equivalent for objects
import PromisePool from 'es6-promise-pool'
/**
* Like Array.map() but supports promises and concurrency limiting.
* @param {array} array - Input array to map.
* @param {function} asyncFn(currentValue, index, array) - Called once for each
* value in the array.
* @param {number} [concurrencyLimit] - Optional cap on the number of
* asynchronous methods to execute at once.
* @returns {array} An array containing the mapped output values.
@jhurliman
jhurliman / es7-setup.md
Last active January 26, 2016 00:23
ES6/ES7 setup for sandbox development of modern code

These instructions will allow you to write and run one-off node.js scripts using modern ECMAScript syntax, including async/await.

  1. Install Babel globally

npm install -g babel-cli

  1. Add a .babelrc file to the folder holding your code containing the following:

{ "presets": [ "es2015", "stage-3" ] }

@jhurliman
jhurliman / cesium-oakland.js
Created February 8, 2016 19:25
Cesium.js viewer for Oakland buildings
var viewer = new Cesium.Viewer('cesiumContainer', {timeline: false, animation: false});
var promise = Cesium.GeoJsonDataSource.load(
'https://s3.amazonaws.com/jhurliman/oakland-building-footprints.geojson');
promise.then(function(dataSource) {
viewer.dataSources.add(dataSource);
// Get the array of buildings
var entities = dataSource.entities.values;
@jhurliman
jhurliman / recent-ios-reviews-score.sh
Last active April 3, 2016 19:26
Return the average review score based on recent reviews for an app store app
#!/usr/bin/env bash
COUNTRY=us
APPID=1081557588
curl -s "https://itunes.apple.com/${COUNTRY}/rss/customerreviews/id=${APPID}/sortBy=mostRecent/json" |\
jq '[.feed.entry[]."im:rating".label // empty | tonumber] | add/length'
@jhurliman
jhurliman / binaryninja-dijkstra.cpp
Created December 13, 2016 23:16
Find the shortest distance between source and target basic blocks in Binary Ninja
namespace BN = BinaryNinja;
using std::map;
using std::pair;
using std::priority_queue;
using std::vector;
int32_t dijkstra(vector<BN::Ref<BN::BasicBlock>> blocks, BN::Ref<BN::BasicBlock> source, BN::Ref<BN::BasicBlock> target) {
using path = pair<int32_t, uint64_t>;
@jhurliman
jhurliman / valueAtPath.js
Created April 22, 2017 23:20
Safely retrieve a value from a nested object with a path like 'a.b.c'
/**
* Safely retrieve a value from a nested object using a string path such as
* 'a.b.c' or an array ['a', 'b', 'c'].
*/
function valueAtPath (obj, path) {
if (!Array.isArray(path)) {
if (!path) return obj
path = path.split('.')
}
@jhurliman
jhurliman / allWithProgress.js
Created May 5, 2017 17:30
Wrapper for Promise.all() that adds a progress callback
function allWithProgress(promises, callback) {
let completed = 0
callback(0, promises.length)
promises.forEach(p => {
p.then(() => callback(++completed, promises.length))
})
return Promise.all(promises)
}
@jhurliman
jhurliman / tls_guardvar.cpp
Created May 8, 2018 17:37
Directly access a compiler-generated Thread Local Storage guard variable
// Tested with clang-6.0 on Ubuntu 14.04 using:
// clang --std=c++14 -stdlib=libstdc++ -lstcd++ -fasm-blocks -O3 tls_guardvar.cpp -o tls_guardvar
// I will be genuinely surprised if this works in any other circumstance.
#include <iostream>
struct S {
int x;
S() { x = 42; }
};