Skip to content

Instantly share code, notes, and snippets.

View timvandam's full-sized avatar

Tim van Dam timvandam

View GitHub Profile
@timvandam
timvandam / permutations.ts
Created January 13, 2024 15:37
TypeScript union permutations
type Permutations<T, O = T> = [T] extends [never] ? [] : T extends any ? [T, ...Permutations<Exclude<O, T>>] : never;
@timvandam
timvandam / getNestedObjectProperty.ts
Created January 13, 2024 15:36
TypeScript getNestedObjectProperty
type JoinPath<A extends readonly string[]> = A extends readonly [infer K]
? K extends string
? K
: never
: A extends readonly [infer K, ...infer Rest]
? K extends string
? Rest extends readonly string[]
? `${K}.${JoinPath<Rest>}`
: never
: never
@timvandam
timvandam / train.py
Created August 23, 2022 11:51
DDP training
import math
import operator
from multiprocessing import Pool
from typing import List
import torch
import random
import os
import numpy as np
from fuzzywuzzy import fuzz
from torch.utils.data import DataLoader, Dataset, RandomSampler, DistributedSampler, SequentialSampler
@timvandam
timvandam / type-stuff.ts
Created December 31, 2021 12:49
Type-stuff
export type PrimaryKey<Type> = Metadata<Type, 'primaryKey', { primaryKey: true }>;
export type BackReference<ReferencedBy, RelationName extends string> = Metadata<
ReferencedBy,
'backReference',
{ backReference: true; relationName: RelationName }
>;
export type GetBackReferenceRelationNames<Entity> = string &
{
@timvandam
timvandam / PromiseIterator.ts
Created February 1, 2021 11:38
Promise Iterator
/**
* Yields promises as they settle.
* @param promises List of promise to yield through
*/
export default async function* PromiseIterator<P>(promises: Iterable<Promise<P>>): AsyncGenerator<[Promise<P>], void, void> {
const pending = new Set<Promise<P>>(promises)
const settled = new Set<Promise<P>>()
// Move pending promises to `settled` when they settle
pending.forEach((promise) =>