Skip to content

Instantly share code, notes, and snippets.

View NickBeeuwsaert's full-sized avatar

Nick Beeuwsaert NickBeeuwsaert

View GitHub Profile
@NickBeeuwsaert
NickBeeuwsaert / package.json
Created October 15, 2020 00:01
Sample to demonstrate microbundle not working with typescript and css modules
{
"scripts": {
"build:js": "microbundle -i test.js -o js_out/ -f modern",
"build:ts": "microbundle -i test.ts -o ts_out/ -f modern"
},
"dependencies": {
"microbundle": "^0.12.4"
}
}
@NickBeeuwsaert
NickBeeuwsaert / context.py
Last active October 4, 2019 14:56
Use react's context API in python!
from contextlib import contextmanager
import threading
def use_context(context):
with context.consumer() as value:
return value
class Context(threading.local):
def __init__(self, default = None):
self.value = default
@NickBeeuwsaert
NickBeeuwsaert / v1.js
Created May 12, 2019 02:14
Cartesian Product
function* cartesianProduct(...iterables) {
const pools = iterables.map(function(iterable) {
const pool = Array.from(iterable);
return {pool, length: pool.length, counter: 0};
});
let iterations = pools.reduce(
(total, {length}) => total * length,
1
);
@NickBeeuwsaert
NickBeeuwsaert / yay_plists.jsx
Last active September 2, 2016 04:34
Write your JS objects as plists using JSX!
let up = (msg) => new Error(msg);
function zip(...args) {
args = args.filter(arg => 'length' in args);
let result = [];
let minLength = Math.min(...args.map(arg => arg.length));
if(args.length === 0) return result;
for(let i = 0; i < minLength; i++) {
@NickBeeuwsaert
NickBeeuwsaert / serialize.js
Created April 1, 2016 03:27
Serialize a element containing form controls
export function serialize(element) {
// I could probably use form.elements here
// But I want this to work if elements are just in a div
const controls = Array.from(element.querySelectorAll("input, textarea, select, button"));
const withoutName = (control) => !!control.name;
const disabled = (control) => !control.disabled;
const removeFiles = (control) => !control.matches('input[type="file"]');
const checkboxes = (control) => control.matches('input[type="checkbox"], input[type="radio"]')?control.checked:true;
@NickBeeuwsaert
NickBeeuwsaert / range.js
Created March 31, 2016 03:16
JS range function like in python
function range(start, stop, step=1) {
let length = 0;
if(arguments.length == 1) {
[start, stop, step] = [0, start, 1];
}
[start, stop, step] = [start, stop, step].map(Math.floor);
if(step === 0)
throw new TypeError("step cannot be zero");
@NickBeeuwsaert
NickBeeuwsaert / example.php
Last active March 19, 2016 03:39
Some functional array utilities in PHP. (TBH I just wanted to see how many times I could not type `for`)
<?php
// Data like one might recieve from a form
$data = [
'id' => [
10,
20,
100,
],
'first_name' => [
"Nick",
@NickBeeuwsaert
NickBeeuwsaert / main.py
Last active February 17, 2022 19:43
OpenGL Shader example using python
#!/usr/bin/env python
from __future__ import division
from OpenGL.GL import *
import numpy as np
import math
import pygame
import textwrap
from PIL import Image
vertex_shader_source = textwrap.dedent("""\
@NickBeeuwsaert
NickBeeuwsaert / matrix.py
Last active March 8, 2016 02:38
Crappy MS3D parser
import numpy
import math
def perspective(fovy, aspect, z_near, z_far):
f = 1 / math.tan(math.radians(fovy) / 2)
return numpy.array([[f / aspect, 0, 0, 0],
[ 0, f, 0, 0],
[ 0, 0, (z_far + z_near) / (z_near - z_far), -1],
[ 0, 0, (2*z_far*z_near) / (z_near - z_far), 0]])
@NickBeeuwsaert
NickBeeuwsaert / ccairo.py
Created February 15, 2016 02:47
Using SDL2 and Cairo from python
"""I'm using ctypes to interact with cairo instead of pycairo
because pycairo hasn't been updated in 4 years and has features missing
in python3.
"""
import ctypes
import ctypes.util
from ctypes import c_void_p, c_double
# Enumerate pixel formats
FORMAT_INVALID = -1