Skip to content

Instantly share code, notes, and snippets.

View YBogomolov's full-sized avatar

Yuriy Bogomolov YBogomolov

View GitHub Profile
@YBogomolov
YBogomolov / 00-README.md
Created March 23, 2021 19:53
Статья про Task, TaskEither, ReaderTaskEither

Примеры кода для статьи на Хабре о функциональной замене промисам

Статья: https://habr.com/ru/post/548622

import { either } from 'fp-ts';
import { pipe } from 'fp-ts/function';
import Either = either.Either;
declare global {
interface ErrorConstructor<T extends string = string> extends Error<T> {}
interface Error<T extends string = string> {
readonly tag: T;
new<T extends string>(message?: string): Error<T>;
@YBogomolov
YBogomolov / tf.ts
Last active September 9, 2023 08:29
Tagless Final example with fp-ts
import { HKT, Type, URIS } from 'fp-ts/lib/HKT';
import { identity, Identity, URI } from 'fp-ts/lib/Identity';
import { Monad, Monad1 } from 'fp-ts/lib/Monad';
// Task: get all users named 'John' and make them admins
interface User {
id: number;
name: string;
age: number;
@YBogomolov
YBogomolov / inflate.ts
Last active July 11, 2023 09:51
Inflating objects
type Split<S extends string, D extends string> = S extends `${infer T}${D}${infer U}` ? [T, ...Split<U, D>] : [S];
type Join<T extends string[], D extends string> = T extends []
? ''
: T extends [infer F extends string, ...infer R extends string[]]
? `${F}${D}${Join<R, D>}`
: never;
type Inflate<T> = Compact<UnionToIntersection<{ [K in keyof T]: InflateInner<T[K], K & string>; }[keyof T]>>;
@YBogomolov
YBogomolov / uuid.ts
Last active June 21, 2023 14:15
Type-level UUID
type VersionChar =
| '1' | '2' | '3' | '4' | '5';
type Char =
| '0' | '1' | '2' | '3'
| '4' | '5' | '6' | '7'
| '8' | '9' | 'a' | 'b'
| 'c' | 'd' | 'e' | 'f';
type Prev<X extends number> =
@YBogomolov
YBogomolov / 0_README.md
Created June 7, 2020 13:34
Idris 2 DevContainer

How to use Idris 2 in devcontainer

Step 1: build executable and libraries

Use the Dockerfile below to build the Idris 2 executable and standard library:

docker build --tag=idris2 .
@YBogomolov
YBogomolov / session.ts
Last active November 26, 2022 16:45
Session types for TypeScript
// Inspired by https://github.com/Munksgaard/session-types
interface Chan<Env, Protocol> {
env_and_protocol: [Env, Protocol];
}
class Eps implements HasDual {
readonly tag: 'Eps' = 'Eps';
readonly dual!: Eps;
}
@YBogomolov
YBogomolov / either_tuple.ts
Created April 13, 2020 15:02
Encoding Either<E, A> as a tuple
export type Either<E, A> = [E, null] | [null, A];
type Fn<A, B> = (a: A) => B;
export const left = <E, A>(e: E): Either<E, A> => [e, null];
export const right = <E, A>(a: A): Either<E, A> => [null, a];
export const isLeft = <E, A>(e: Either<E, A>): e is [E, null] => e[1] === null;
export const isRight = <E, A>(e: Either<E, A>): e is [null, A] => e[0] === null;
@YBogomolov
YBogomolov / tf-streams.ts
Last active August 23, 2020 14:14
Tagless Final example with functional streams
import { Do } from 'fp-ts-contrib/lib/Do';
import { array } from 'fp-ts/lib/Array';
import { constUndefined, identity, Lazy } from 'fp-ts/lib/function';
import { Kind, URIS } from 'fp-ts/lib/HKT';
import * as Id from 'fp-ts/lib/Identity';
import { Monad1 } from 'fp-ts/lib/Monad';
import { none, Option } from 'fp-ts/lib/Option';
import { Traversable1 } from 'fp-ts/lib/Traversable';
declare module 'fp-ts/lib/HKT' {
@YBogomolov
YBogomolov / fsm_cb.ts
Created March 24, 2019 09:14
FSM using Circuit Breaker
import AbortController from 'abort-controller';
import { circuitBreaker, defaultBreakerOptions } from 'circuit-breaker-monad/lib';
import { BreakerClosed, BreakerOpen, BreakerState } from 'circuit-breaker-monad/lib/types';
import { Either, left } from 'fp-ts/lib/Either';
import { Lazy } from 'fp-ts/lib/function';
import { IORef } from 'fp-ts/lib/IORef';
import fetch from 'node-fetch';
const fetcher = circuitBreaker<User[]>().run(defaultBreakerOptions);