Skip to content

Instantly share code, notes, and snippets.

Avatar

Gordon Brander gordonbrander

View GitHub Profile
@gordonbrander
gordonbrander / contract.js
Last active Feb 10, 2020
Racket-style contracts code sketch
View contract.js
// Simple runtime type checking.
export class ContractViolation extends Error {}
export const contract = predicate => value => {
if (predicate(value)) {
return value
} else {
throw new ContractViolation(
`Contract violation. Expected ${predicate}. Given ${value}.`
)
@gordonbrander
gordonbrander / starting.yaml
Created May 12, 2019
Starting - creative prompts
View starting.yaml
# Prompts for beginning new projects
start:
- "#5ps#"
- "#heart#"
- "#disirability_feasibility_viability#"
- "#practical_step#"
- "#build#"
- "#story#"
- "#ooda#"
@gordonbrander
gordonbrander / tolstoy-confession-epilogue.md
Last active Feb 21, 2019
Tolstoy, A Confession (Epilogue)
View tolstoy-confession-epilogue.md

Leo Tolstoy, A Confession — Epilogue

Here is the dream: I see that I am lying in bed. Feeling neither good nor bad, I am lying on my back. But I begin to wonder whether it is a good thing for me to be lying there; and it seems to me that there is something wrong with my legs; whether they are too short or uneven, I do not know, but there is something awkward about them. As I start to move my legs, I begin to wonder how and on what I am lying, something that up till now had not entered my mind. Looking about my bed, I see that I am lying on some cords woven together and attached to the sides of the bed. My heels are resting on one of the cords and my lower legs on another in an uncomfortable way.

Somehow I know that these cords can be shifted. Moving one leg, I push away the furthest cord. It seems to me that it will be more comfortable that way. But I have pushed it too far away; I try to catch it, but this movement causes another cord to slip out from under my legs, leaving them hanging down. I rearran

@gordonbrander
gordonbrander / Textiness.md
Last active Feb 14, 2019
textiness.py — script to filter text based on how "texty" it is
View Textiness.md

Textiness

Example using pdfminer to extract text and then filter out lines that aren't "texty" enough.

pdf2txt.py something.pdf | ./textiness.py > temp.txt

For example, I'm personally using this to extract the text of the IPCC report on 1.5°C:

@gordonbrander
gordonbrander / rest.js
Last active Sep 14, 2018
rest - decorate functions so you can bind "rest" arguments with f.rest().
View rest.js
// Decorate a function with a method that allows you to bind "rest" args
// (everything except the first argument).
// Usage:
//
// const f = rest((a, b, c) => a + b + c)
// const fbc = f.rest("b", "c")
// fbc("a")
// // "abc"
const rest = f => {
f.rest = (...rest) => v => f(v, ...rest)
@gordonbrander
gordonbrander / event-delegate.js
Last active Oct 16, 2018
addEventDelegate() - easy event delegation
View event-delegate.js
// Copyright 2018 Gordon Brander
// Released under MIT License https://opensource.org/licenses/MIT
const closest = (el, selector) =>
el.closest ? el.closest(selector) : null;
/**
* Delegate event handling to a parent element.
* @arg {Element} el - the parent element that we will be delegating handling to.
* @arg {string} selector - CSS selector of elements that should receive events.
@gordonbrander
gordonbrander / ruleset.example.js
Last active Aug 6, 2020
ruleset.js — parse style-attribute-like strings.
View ruleset.example.js
import {Schema, string, bool, number, listOf, optional, cssUnit4} from './ruleset.js';
const parse = Schema({
title: string,
isHidden: optional(bool, false),
numbers: listOf(number)
});
parse("title: 8 Beautiful Notes; numbers: 1, 2, 3, 4, 5, 6, 7, 8")
// > {title: "8 Beautiful Notes", numbers: [1, 2, 3...], isHidden: false}
@gordonbrander
gordonbrander / select.js
Last active Apr 16, 2018
select — sugarfree D3-style enter/update/exit DOM manipulation, using a simple function instead of method chaining
View select.js
// D3-style select-baed DOM updating.
//
// Rather than relying on jQuery-style method-chaining to update the DOM,
// we use a "view" object, an ordinary object that contains the following
// functions:
//
// - `enter(datum) -> Element` returns a DOM node for new elements.
// Element will be appended to parent.
// - `update(el, datum, old)` handles mutating an element in response to
// changes in data. `old` is the last-known data for this element.
@gordonbrander
gordonbrander / preload.js
Last active Apr 8, 2018
Preload images with promises
View preload.js
// Tip: use with await.
// const preloaded = await preloadAll(['img1.jpg', 'img2.jpg'])
export const preload = src => new Promise((resolve, reject) => {
const img = new Image()
img.onload = resolve
img.onerror = reject
img.src = src
})
View frame.js
// Promise for an animation frame.
// Tip: use with `await`.
export const frame = () => new Promise(requestAnimationFrame)
You can’t perform that action at this time.