Skip to content

Instantly share code, notes, and snippets.

View gcanti's full-sized avatar

Giulio Canti gcanti

View GitHub Profile
@gcanti
gcanti / HKT.js
Last active October 27, 2018 06:56
HKTs in Flow
// @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>
}
@gcanti
gcanti / mtl-style.ts
Last active June 1, 2020 03:43
MTL-style using fp-ts
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
@gcanti
gcanti / type-safe-ops.ts
Created September 21, 2017 15:23
Type-safe get, set, remove, pick, insert with TypeScript
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
@gcanti
gcanti / tic-tac-toe.ts
Created August 31, 2017 13:34
Type Tac Toe: Advanced Type Safety (in TypeScript)
/*
Type Tac Toe: Advanced Type Safety
==================================
Adapted from http://chrispenner.ca/posts/type-tac-toe
*/
/** Either X, O, or Nothing */
@gcanti
gcanti / type-level-monotonic.ts
Created August 15, 2017 16:14
Enforce lists to be monotonic at compile time
// 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
@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>
>
@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-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-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-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
}