Skip to content

Instantly share code, notes, and snippets.

View wcarss's full-sized avatar

Wyatt Carss wcarss

View GitHub Profile
@wcarss
wcarss / order-by.js
Last active May 13, 2021 15:50
a toy function to sort js arrays with sql-like syntax
// preamble: public domain; no warranty
//
// this is a toy -- it is slow, cannot handle unicode, keys w/
// spaces, nested keys, natural ordering, etc.
// orderBy('key1 [asc|1|desc|-1][, key2 [asc|1|desc|-1]][, etc]')
//
// write sql-like sort orders for arrays of objects, e.g.:
//
@wcarss
wcarss / raymarch.py
Last active March 9, 2021 00:44
diffuse raymarcher from https://ch-st.de/its-ray-marching-march/ ported to python (tested in 2.7 and ~3.8.6)
# original C code from https://ch-st.de/its-ray-marching-march/
# retrieved, roughly ported, and refactored a little on 2021-03-08
from __future__ import print_function
import math
import time
import sys
class Vec3:
@wcarss
wcarss / is_valid_yyyy_mm_dd_date.js
Last active August 26, 2019 21:45
javascript to check if a string is a valid YYYY-MM-DD-style date or datetime. This is more permissive than ISO 8601 but less permissive than the Date() constructor.
/*
* returns true/false on "is this string a valid date/datetime string"
*
* any credit for coolness goes to https://stackoverflow.com/a/44198641,
* and any blame for wrongness goes to me. :)
*
*/
const isValidDate = dateString => {
let date = new Date(dateString); // we're gonna use both :0
return (
@wcarss
wcarss / sort_array_by_array.js
Created January 28, 2019 21:45
sort an array of objects by another array's values in js
const ordering_arr = ["primary", "home", "business", "camp", "outhouse", "mars", "pluto"];
const to_sort = [
{
"address": "1",
"type": "primary"
},
{
"address": "2",
"type": "home"
@wcarss
wcarss / probability_distribution_test.py
Last active April 27, 2018 14:39
A test-file for an algorithm to pick-random values by a per-key distribution
import sys
import random
from collections import defaultdict
# some utility classes for nicer printing + default-ints
class PrettyDefaultDict(defaultdict):
def pprint(self):
lines = []
@wcarss
wcarss / requests
Created July 26, 2016 21:56
200 for a given code, followed by 401s for the same code
2016-07-26T15:33:51-04:00 "POST /auth/oauth/token?client_id=<your id>&client_secret=<your secret>&code=b6f682f2518671329bb73a815949fab59ac01e19ac2105cc8bee2718e64b0892&grant_type=authorization_code&redirect_uri=<yours> HTTP/1.0" 200
2016-07-26T15:34:05-04:00 "POST /auth/oauth/token?client_id=<your id>&client_secret=<your secret>&code=b6f682f2518671329bb73a815949fab59ac01e19ac2105cc8bee2718e64b0892&grant_type=authorization_code&redirect_uri=<yours> HTTP/1.0" 401
2016-07-26T15:38:02-04:00 "POST /auth/oauth/token?client_id=<your id>&client_secret=<your secret>&code=f497768aac61e961c74e488e0768a01337134dd7ff8b6177320a1af476a4934c&grant_type=authorization_code&redirect_uri=<yours> HTTP/1.0" 200
2016-07-26T15:38:24-04:00 "POST /auth/oauth/token?client_id=<your id>&client_secret=<your secret>&code=f497768aac61e961c74e488e0768a01337134dd7ff8b6177320a1af476a4934c&grant_type=authorization_code&redirect_uri=<yours> HTTP/1.0" 401
2016-07-26T15:38:31-04:00 "POST /auth/oauth/token?client_id=<your id>&client_secret=<your se
@wcarss
wcarss / bitwise_and_test.c
Created December 25, 2013 07:23
A test of bitwise and used to increment and wrap access into an array in doom's event-handling.
#include<stdio.h>
int main() {
int SIZE = 10;
int head = 0;
int tail = 0;
head = 3;
tail = 5;
def get_list(resource, base_url = ''):
# hit db in real life
total_items = 45
try:
page_number = int(request.args.get('page', 1))
except ValueError, e:
# we want an int; if we can't make an int, assume page 1
page_number = 1
@wcarss
wcarss / all_subsets.py
Created August 23, 2011 19:43
subset generator
def all_subsets(*args):
from collections import Iterable
from itertools import permutations as permute
if len(args) == 1:
if not isinstance(args[0], Iterable):
args = [args[0]]
else:
args = args[0]
output = []