Skip to content

Instantly share code, notes, and snippets.

View graup's full-sized avatar

Paul Grau graup

View GitHub Profile
@graup
graup / Animated SVG to WEBP.md
Last active April 16, 2024 12:00
Convert animated SVG into animated WEBP

We use playwright to capture screenshots of our animated SVG (or really anything that can be loaded into a browser), then use sharp to convert pngs into webps, then node-webpmux to create the animated webp.

  1. Install dependencies
  2. yarn ts-node capture.ts
  3. yarn ts-node writeWebP.ts
try {
const result = await timedAsync(originalAsyncFunction, {
slow: () => {
console.log('operation is slow and still going on'):
},
fast: () => {
console.log('operation finished quickly'):
}
});
} catch (e) {
const SLOW_LOAD_TIME = 1500;
function waitOrLoad(callbackIfSlow, maximumLoadTime = SLOW_LOAD_TIME) {
const timeout = setTimeout(callbackIfSlow, maximumLoadTime);
return function loadingFinished() {
clearTimeout(timeout);
}
}
async function doSomething() {
const FAST_LOAD_TIME = 500;
const clamp = (num, min, max) => Math.min(Math.max(num, min), max);
function loadAndWait(minimumLoadTime = FAST_LOAD_TIME) {
const startedLoading = + new Date();
return function waitMinimum() {
return new Promise(resolve => {
const loadDuration = new Date() - startedLoading;
import JSBI from 'jsbi';
const useNativeBigIntsIfAvailable = true;
if (useNativeBigIntsIfAvailable) JSBI.useNativeBigIntsIfAvailable();
const BigInt = JSBI.BigInt
DataView.prototype._setBigUint64 = DataView.prototype.setBigUint64;
DataView.prototype.setBigUint64 = function(byteOffset, value, littleEndian) {
if (typeof value === 'bigint' && typeof this._setBigUint64 !== 'undefined') {
// the original native implementation for bigint
this._setBigUint64(byteOffset, value, littleEndian);
from itertools import permutations
# Generate permutations
p = list(permutations('1234', 4))
# Flip table for display in columns
flipped = [[p[j][i] for j in range(4*3*2)] for i in range(4)]
# Print table
print("\n".join([" ".join(row) for row in flipped]))
def balanced_latin_squares(n):
l = [[((j//2+1 if j%2 else n-j//2) + i) % n + 1 for j in range(n)] for i in range(n)]
if n % 2: # Repeat reversed for odd n
l += [seq[::-1] for seq in l]
return l
@graup
graup / decimal.py
Created January 10, 2017 16:56
The reason why you should be careful with currency amount calculation
>>> from decimal import *
>>> VAT_RATE = Decimal('0.19')
>>> amount = Decimal('12.12')
>>> ((amount / (VAT_RATE + 1)).quantize(Decimal('1.00')) * (1+VAT_RATE)).quantize(Decimal('1.00'))
Decimal('12.11')
>>> ((amount / (VAT_RATE + 1)).quantize(Decimal('1.0000')) * (1+VAT_RATE)).quantize(Decimal('1.00'))
Decimal('12.12')
@graup
graup / lambda.js
Last active November 1, 2016 05:13
console.log('Lambda calculus with ES6 syntax');
console.log(
// (\(x,y).x+y)(1,2) => 1+2 => 3
((x, y) => x + y) (1, 2)
);
console.log(
// ((\x.\y.x+y) 1) 2 => (\y.1+y) 2 => 3
(x => y => x + y) (1) (2)

Keybase proof

I hereby claim:

  • I am graup on github.
  • I am grau (https://keybase.io/grau) on keybase.
  • I have a public key whose fingerprint is 6D57 7918 F01C 3B85 6A61 23AA AFE6 8F55 6DED DABB

To claim this, I am signing this object: