Skip to content

Instantly share code, notes, and snippets.

View erodactyl's full-sized avatar
🎯
Focusing

Erik Davtyan erodactyl

🎯
Focusing
View GitHub Profile
@erodactyl
erodactyl / lexer.ml
Created June 21, 2023 19:45
A lexer in OCaml
type token =
| INT of int
| IDENTIFIER of string
| PLUS
| MINUS
| TIMES
| DIV
| LET
| ASSIGN
| EQ
@erodactyl
erodactyl / sort.ts
Created March 15, 2023 08:02
Sorting an array using only the ts type system.
type Iterate<T extends number, A extends number[] = []> = A["length"] extends T
? A
: Iterate<T, [...A, A["length"]]>;
type StrictlyLonger<A extends any[], B extends any[]> = A extends [
any,
...infer ATail
]
? B extends [any, ...infer BTail]
? StrictlyLonger<ATail, BTail>
@erodactyl
erodactyl / utility-types.ts
Last active March 3, 2023 11:47
Implementing TypeScript utility types (https://www.typescriptlang.org/docs/handbook/utility-types.html) from scratch.
/**
* `Record<Keys, Type>`
*
* Constructs an object type whose property keys are Keys and whose property values are Type. This utility can be used to map the properties of a type to another type.
*
* https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeys-type
*/
type _Record<K extends string | number | symbol, T> = {
[P in K]: T;
};
@erodactyl
erodactyl / main.md
Created March 1, 2023 16:55 — forked from hediet/main.md
Proof that TypeScript's Type System is Turing Complete
type StringBool = "true"|"false";


interface AnyNumber { prev?: any, isZero: StringBool };
interface PositiveNumber { prev: any, isZero: "false" };

type IsZero<TNumber extends AnyNumber> = TNumber["isZero"];
type Next<TNumber extends AnyNumber> = { prev: TNumber, isZero: "false" };
type Prev<TNumber extends PositiveNumber> = TNumber["prev"];
@erodactyl
erodactyl / sonicpi.ts
Created August 18, 2022 13:51
Sonic Pi in Typescript
import { default as player } from "play-sound";
const play = (name: string) => player().play(`./assets/${name}.wav`);
const sleep = (time: number) => {
return new Promise((res) =>
setTimeout(() => {
res(null);
}, time)
);
};
@erodactyl
erodactyl / maybe.ts
Last active July 11, 2022 06:06
Maybe monad implemented functionally in typescript
class None {}
class Some<T> {
constructor(public value: T) {}
}
type Maybe<T> = None | Some<T>;
export const none = new None();
@erodactyl
erodactyl / mini-rxjs.ts
Created October 10, 2021 20:22
Implementation of basic rxjs observable and a few operators.
interface Observer<T> {
next: (el: T) => void;
}
type Dispose = () => void;
type Operator<T, U> = (source$: Observable<T>) => Observable<U>
class Observable<T> {
constructor(private cb: (observer: Observer<T>) => Dispose) {}
@erodactyl
erodactyl / createStore.ts
Last active October 3, 2021 16:30
State management solution for easily and safely handling complex async state like Promises or eventListeners.
import { useCallback, useEffect, useReducer, useRef } from "react";
type Comparator = (value1: any, value2: any) => boolean;
/** Core */
type Listener = () => void;
type Destroy = () => void;
import { useEffect, useReducer, useMemo } from "react";
import { BehaviorSubject } from "rxjs";
export const createStateHook = <S>(init: S) => {
const state$ = new BehaviorSubject(init);
const setState = (change: S | ((state: S) => S)) => {
state$.next(change instanceof Function ? change(state$.value) : change);
};
@erodactyl
erodactyl / deeply_nested_flatten_unflatten.ts
Last active August 6, 2021 08:34
Helper functions for deeply nested data structures. Can be used with GraphQL queries that have to return arbitrarily deep trees. "flatten" is called in the backend resolver to traverse the tree into an array, "unflatten" is called in the frontend resolver to bring it to original shape. The shape has a key "id" and an arbitrary key for nested chi…
type IFlattenable<K extends string> = {
[k in K]?: IFlattenable<K>[];
} & { id: number };
type IFlattened<T extends IFlattenable<K>, K extends string> = Omit<
T,
K | 'id'
> & {
parentId: number;
id: number;