Skip to content

Instantly share code, notes, and snippets.

View shicks's full-sized avatar

Stephen Hicks shicks

  • Google
  • Sunnyvale, CA
View GitHub Profile
shicks /
Last active August 14, 2023 03:41
My TypeScript generics wishlist

Background: TypeScript Generics Wishlist

There are a handful of issues that are both separate features, but are also loosely related in that they speak to certain problematic aspects of generics today. In particular,

  • [#7061] (specifically later in the thread) asks for a way to assign a private alias to a complex typed derived from the actual type parameters, usable from within the class/function signature. There are suggestions to use namespaces and other hacks, along with explanations of why <T, U extends Foo<T> = Foo<T>> is insufficient.
  • [#26242] and [#16597] both pertain to partial inference of type parameters. The rough summary is that you can write ``, but there's no way to explicitly specify T and still get inference on `U`. If you
shicks / num.ts
Last active October 16, 2020 10:14
Parsing integers in TypeScript's type system
type NumCat<X, Y> = X extends any[] ? Y extends any[] ? [...X, ...X, ...X, ...X, ...X, ...X, ...X, ...X, ...X, ...X, ...Y] : never : never;
type OneDigit = {
'0': [], '1': [any], '2': [any, any], '3': [any, any, any],
'4': [any, any, any, any], '5': [any, any, any, any, any],
'6': [any, any, any, any, any, any], '7': [any, any, any, any, any, any, any],
'8': [any, any, any, any, any, any, any, any],
'9': [any, any, any, any, any, any, any, any, any],
type Digit<S> = S extends keyof OneDigit ? OneDigit[S] : never;
type Len<X> = X extends any[] ? X['length'] : never;
shicks /
Created February 14, 2020 23:01
Tetraflexagon creator
# Perl script for creating hexatetraflexagons.
# Usage: flex a.jpg b.jpb c.jpg. d.jpg e.jpg f.jpg
# Will pair a/b as the central pair, c/d on one side, and e/f on the other.
# Images must be square.
use strict;
use warnings;
shicks / fround.js
Last active January 31, 2022 19:01
Universal polyfill for Math.fround, does not depend on Float32Array.
// NOTE: This implementation is incorrect (it doesn't break ties to even). See comments below for discussion.
Math.fround = Math.fround || function(arg) {
arg = Number(arg);
// Return early for ±0 and NaN.
if (!arg) return arg;
var sign = arg < 0 ? -1 : 1;
if (sign < 0) arg = -arg;
// Compute the exponent (8 bits, signed).
var exp = Math.floor(Math.log(arg) / Math.LN2);
shicks / Strokes.hs
Last active September 12, 2016 03:48
Stroke-counting function for Sino-Japanese numbers
-- Stroke-counting function for Sino-Japanese numbers
module Strokes where
import Data.Ord (comparing)
import Data.List (elemIndex, maximumBy)
ichi = "一"
ni = "二"
san = "三"
shicks / Dots.hs
Last active October 25, 2023 05:37
Console Fractals
-- Defines a data structure for plotting data in a text console using braille
module Dots where
import Control.Monad.State (State, modify, execState)
import Data.Ix (range)
import qualified Data.Set as S
data Dots = Dots (Int, Int) (S.Set (Int, Int))