Skip to content

Instantly share code, notes, and snippets.

@gordonbrander
gordonbrander / viewstore.swift
Last active May 17, 2021 20:48
SwiftUI immutable ViewStore
/// ViewStore acts as a state container, typically for some view over the application's central store.
/// You construct a ViewStore and pass it to a subview. Because it is Equatable, you can
/// make the subview Equatable as well, with `.equatable()`. The subview will then only render
/// when the actual value of the state changes.
struct ViewStore<LocalState, LocalAction>: Equatable
where LocalState: Equatable {
static func == (
lhs: ViewStore<LocalState, LocalAction>,
rhs: ViewStore<LocalState, LocalAction>
) -> Bool {
@gordonbrander
gordonbrander / dirty.js
Last active November 6, 2022 09:19
dirty.js - dirty marking for any JS object
/*
Released under MIT License
Copyright 2020 Gordon Brander
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
const $state = Symbol('state')
const $rendered = Symbol('rendered state')
const $shadow = Symbol('shadow')
const $frame = Symbol('animation frame')
// StoreElement is a deterministic web component,
// inspired loosely by Elm's App Architecture pattern.
export class StoreElement extends HTMLElement {
constructor() {
super()
@gordonbrander
gordonbrander / README.md
Last active April 25, 2023 20:38
microview - data-driven views that render once per frame, at most

Microview

What if... Virtual DOM... but by hand?
aha ha, just kidding...
unless.. ?

Microview is a tiny library for writing efficient data-driven DOM rendering logic by hand. DOM writes are driven by a pure data model. Using Microview, you can freely "bash the dom". Writes will be batched — they'll only happen once per animationframe, and only if the data model changes.

@gordonbrander
gordonbrander / framescheduler.js
Created October 20, 2020 17:34
FrameScheduler - a frame scheduler that will call callback at most once per frame.
// Creates a frame scheduler that will call callback at most once per frame.
export const FrameScheduler = callback => {
let isScheduled = false
const draw = t => {
isScheduled = false
callback(t)
}
const schedule = () => {
@gordonbrander
gordonbrander / contract.js
Last active February 10, 2020 18:45
Racket-style contracts code sketch
// 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 00:29
Starting - creative prompts
# Prompts for beginning new projects
start:
- "#5ps#"
- "#heart#"
- "#disirability_feasibility_viability#"
- "#practical_step#"
- "#build#"
- "#story#"
- "#ooda#"
@gordonbrander
gordonbrander / tolstoy-confession-epilogue.md
Last active February 21, 2019 01:45
Tolstoy, A Confession (Epilogue)

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 November 6, 2022 07:36
textiness.py — script to filter text based on how "texty" it is

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 November 6, 2022 07:35
rest - decorate functions so you can bind "rest" arguments with f.rest().
// 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)