This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @flow | |
// type-level dictionary URI -> type constructor | |
export type URI2HKT<U, L, A> = { | |
Identity: Identity<A> | |
// other type constructors here... | |
// Option: Option<A>, | |
// Either: Either<L, A>, | |
// Foo: Foo<U, L, A> | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Monad, Monad1 } from 'fp-ts/lib/Monad' | |
import { HKT, URIS, Type } from 'fp-ts/lib/HKT' | |
import { liftA2 } from 'fp-ts/lib/Apply' | |
import { flatten } from 'fp-ts/lib/Chain' | |
import { Newtype, iso } from 'newtype-ts' | |
// Adapted from https://tech.iheart.com/why-fp-its-the-composition-f585d17b01d3 | |
// | |
// newtypes |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ObjectOmit } from 'typelevel-ts' | |
const get = <O, K extends keyof O>(k: K, o: O): O[K] => o[k] | |
const set = <O, K extends keyof O>(k: K, v: O[K], o: O): O => Object.assign({}, o, { [k as any]: v }) | |
const remove = <O, K extends keyof O>(k: K, o: O): ObjectOmit<O, K> => { | |
const copy: any = Object.assign({}, o) | |
delete copy[k] | |
return copy |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Type Tac Toe: Advanced Type Safety | |
================================== | |
Adapted from http://chrispenner.ca/posts/type-tac-toe | |
*/ | |
/** Either X, O, or Nothing */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Adapted from http://blog.braulio.me/posts/2017-08-10-enforce-monotonic.html | |
// | |
// booleans | |
// | |
type Bool = 'T' | 'F' | |
type If<B extends Bool, Then, Else> = { | |
T: Then |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { traverse } from 'fp-ts/lib/Array' | |
const checkGenderV = (p: Person) => (p.gender !== 'Male' ? failure(['Men only']) : validation.of(p)) | |
function costToEnter3(p: Person): Validation<Errors, number> { | |
const price = (p: Person) => p.age + 1.5 | |
const checks = [checkAgeV, checkClothesV, checkSobrietyV, checkGenderV] as Array< | |
(p: Person) => Validation<Errors, Person> | |
> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Validation, failure, getApplicative } from 'fp-ts/lib/Validation' | |
import { getArrayMonoid } from 'fp-ts/lib/Monoid' | |
const validation = getApplicative(getArrayMonoid<string>()) | |
const checkAgeV = checkAge(validation, failure) | |
const checkClothesV = checkClothes(validation, failure) | |
const checkSobrietyV = checkSobriety(validation, failure) | |
function costToEnter2(p: Person): Validation<Errors, number> { | |
const price = () => () => (): number => (p.gender === 'Female' ? 0 : 5) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Either, either, left } from 'fp-ts/lib/Either' | |
const checkAgeE = checkAge(either, left) | |
const checkClothesE = checkClothes(either, left) | |
const checkSobrietyE = checkSobriety(either, left) | |
function costToEnter(p: Person): Either<Errors, number> { | |
return either.chain(checkAgeE(p), () => | |
either.chain(checkClothesE(p), () => either.map(checkSobrietyE(p), (): number => (p.gender === 'Female' ? 0 : 5))) | |
) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { HKT, URIS2, Type2 } from 'fp-ts/lib/HKT' | |
import { Applicative, Applicative2, Applicative2C } from 'fp-ts/lib/Applicative' | |
type Errors = Array<string> | |
function checkAge<M extends URIS2>( | |
applicative: Applicative2<M>, | |
fail: (errors: Errors) => Type2<M, Errors, Person> | |
): (p: Person) => Type2<M, Errors, Person> | |
function checkAge<M extends URIS2>( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Sobriety = 'Sober' | 'Tipsy' | 'Drunk' | 'Paralytic' | 'Unconscious' | |
type Gender = 'Male' | 'Female' | |
interface Person { | |
gender: Gender | |
age: number | |
clothes: Array<string> | |
sobriety: Sobriety | |
} |