Skip to content

Instantly share code, notes, and snippets.

View morozovamv's full-sized avatar
🤘

Aleksandr Morozov morozovamv

🤘
View GitHub Profile
@morozovamv
morozovamv / monad.ts
Created December 10, 2018 14:32
Road to monad: final
import { some, Option, none, getSetoid, option } from 'fp-ts/lib/Option';
import { setoidNumber } from 'fp-ts/lib/Setoid';
import { liftA2 } from 'fp-ts/lib/Apply';
const isEquals = (x: Option<number>, y: Option<number>) => getSetoid(setoidNumber).equals(x, y);
// Monad = Applicative & Chain + Laws
// Laws:
// 1. Left identity: M.chain(M.of(a), f) = f(a)
@morozovamv
morozovamv / applicative-chain-setoid.ts
Created November 22, 2018 13:44
Road to monad: applicative and chain (+setoid)
import { Option, some, none, fromNullable, getSetoid as getSetoidOpt } from 'fp-ts/lib/Option';
import { success, getSetoid as getSetoidRD } from '@devexperts/remote-data-ts';
import { of, Observable, combineLatest } from 'rxjs';
import { setoidNumber, setoidString, getRecordSetoid } from 'fp-ts/lib/Setoid';
import { Function1 } from 'fp-ts/lib/function';
type TUser = {
name: string;
};
@morozovamv
morozovamv / functor.ts
Created October 22, 2018 10:04
Road to monad: functor
import { Function1 } from 'fp-ts/lib/function';
import { Option, none, some } from 'fp-ts/lib/Option';
// T -> T
const identity = <T>(v: T): T => v;
// (B -> C, A -> B) -> A -> C
const compose = <A, B, C>(f: (Function1<B, C>), g: Function1<A, B>) => (x: A): C => f(g(x));
const plusFive = (n: number): number => n + 5;
@morozovamv
morozovamv / apply.ts
Last active April 24, 2019 06:30
Road to monad: apply
import { Option, some, none, option } from "fp-ts/lib/Option";
import { liftA4 } from 'fp-ts/lib/Apply';
import { Function1 } from 'fp-ts/lib/function';
// Task: find the total age of two users
type TPerson = {
name: string;
// age: number;
age: Option<number>;