Skip to content

Instantly share code, notes, and snippets.

View jbreckmckye's full-sized avatar
🛸

Jimmy Breck-McKye jbreckmckye

🛸
View GitHub Profile
@jbreckmckye
jbreckmckye / fun.ts
Created October 11, 2020 19:01
Fun with distributions
// Fun with distributions
// You probably know that TypeScript has union types
type StringsAndNumbers = string | number;
// You also probably know about 'discriminated unions' or 'tagged unions',
// representing different kinds of structs
type Eventing =
@jbreckmckye
jbreckmckye / British PC Layout.keylayout
Created June 3, 2020 15:19
MacOS British PC keyboard mapping (put this in Library/Keyboard Layouts/)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE keyboard SYSTEM "file://localhost/System/Library/DTDs/KeyboardLayout.dtd">
<keyboard group="0" id="5001" name="British PC Layout" maxout="2">
<layouts>
<layout first="0" last="0" modifiers="48" mapSet="312" />
</layouts>
<modifierMap id="48" defaultIndex="0">
<keyMapSelect mapIndex="0">
<modifier keys="" />
</keyMapSelect>
## Personas
## Subjects/Verbs :: Characters/Action
## Sentences should flow old information -> new
## Keep topics coherent
## Use topics to assign responsibility
@jbreckmckye
jbreckmckye / ghcide-and-vscode.md
Created February 24, 2020 19:15
GHCIDE & VSCode setup

GHCIDE & VSCode setup

1. Install Stack

curl -sSL https://get.haskellstack.org/ | sh
stack upgrade

Then ensure ~/.local/bin is on your $PATH.

@jbreckmckye
jbreckmckye / group.js
Last active October 10, 2019 15:48
Group objects by a string field
function group (rows, discriminant) {
const asMap = rows.reduce(
(acc, item) => {
const key = item[discriminant]
return {
...acc,
[key]: acc[key] ? [...acc[key], item] : [item]
}
},
{}
@jbreckmckye
jbreckmckye / groupedThrottle.js
Created April 24, 2019 16:57
Grouped throttle function
function throttleGroup(fn, count, delay) {
let queue = [];
let nextDrain = null;
function scheduleDrain() {
nextDrain = window.setTimeout(drainQueue, delay);
}
function drainQueue() {
const groupSize = Math.min(count, queue.length);
// Get a WebGL context
import {Perhaps, None, Some, Any} from 'highly-questionable';
const canvas = document.createElement('canvas');
const context = Perhaps
.of(canvas.getContext('webgl2'))
.or(canvas.getContext('webgl'))
.orFrom(()=> canvas.getContext('experimental-webgl'));
@jbreckmckye
jbreckmckye / AjaxTextureLoader.js
Created April 11, 2017 13:23
Loading THREE textures with onProgress events
const THREE = require('three');
function AjaxTextureLoader() {
/**
* Three's texture loader doesn't support onProgress events, because it uses image tags under the hood.
*
* A simple workaround is to AJAX the file into the cache with a FileLoader, then extract that into a
* texture with a separate TextureLoader call.
*/
@jbreckmckye
jbreckmckye / promise-first-truthy.ts
Created December 6, 2018 12:11
Get first truthy promise result
function firstTruthy<T>(promises: Array<Promise<any>>): Promise<T|null> {
return new Promise((resolve, reject) => {
// If any promise returns truthy value, immediately resolve with it
promises.forEach(async promise => {
const result = await promise;
if (!!result) resolve(result);
});
// If any promise rejects, immediately throw the error
Promise.race(promises).catch(reject);
@jbreckmckye
jbreckmckye / watchr.d.ts
Created November 22, 2018 15:56
TypeScript definition for watchr js
/// <reference types="node" />
declare module 'watchr' {
import {Stats, FSWatcher} from 'fs';
import {EventEmitter} from 'events';
export type ChangeType = 'update' | 'create' | 'delete';
export type State = 'pending' | 'active' | 'deleted' | 'closed';