Skip to content

Instantly share code, notes, and snippets.

View JoshuaGross's full-sized avatar

Joshua Gross JoshuaGross

View GitHub Profile
@JoshuaGross
JoshuaGross / sort-by-version.js
Created January 13, 2016 00:26
Sort semantic version numbers.
// Sort semantic version numbers that look like this:
// 0.21
// 0.21.4.3
// 0.21.4-18bdje
function (a, b) {
a = a.split(/[-\.]/);
b = b.split(/[-\.]/);
for (var i = 0; i < a.length && i < b.length; i++) {
if (parseFloat(a[i]) > parseFloat(b[i])) {
return 1;
@JoshuaGross
JoshuaGross / devrandom-uid-8.sh
Created February 9, 2016 00:02
Random alphanum identifier, 8 chars
cat /dev/random | LC_CTYPE=C tr -dc 'a-zA-Z0-9' | fold -w 8
@JoshuaGross
JoshuaGross / node-cli-json.sh
Created April 6, 2016 23:03
Process JSON on the command-line with Node
echo "{ \"param\": 234 }" | node -e 'var input = ""; process.stdin.on("data", function (d) { input += d.toString(); }); process.stdin.on("close", function () { console.log(JSON.parse(input).param); });'
@JoshuaGross
JoshuaGross / typeclass.hs
Created May 14, 2016 04:26
Haskell - important typeclasses and cotypeclasses
-- none of this is terribly interesting, mostly here for reference
class Functor (f :: * -> *) where
fmap :: (a -> b) -> f a -> f b
(<$) :: a -> f b -> f a
-- The same as Functor!
-- http://stackoverflow.com/questions/34732571/why-there-is-no-cofunctor-typeclass-in-haskell/34732721
class Cofunctor (f :: * -> *) where
fmap :: (b -> a) -> f b -> f a
@JoshuaGross
JoshuaGross / sdiff_empty.c
Last active September 22, 2016 21:56 — forked from fnoble/sdiff_empty.c
single diff measurement question (intersection of two structs via prns)
// Compiles with gcc -O0 sdiff_empty.c
// After running your sdiff function once, it will run
// a second time on canned data and compare your results with
// known outputs.
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <inttypes.h>
@JoshuaGross
JoshuaGross / buffer-to-shell.md
Created September 27, 2016 17:07
Translate Node Buffer into shell-friendly binary echo
console.log('echo -e "' + buf.reduce((s, x) => s + '\\x' + x, '') + '"');

Shell:

echo -e "\x85\x0\x2\x68\x0\x32\x194\x41\x247\x13\x33\x183\x212\x148\x92\x165\x68\x193\x4\x214\x164\x10\x90\x65\x80\x193\x114\x14\x255\x164\x18\x165\x77\x65\x0\x0\x9\x0\x50\x55"
@JoshuaGross
JoshuaGross / pg-running-queries.sql
Created September 30, 2016 22:04
List running queries in PostGres
select * from pg_stat_activity;
@JoshuaGross
JoshuaGross / pg-datediff.sql
Created September 30, 2016 22:11
PostGres custom DATEDIFF function (from http://www.sqlines.com/postgresql/how-to/datediff)
CREATE OR REPLACE FUNCTION DateDiff (units VARCHAR(30), start_t TIMESTAMP, end_t TIMESTAMP)
RETURNS INT AS $$
DECLARE
diff_interval INTERVAL;
diff INT = 0;
years_diff INT = 0;
BEGIN
IF units IN ('yy', 'yyyy', 'year', 'mm', 'm', 'month') THEN
years_diff = DATE_PART('year', end_t) - DATE_PART('year', start_t);
@JoshuaGross
JoshuaGross / pg-running-queries-age.sql
Created September 30, 2016 22:12
PG running queries with age in seconds
# Requires pg-datediff: https://gist.github.com/JoshuaGross/18b9bb1db8021efc88884cbd8dc8fddb
select datid, query, DateDiff('second', query_start::timestamp, NOW()::timestamp) from pg_stat_activity;
@JoshuaGross
JoshuaGross / README.md
Created November 17, 2016 23:31
Having trouble with networking in a Docker container?

Recently I've experienced strange issues with networking inside of Docker containers - like being unable to ping google.com from inside a container, and all of the common suggestions around DNS resolution not helping.

It turns out that Docker will copy your computer's default DNS settings into a container. If you change your DNS settings at some point - either manually or because your machine accepts defaults from your router/provider - the settings inside your cached Docker layers may no longer match what's on your machine, and certain DNS resolutions will fail in unusual ways.

My fix: docker run -it {image} /bin/bash manually (for example, docker run -it python:2.7 bash) to find the root image that is unable to resolve the domain you're interested in, and then remove the culprit with docker rmi {hash}.