// A is a phantom type that ties an event instance...
class Event<A> {}
// ...to its handler
type Handler<A> = (a: A, ...rest: Array<void>) => void;
declare class EventEmitter {
on<A, F: Handler<A>>(event: Event<A>, handler: F): void;
emit<A>(event: Event<A>, a: A): void;
}
// @flow
// Params -> Querystring -> A
type Handler<A, P = void, Q = void> = (p: P, q: Q) => A;
type Id = { id: string };
// config type
type Router<a> = {</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
// @flow | |
// | |
// library agnostic types and helpers | |
// | |
// keep private | |
class Unit<A> {} | |
class IsMedia {} |
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 * as React from 'react' | |
type Witness<A, B, C> = (x: A & B) => C | |
// P = original props | |
// D = defaulted props | |
// U = untouched props | |
function removeProps<P, D extends keyof P, U extends keyof P>(defaults: Pick<P, D>, witness: Witness<Pick<P, D>, Pick<P, U>, P>): (Component: React.ComponentClass<P>) => React.ComponentClass<Pick<P, U>> { | |
return Component => class extends React.Component<Pick<P, U>, void> { | |
render() { |
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 | |
} |
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
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 { 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 { 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> | |
> |