Skip to content

Instantly share code, notes, and snippets.

View betafcc's full-sized avatar
🎯
Focusing

Brendon betafcc

🎯
Focusing
View GitHub Profile
@betafcc
betafcc / Ticks.tsx
Last active December 23, 2023 20:35
Clock marks with dasharray
import { ComponentProps, FC } from 'react'
export const Ticks: FC<
Omit<
ComponentProps<'circle'>,
'strokeWidth' | 'pathLength' | 'strokeDasharray' | 'strokeDashoffset' | 'r'
> & {
r: number
count: number
length: number
import { ComponentProps, ComponentRef, forwardRef } from "react"
import { Slot } from "@radix-ui/react-slot"
import { cn } from "@/lib/util"
const variants = {
light: "rgba(255,255,255, 0.3)",
dark: "rgba(0,0,0, 0.2)",
} as const
@betafcc
betafcc / env.py
Created October 16, 2022 22:26
dotenv utility wrapper
from __future__ import annotations
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Generic, Optional, TypeVar, cast, overload
from dotenv import dotenv_values # type: ignore
from typing_extensions import LiteralString
KI = TypeVar("KI", bound=LiteralString)
@betafcc
betafcc / fmap.py
Created October 16, 2022 20:10
polymorphic python fmap
from __future__ import annotations
from asyncio import Task, create_task
from dataclasses import dataclass
from typing import (
Any,
AsyncIterable,
AsyncIterator,
Awaitable,
Callable,
@betafcc
betafcc / record.py
Last active October 19, 2022 12:05
Extensible typed records in python (pyright)
from __future__ import annotations
from dataclasses import dataclass
from typing import Any, ClassVar, Generic, Literal, NoReturn, Type, TypeAlias, TypeVar
from typing_extensions import LiteralString
K = TypeVar("K", bound=LiteralString)
V = TypeVar("V", covariant=True)
AnyPair: TypeAlias = tuple[LiteralString, Any]
@betafcc
betafcc / mypy_operators.py
Created October 14, 2022 18:52
Mypy type-level operators
from __future__ import annotations
from typing import (
Any,
Awaitable,
Callable,
Iterable,
Literal,
Mapping,
ParamSpec,
@betafcc
betafcc / FizzBuzzIterator.d.ts
Last active September 5, 2022 10:57
Type-Level FizzBuzz in Typescript
// Iterator approach, seems to be good up until ~850
type Result = Take<100, FizzBuzzIterator[1]>
// ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz", ...]
export type FizzBuzzIterator<
S extends Record<any, any> = { 3: []; 5: []; i: [] },
R extends string = `${S[3]["length"] extends 3 ? "Fizz" : ""}${S[5]["length"] extends 5 ? "Buzz" : ""}`
> = [
R extends "" ? `${S["i"]["length"]}` : R,
FizzBuzzIterator<{
@betafcc
betafcc / elm.http.tsx
Last active August 1, 2022 15:53
Elm style useReducer
export type Msg = { GotText: Http.Result<Error, string> }
export type Model = { Failure: [] } | { Loading: [] } | { Success: string }
// copy of https://guide.elm-lang.org/effects/http.html
export const main = (
<ElmElement<Model, Msg>
init={() => [
{ Loading: [] },
Http.get({
url: 'https://elm-lang.org/assets/public-opinion.txt',
@betafcc
betafcc / reveal.ts
Last active July 26, 2022 13:20
Expand type aliases from a typescript file
import { readFileSync } from 'fs'
import { resolve } from 'path'
import ts from 'typescript'
import * as prettier from 'prettier'
/**
* reveal all top level `type` declarations from a file
*/
export const reveal = (sourcePath: string) => {
const program = ts.createProgram([sourcePath], getConfig())
@betafcc
betafcc / Permutation.d.ts
Last active June 24, 2024 10:02
Typescript Union Permutation, count of keys in an object, count of cases in an Union
/**
* @example
* type P = Permutation<1 | 2 | 3>
* // [1, 2, 3] | [1, 3, 2] | [2, 1, 3] | [2, 3, 1] | [3, 1, 2] | [3, 2, 1]
*/
export type Permutation<U, T = U> = [U] extends [never]
? []
: T extends unknown
? [T, ...Permutation<Exclude<U, T>>]
: never