Skip to content

Instantly share code, notes, and snippets.

View safareli's full-sized avatar

Irakli Safareli safareli

View GitHub Profile
type _Boolean = <A>(onTrue: () => A, onFalse: () => A) => A
const _true: _Boolean = (onTrue) => onTrue()
const _false: _Boolean = (_, onFalse) => onFalse()
const _if = <A>(condition: _Boolean, onTrue: () => A, onFalse: () => A): A => {
return condition(onTrue, onFalse)
}
const _and = (a: _Boolean,b: _Boolean): _Boolean => {
return (onTrue: () => any, onFalse: () => any) => {
@safareli
safareli / parMap.ts
Last active February 26, 2021 09:40
Traverse record of promises in parallel and give result of the same shape. (similar to sequenceRecord from purescript)
const parMap = async <T, U>(array:T[], callback: (value: T) => U | PromiseLike<U>): Promise<U[]> => {
return Promise.all(array.map(callback))
}
const parEach = async <T, U>(array:T[], callback: (value: T) => void | PromiseLike<void>): Promise<void> => {
await Promise.all(array.map(callback))
}
async function main() {
const names = ["foo.txt","baz.txt"]
@safareli
safareli / PVar.ts
Last active February 19, 2021 13:22
type PVar<T> = Promise<T> & {
resolve: (res: T | PromiseLike<T>) => void;
reject: (reason?: unknown) => void;
};
function mkPVar<T>() {
const result: PVar<T> = new Promise<T>((resolve, reject) => {
result.resolve = resolve;
result.reject = reject;
}) as PVar<T>;
export interface Canceler {
(): void;
}
/** No operation canceler which "does nothing".
*
* ```js
* concatCancelers(a, emptyCanceler) ==== concatCancelers(emptyCanceler, a) ==== a
* ```
*/
/**
* Stricter and Safer version of Type Guards.
*
* It takes function that actually returns Output type and constructs
* type guard which is actually safe and can't ever go out of sync.
* for example this type guard compiles:
* ```ts
* function isNumber(x: any): x is number {
* return typeof x === "string";
@safareli
safareli / _mutex2.ts
Last active January 18, 2021 13:23
Added comments to mutex implementation from https://spin.atomicobject.com/2018/09/10/javascript-concurrency/
type CleanUp = () => void
export class Mutex<T> {
private mutex:Promise<void> = Promise.resolve();
lock(): Promise<CleanUp> {
let resolver = (unlock: CleanUp): void => {};
const result = new Promise<CleanUp>(resolve => { resolver = resolve });
// whatever is running currently, let's wait untill that's done
@safareli
safareli / decoder.ts
Last active June 9, 2021 19:03
Mini decoder library in 60 lines.
type Decoder<I,O> = (_:I) => O
type TypeOf<D> = D extends Decoder<any, infer R> ? R: never
type InputOf<D> = D extends Decoder<infer R, any> ? R: never
const string: Decoder<unknown, string> = (x) => {
if (typeof x === "string") return x;
throw new Error("Expected string");
}
const number: Decoder<unknown, number> = (x) => {
const consoleLogger = (...args: unknown[]) => {
console.log(...args);
};
const consoleTracer = (...args: unknown[]) => {
console.trace(...args);
};
type MonitorAction = "get" | "set";
type MonitorDesc<T> = [prop: keyof T, ...actions: MonitorAction[]];
@safareli
safareli / EventEmitter.ts
Last active December 29, 2020 06:49
typed TypeScript event emitter
type Listener<T> = [T] extends [void] ? () => void : (payload: T) => void;
type BaseSpec = Record<string, unknown>;
type ListenerMap<T extends BaseSpec> = { [K in keyof T]?: Listener<T[K]>[] };
type EventPayload<T> = [T] extends [void] ? [] : [payload: T];
/** Typed event emitter implementation
* example:
@safareli
safareli / nullable_vs_union.ts
Created November 25, 2020 11:15
Typescript: Nullable vs union
type RPCResponsev1 =
{ error?: string
, result?: string
}
// TS will not let this compile as it know there might be case
// where both error and result is null and function will just return implicitly with undefined.
function example(res:RPCResponsev1): number {
if(res.error == null) {
return 1