Skip to content

Instantly share code, notes, and snippets.

@RoyalIcing
RoyalIcing / f1-qualifying-state-machine.js
Created March 23, 2022 03:55
Modelling Formula One qualifying as a state machine
const S1Time = Symbol('S1Time');
function DriverTime() {
function* NoTime() {
yield on('sector1Set', SectorTime, id('Sector1'));
yield on('sector2Set', SectorTime, id('Sector2'));
yield on('sector3Set', SectorTime, id('Sector3'));
}
function* SectorTime() {
@RoyalIcing
RoyalIcing / youtube-url-parser.js
Last active March 4, 2022 05:41
YouTube URL YieldParser
function* VideoID() {
const [videoID] = yield /^[a-zA-Z0-9_]+$/;
return videoID;
}
function* Long() {
yield "https://www.youtube.com/watch?v=";
const videoID = yield VideoID;
return videoID;
}
function* Embed() {
export function* TrafficLights() {
function* Green() {
yield on("timer", Yellow);
}
function* Yellow() {
yield on("timer", Red);
}
function* Red() {
yield on("timer", Green);
}
@RoyalIcing
RoyalIcing / loader.js
Created February 15, 2022 06:13
Loader state machine component
export function Loader() {
function* Loading() {
yield on("error", Failed);
yield on("timeout", TimedOut);
yield on("abort", Aborted);
yield on("success", Loaded);
}
function* Failed() {}
function* TimedOut() {}
function* Aborted() {}
@RoyalIcing
RoyalIcing / remorse.php
Created January 20, 2022 01:43
Remorse: Focused on web fundamentals and archaic UX
<?php
function loader($request) {
return getProjects();
}
function action($request) {
$form = $request.formData();
return createProject([ "title" => $form.get("title") ]);
}
@RoyalIcing
RoyalIcing / 1.js
Last active March 9, 2022 04:23
Mini modules
/**
* Pi to 8 decimal places
*/
export const pi = 3.14159265;
export const dateFormat = "YYYY/MM/DD";
export const isEnabled = true;
export const flavors = ["vanilla", "chocolate", "caramel", "raspberry"];
@RoyalIcing
RoyalIcing / timing.ts
Last active December 2, 2021 02:59
Debouncing with logical clocks in React
import { DispatchWithoutAction, useEffect, useMemo, useReducer } from 'react';
import type { DependencyList, EffectCallback } from 'react';
/**
* A logical clock.
*
* @returns a tuple with the current clock value, and a stable function that advances the clock.
*/
export function useTicker(): [number, DispatchWithoutAction] {
return useReducer(n => n + 1, 0);
@RoyalIcing
RoyalIcing / example.js
Created September 11, 2021 05:34
`in` as pipeline keyword
// See: https://twitter.com/buildsghost/status/1436394640861646848
envars
|> Object.keys(in)
|> in.map(envar => `${envar}=${envars[envar]}`)
|> in.join(' ')
|> `$ ${in}`
|> chalk.dim(in, 'node', args.join(' '))
|> console.log(in)
@RoyalIcing
RoyalIcing / main.md
Last active September 3, 2021 00:21
RxJS Loading Patterns

RxJS Loading Patterns

Observable creators

of(A...) => Observable<A>

Create an observable emitting each item passed in.

from(Promise<A> | Iterable<A>) => Observable<A>

@RoyalIcing
RoyalIcing / init.lua
Last active November 20, 2023 03:48
My Hammerspoon config
-- Runs using the open source Mac app https://www.hammerspoon.org
-- Install Hammerspoon, and then copy this Gist into a file at ~/.hammerspoon/init.lua
require("hs.ipc")
hs.ipc.cliInstall()
local math = require("hs.math")
currentSpeech = nil