Skip to content

Instantly share code, notes, and snippets.

Avatar

Potch potch

View GitHub Profile
@potch
potch / dom.js
Created Apr 30, 2022
small dom generation code, compatible with htm
View dom.js
const set = (el, o) => {
if (o === null) return
if (typeof o === 'function') {
set(el, o(el))
} else if (o instanceof Array) {
el.append(...o)
} else if (o instanceof Promise) {
o.then(r => set(el, r))
} else if (typeof o === 'object' && !(o instanceof Node)) {
Object.entries(o).forEach(p => el.setAttribute(p[0], p[1]));
@potch
potch / listSelector.lua
Last active Mar 16, 2022
Playdate list item selector that unifies d-pad and crank input
View listSelector.lua
-- listSelector.lua
-- by potch
-- MIT License
-- Playdate item selector that unifies d-pad and crank input
-- useful for menus where you want to allow either crank or arrow input.
-- First argument `count` is the number of items.
-- second argument `options` is a table with the following optional fields:
-- `initSelected`: the position of the item selected at start. default is 0.
-- `crankScale`: # of crank degrees for each item. default is 90.
@potch
potch / bayer.lua
Last active Mar 5, 2022
Small Lua library for working with bayer filters for use with playdate.graphics
View bayer.lua
-- bayer.lua v1.0
-- by Potch
-- MIT License
-- hard-code smallest bayer
local bayer2 = { [0] = 0x0, [1] = 0x2, [2] = 0x3, [3] = 0x1 }
-- t should be a 64 entry table of 1 and 0s, indexed at 0
-- returns a 1-indexed table of bytes for use with playdate.graphics.setPattern
local function toBytes(t)
@potch
potch / LICENSE
Created Jul 1, 2020
potch's static site generator. MIT license but I can't recommend it.
View LICENSE
MIT License
Copyright (c) 2020 Potch
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:
@potch
potch / procedure-fuzzer.js
Created Jan 3, 2020
procedure solver for Advent of Code 2019's Day 17
View procedure-fuzzer.js
let input = 'L,8,R,10,L,10,R,10,L,8,L,8,L,10,L,8,R,10,L,10,L,4,L,6,L,8,L,8,R,10,L,8,L,8,L,10,L,4,L,6,L,8,L,8,L,8,R,10,L,10,L,4,L,6,L,8,L,8,R,10,L,8,L,8,L,10,L,4,L,6,L,8,L,8,';
let out = [];
// convert every substr occurence in `arr` to a symbol in `str`
// toSymbols('12345123',['123','45']) yields 'ABA'
function toSymbols(str, arr) {
arr.forEach((s, i) => {
let re = new RegExp(s, 'g');
str = str.replace(re, String.fromCharCode(65 + i));
});
@potch
potch / tractorbeam.js
Created Dec 19, 2019
you really got a hold on me
View tractorbeam.js
const fs = require('fs');
const { IntCode, test } = require('../IntCode.js');
class Grid {
constructor({data, width, height}) {
if (data) {
this.data = data;
this.width = data[0].length;
this.height = data.length;
}
@potch
potch / findSubSequences.js
Last active Dec 17, 2019
Find the subsequences from Advent of Code 2019 Day 17 Part 2 by random sampling
View findSubSequences.js
let input = 'L,8,R,10,L,10,R,10,L,8,L,8,L,10,L,8,R,10,L,10,L,4,L,6,L,8,L,8,R,10,L,8,L,8,L,10,L,4,L,6,L,8,L,8,L,8,R,10,L,10,L,4,L,6,L,8,L,8,R,10,L,8,L,8,L,10,L,4,L,6,L,8,L,8,';
let out = [];
// convert every substr occurence in `arr` to a symbol in `str`
// toSymbols('12345123',['123','45']) yields 'ABA'
function toSymbols(str, arr) {
arr.forEach((s, i) => {
let re = new RegExp(s, 'g');
str = str.replace(re, String.fromCharCode(65 + i));
});
@potch
potch / spice-rack.js
Created Sep 4, 2019
laser turtles source file for spice racks
View spice-rack.js
UNIT = 'in';
// 100 units in an inch
SUBDIV = 100;
// material thickness
const U = 12.5;
// width of rack
const width = 1000;
// number of shelves
View blockquote.html
<style>
.post blockquote {
border-left: .5em solid #eee;
padding-left: 1.5em;
margin-left: 0;
}
</style>
View asyncawait.html
<p>Firefox 52 also includes a brand new JavaScript feature from ES2017: asynchronous functions and their companion, the <code>await</code> keyword. Async functions build on top of ES2015 Promises, allowing authors to write asynchronous sequences in a similar way to how they would write their synchronous equivalents.</p>
<p>Take the following example, which takes the result of one asynchronous request, modifies it, and passes it as the argument to a second asynchronous function. Here&#39;s how it would look with a traditional callback approach:</p>
<pre><code class="language-javascript">function doThings() {
doThingA(function (err, result) {
if (err) {
handleError(err);
} else {
doThingB(result + 1, function (err, newResult) {