Skip to content

Instantly share code, notes, and snippets.

@mvaldesdeleon
Last active August 23, 2019 19:38
Show Gist options
  • Save mvaldesdeleon/caa249ec5e0953e42cc11c2fd2f14ecf to your computer and use it in GitHub Desktop.
Save mvaldesdeleon/caa249ec5e0953e42cc11c2fd2f14ecf to your computer and use it in GitHub Desktop.
import { Functor3 } from 'fp-ts/lib/Functor'
import { IxMonad3 } from 'fp-ts/lib/IxMonad';
declare module 'fp-ts/lib/HKT' {
// IxBurgerBuilder :: * -> * -> * -> *
interface URI2HKT3<U, L, A> {
IxBurgerBuilder: IxBurgerBuilder<U, L, A>
}
}
export const URI = 'IxBurgerBuilder'
export type URI = typeof URI
// newtype IxBurgerBuilder i o spec...
export class IxBurgerBuilder<I, O, A> {
readonly _A: A
readonly _L: O
readonly _U: I
readonly _URI: URI
// ... = IxBurgerBuilder spec
constructor(readonly spec: A) {}
// runIxBurgerBuilder :: forall prev next spec. IxBurgerBuilder prev next spec -> spec
// runIxBurgerBuilder (IxBurgerBuilder spec) = spec
runIxBurgerBuilder(): A {
return this.spec
}
// Instance implementation of Functor3
map<B>(f: (a: A) => B): IxBurgerBuilder<I, O, B> {
return new IxBurgerBuilder(f(this.spec));
}
// Instance implementation of IxMonad3
ichain<Z, B>(f: (a: A) => IxBurgerBuilder<O, Z, B>): IxBurgerBuilder<I, Z, B> {
return new IxBurgerBuilder(f(this.spec).runIxBurgerBuilder());
}
}
// Static implementation of Functor3
export const map = <I, O, A, B>(fa: IxBurgerBuilder<I, O, A>, f: (a: A) => B): IxBurgerBuilder<I, O, B> => {
return fa.map(f)
}
// instance ixMonadIxBurgerBuilder :: IxMonad IxBurgerBuilder where
// ipure = IxBurgerBuilder
export const iof = <I, A>(a: A): IxBurgerBuilder<I, I, A> => {
return new IxBurgerBuilder(a);
}
// ibind (IxBurgerBuilder spec) f = IxBurgerBuilder <<< runIxBurgerBuilder $ f spec
export const ichain = <I, O, Z, A, B>(fa: IxBurgerBuilder<I, O, A>, f: (a: A) => IxBurgerBuilder<O, Z, B>): IxBurgerBuilder<I, Z, B> => {
return fa.ichain(f)
}
// ixburgerbuilder is an instance of IxMonad3 and Functor3
export const ixburgerbuilder: IxMonad3<URI> & Functor3<URI> = {
URI,
map,
iof,
ichain
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment