Skip to content

Instantly share code, notes, and snippets.

Avatar

Philip Polkovnikov polkovnikov-ph

  • Tbilisi, Georgia
View GitHub Profile
@polkovnikov-ph
polkovnikov-ph / brzozowski.ts
Created June 20, 2022 00:21
Brzozowski's DFA minification in JavaScript
View brzozowski.ts
type Dfa<Tag> = {
trans: Map<string, [Tag, string][]>;
initial: string[];
final: string[];
}
const invertEdges = <Tag,>(auto: Dfa<Tag>): Dfa<Tag> => {
const trans = new Map<string, [Tag, string][]>();
for (const [from] of auto.trans) {
trans.set(from, []);
View use-draggable.ts
import "./styles.css";
import React, { useRef, useState, useEffect, useCallback } from "react";
import { unstable_batchedUpdates as batch } from "react-dom";
// call `f` no more frequently than once a frame
export const throttle = (f) => {
let token = null,
lastArgs = null;
const invoke = () => {
f(...lastArgs);
View describe-object.ts
type IfEquals<X, Y, A, B> = (<T>() => T extends X ? 1 : 2) extends (<T>() => T extends Y ? 1 : 2) ? A : B;
type ReadonlyKeys<U, T = { [K in keyof U]-?: U[K] }> = {
[P in keyof T]: IfEquals<{ [Q in P]: T[P] }, { -readonly [Q in P]: T[P] }, never, P>
}[keyof T];
type Full<T, R = ReadonlyKeys<T>> = {
-readonly [K in keyof T]-?: {
type: T[K],
optional: T extends Record<K, any> ? false : true,
readonly: K extends R ? true : false,
}
@polkovnikov-ph
polkovnikov-ph / concrete.factorio
Last active May 12, 2021 17:36
factorio beaconed refined concrete 45/s
View concrete.factorio
0eNq1XdtyGzkO/ZUpPduzvF/yuL+xlUrJdmema2VJJclz2VT+fVt2xq3YTfPgwH7aynoEkAAOmgQPwW+rm83DsD+M29Pq07fV3XC8PYz707jbrj6t/j2sb3fb4e6Xw/B1PP/v9K/bw3AafgnxX8fV1Wqc/o/j6tN/vq2O42/b9eYs4vT3fph+O56G++m/2K7vz//6IeD6HwGr79OPt3fDX6tP9vtV9+c3jwO5+JH7/vlqNWxP42kcngbw+I+/v2wf7m+GwyT15W+vVvvdcXya2LfVJKL8Gq9Wf68+ZfdrPAueFB7Pfzruh2mg97u7h81w7c+qzgN8Id51xVurke/78oNGfujLzxr5sSvfGY381JfvNfJzX37SyC99+VUjv3ble6eRb01fQVQp6APYqxBs+xAOKgjbPoaDCsO2D+KgArHtoziqUGz7MI4qGNs+jqMKx7YP5KgCsu0jOamQ7PpITiokuz6Sk+5b3EdyViHZzUge/tofhuPx+nRYb4/73eF0fTNsTgsKn03mHxXejYfh9unvaUlDkGsITQ1hSUN8peG434yn0/TH17Kf00Z4Mfar1ddxc/7NtEI77LbX+816WsxdrXYPp/3D6cu0jtwdJrXTnzfD19NqaSBJPtUkM2aWa6gyDUWuocg0zLA/L7FP6+1pWjvf34zb9Wm34LHn+I6vpF+dJZwOu82Xm+H39R+Tg86/eXIjtHY/noZh88PT38/SHs57hSpawF8EyyzinDyf1/NX5DjcPA77/fPiWtqQtgzvYcuvm4fxbp7En+szeubhpxwvDLk8fiuONt8MB7ekwIkVOJmCOYHux/1wfdpd/3aY5n/3huCAwMTL82a8HPmSyMiLDJAx5AkwyKyd8b1vZPamr7Pf5Mrh8OTSxhRS20ZXz3DfTt+QpW+Gn7Ph+ngc7m824/a36/v17e/j9jzO1xB+DqP0qG5SNu5/JICn+sMbkw6LG2YDh7C1ongIFpfsRegI7ifJb5kpLIMheMLVNmp8HQKjMgAqn9Yoizq
@polkovnikov-ph
polkovnikov-ph / example.ts
Created April 5, 2021 10:58
Custom type error messages in TypeScript
View example.ts
declare const StaticErrorTag: unique symbol;
type StaticError<E extends string> = { [StaticErrorTag]: E }
type AAndBAreSame<T> = T extends {a: any}
? T extends {b: T["a"]}
? T
: StaticError<"Types of `a` and `b` differ">
: StaticError<"No `a` field">
const f = <T,>(t: T & AAndBAreSame<T>) => {};
@polkovnikov-ph
polkovnikov-ph / cpp-guide-en.md
Last active November 7, 2022 11:32
C++ minimum for competitive programming
View cpp-guide-en.md

By sharpc and udpn. Code samples by Evil_Stivie.

This is theoretical STL minimum for competitive programmers, a subset of features of C++ standard library useful for solving algorithmic problems. Topics are grouped mostly by STL header file name and context of use. Almost all names you might encounter in this guide are in std namespace. For it, and only for it, you should add below all #includes:

using namespace std;

<iostream>, <iomanip>

cout, cin, while (cin >> ...)

@polkovnikov-ph
polkovnikov-ph / rtti.ts
Created December 1, 2019 23:10
io-ts done right: arbitrarily handled runtime type information (overloading) for typescript
View rtti.ts
// hkt.ts
interface TagToHkt<T> { }
export type Tag = keyof TagToHkt<any>
export type Apply<F extends Tag, T> = TagToHkt<T>[F]
// util.ts
export type Intersect<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void)
? I
: never;
export type IndexForSure<O, K extends string> = O extends { [K1 in K]: any } ? O[K] : never
@polkovnikov-ph
polkovnikov-ph / hoc.ts
Created May 24, 2019 16:33
How to type HOCs in TS
View hoc.ts
import {ComponentType} from 'react';
import {Omit, Shared, Matching, GetProps} from 'react-redux';
export type Hoc<InjectedProps> = <
C extends ComponentType<Matching<InjectedProps, GetProps<C>>>
>(
WrappedComponent: C,
) => ComponentType<Omit<GetProps<C>, keyof Shared<InjectedProps, GetProps<C>>>>;
@polkovnikov-ph
polkovnikov-ph / index.ts
Created May 3, 2019 22:19
Isomorphic API typing
View index.ts
import { Type, URIS as Uris } from 'fp-ts/lib/HKT';
type JsonMap = { [K: string]: Json }
interface JsonArray extends Array<Json> {}
type Json = JsonMap | JsonArray | number | string | boolean | null
interface Prim<Uri extends Uris> {
num: Type<Uri, number>;
obj: <O>(ss: { [K in keyof O]: Type<Uri, O[K]> }) => Type<Uri, O>;
}
@polkovnikov-ph
polkovnikov-ph / es6-estree.ts
Last active December 23, 2018 04:38
Attempt at proper Mozilla AST typings for ES6
View es6-estree.ts
// if you've found this in Google, take a look into https://github.com/babel/babel/tree/master/packages/babel-types first
export type Node = AssignmentProperty
| CatchClause
| ClassBody
| ExportAllDeclaration
| ExportDefaultDeclaration
| ExportNamedDeclaration
| Expression
| Identifier