Skip to content

Instantly share code, notes, and snippets.

View jsocol's full-sized avatar
🙃
for oss: hoping to get back to a monthly check-in/update cadence

James Socol jsocol

🙃
for oss: hoping to get back to a monthly check-in/update cadence
View GitHub Profile
@jsocol
jsocol / fib.go
Created August 12, 2020 16:43
People love using Fibonacci as an example of recursion but the closed form solution is great
package main
import (
"fmt"
"math"
)
const Psi = -1.0 / math.Phi
var Sqrt5 = math.Pow(5.0, 0.5)
@jsocol
jsocol / rand_gems.py
Created June 22, 2020 01:57
Generate random gemstones for D&D 5e
"""Generate random gemstones for D&D 5e
It always bothered me that the tables in the DMG won't let you randomly
generate a diamond worth 50gp, or 300gp, or 500gp, when these are all common
spell components.
This script generates random gemstones and values. It considers that you might
have a small diamond worth 50gp, but probably not a giant azurite worth 5000gp.
First, we get a base and a multiplier. A base could be e.g. 10, 100, 1000. A
@jsocol
jsocol / dice.py
Last active June 22, 2020 01:58
A Monte Carlo dice roller to investigate ability score generation methods and other probability questions
import argparse
import random
__all__ = ['d4', 'd6', 'd8', 'd10', 'd12', 'd20', 'd100']
class D():
"""A die.
@jsocol
jsocol / ex_test.go
Created July 22, 2019 19:13
#golang test subcases
package ex
import (
"fmt"
"testing"
)
func Multiplier(in int) int {
return in * 2
}
import functools
import operator
import random
class D():
"""A die.
Simulates an n-sided die, e.g. to create a d6, pass 6:
@jsocol
jsocol / finally.js
Last active April 23, 2018 22:07
finally example
async function batchFunctionFinally(commands) {
const batchStats = this.startBatch(commands)
let error = null
let response = null
try {
response = await this.redis.multi(commands)
} catch (e) {
error = e
throw e
} finally {
#!/usr/bin/env python
from __future__ import absolute_import, print_function, unicode_literals
import argparse
import random
import re
import string
sr = random.SystemRandom()
@jsocol
jsocol / async.js
Last active April 14, 2017 12:19
throwing from callbacks
// Logs 'rejected Error: sync error'
const p = new Promise((resolve, reject) => {
throw new Error('sync error');
}).then(null, e => console.log('rejected', e));
// q never settles, uncaught error
const q = new Promise((resolve, reject) => {
setImmediate(() => { throw new Error('async error'); });
}).then(null, e => console.log('rejected', e));
from __future__ import division, print_function, unicode_literals
import operator
import re
import sys
WHITESPACE_RE = re.compile(r'\s+')
NUMBER_RE = re.compile(r'-?\d+(\.\d+)?')
OPERATORS = {
'use strict';
var format = require('util').format;
function Logger() {
}
Logger.prototype.log = function $$Logger_log(msg) {
var context = this.getContext();
console.log(format('%s:%s:%s(%s): %s', context.pathname, context.lno, context.col, context.func, msg));