Skip to content

Instantly share code, notes, and snippets.

View joshburgess's full-sized avatar
💭
🤔

Josh Burgess joshburgess

💭
🤔
View GitHub Profile
@joshburgess
joshburgess / HKT.js
Created October 27, 2018 06:56 — forked from gcanti/HKT.js
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>
}
@joshburgess
joshburgess / HKT.js
Created October 27, 2018 06:55 — forked from hallettj/HKT.js
Concept for emulating higher-kinded types in Flow via type-level functions
/*
* Concept for emulating higher-kinded types using Flow. Instead of passing
* a type that has not been applied to parameters, this pattern passes
* a type-level function that will map a parameter type to the desired
* higher-kinded type applied to the given parameter.
*
* @flow
*/
// a higher-kinded type is represented indirectly via a type-level function from
@joshburgess
joshburgess / exchange-formats.md
Created October 11, 2018 16:00 — forked from gelisam/exchange-formats.md
A list of every data exchange formats I could find

At work, I just spent the last few weeks exploring and evaluating every format I could find, and my number one criteria was whether they supported sum types. I was especially interested in schema languages in which I could describe my types and then some standard specifies how to encode them using an on-the-wire format, usually JSON.

  1. Swagger represents sum types like Scala does, using subtyping. So you have a parent type EitherIntString with two subtypes Left and Right represented as {"discriminator": "Left", value: 42} and {"discriminator": "Right", value": "foo"}. Unfortunately, unlike in Scala in which the parent type is abstract and cannot be instantiated, in Swagger it looks like the parent type is concrete, so when you specify that your input is an EitherIntString, you might receive {"discriminator": "EitherIntString"} instead of one of its two subtypes.
  2. JSON-schema supports unions, which isn't quite the same thing as sum types because
@joshburgess
joshburgess / mtl-style.ts
Created October 10, 2018 00:35 — forked from gcanti/mtl-style.ts
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
type Init<T extends any[], TTail extends any[] = TailArray<T>> = CastArray<{
[K in keyof TTail]: T[keyof T & K];
}>
type PotentialArgs<T extends any[], TResult extends any[] = T> = {
"continue": PotentialArgs<Init<T>, TResult | Init<T>>;
"end": TResult;
}[T extends [] ? "end" : "continue"]
type Args<T extends Function> =
@joshburgess
joshburgess / UIPattern.re
Created September 11, 2018 23:56 — forked from busypeoples/UIPattern.re
Slaying a UI Anti Pattern in ReasonML
/*
Slaying a UI Anti Pattern in ReasonML
Based on Kris Jenkins original writing.
http://blog.jenkster.com/2016/06/how-elm-slays-a-ui-antipattern.html
*/
type remoteData 'e 'a =
| NotAsked
| Loading
| Failure 'e
| Success 'a;
@joshburgess
joshburgess / algebraic.ts
Created September 11, 2018 18:14 — forked from SimonAlling/algebraic.ts
Algebraic Data Types in TypeScript
// Types:
type Just<T> = { Just: T }
type Nothing = {}
type Maybe<T> = Just<T> | Nothing
type Left<L> = { Left: L }
type Right<R> = { Right: R }
type Either<L, R> = Left<L> | Right<R>
// For convenience:
@joshburgess
joshburgess / README.md
Created September 10, 2018 21:56 — forked from OliverJAsh/README.md
FP list combinators demo with fp-ts
tsc && node main.js

Technical overview

A basic Option type

// Option.ts

// definition
export class None {
  readonly tag: 'None' = 'None'
@joshburgess
joshburgess / README.md
Created August 10, 2018 01:14 — forked from bfncs/README.md
Slaying a UI Antipattern with TypeScript & TsMonad