Skip to content

Instantly share code, notes, and snippets.

View gcanti's full-sized avatar

Giulio Canti gcanti

View GitHub Profile
@gcanti
gcanti / type_safe_event_emitter.md
Created November 5, 2016 08:38
Type safe event emitter with Flow
// 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;
}
@gcanti
gcanti / type_safe_functional_router.md
Created November 8, 2016 11:21
Type safe functional router with Flow
// @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>
@gcanti
gcanti / typed-styles.js
Created December 6, 2016 11:31
Typed style POC
// @flow
//
// library agnostic types and helpers
//
// keep private
class Unit<A> {}
class IsMedia {}
@gcanti
gcanti / default-props-hoc.ts
Last active March 7, 2017 15:01
A hacky implementation of a default prop hoc
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() {
@gcanti
gcanti / fp-ts-technical-overview.md
Last active March 11, 2024 02:40
fp-ts technical overview

Technical overview

A basic Option type

// Option.ts

// definition
export class None {
  readonly tag: 'None' = 'None'
@gcanti
gcanti / functional-typescript-either-vs-validation-01.ts
Created June 17, 2017 10:18
Functional TypeScript: Either vs Validation
type Sobriety = 'Sober' | 'Tipsy' | 'Drunk' | 'Paralytic' | 'Unconscious'
type Gender = 'Male' | 'Female'
interface Person {
gender: Gender
age: number
clothes: Array<string>
sobriety: Sobriety
}
@gcanti
gcanti / functional-typescript-either-vs-validation-02.ts
Last active March 23, 2018 16:54
Functional TypeScript: Either vs Validation
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>(
@gcanti
gcanti / functional-typescript-either-vs-validation-03.ts
Last active March 23, 2018 16:54
Functional TypeScript: Either vs Validation
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)))
)
}
@gcanti
gcanti / functional-typescript-either-vs-validation-04.ts
Last active March 23, 2018 16:55
Functional TypeScript: Either vs Validation
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)
@gcanti
gcanti / functional-typescript-either-vs-validation-05.ts
Last active March 23, 2018 16:55
Functional TypeScript: Either vs Validation
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>
>