Skip to content

Instantly share code, notes, and snippets.

@pfgray
Created April 20, 2019 12:19
Show Gist options
  • Save pfgray/8f1fe1a482a8ee0e0da0d9ed8b5f50cd to your computer and use it in GitHub Desktop.
Save pfgray/8f1fe1a482a8ee0e0da0d9ed8b5f50cd to your computer and use it in GitHub Desktop.
import { Monad2 } from "fp-ts/lib/Monad";
import { HKT } from "fp-ts/lib/HKT";
import { createSelector, Selector } from "reselect";
import { sequenceT } from "fp-ts/lib/Apply";
import { Do } from "fp-ts-contrib/lib/Do";
declare module "fp-ts/lib/HKT" {
interface URI2HKT2<L, A> {
Selector: Selector<L, A>;
}
}
export type Foo = HKT<any, any>
export const URI = "Selector";
export type URI = typeof URI;
export const selector: Monad2<URI> = {
URI,
of<State, A>(a: A): Selector<State, A> {
return () => a;
},
map<State, A, B>(fa: Selector<State, A>, f: (a: A) => B): Selector<State, B> {
return createSelector(
fa,
f
);
},
ap<State, A, B>(fab: Selector<State, (a: A) => B>, fa: Selector<State, A>): Selector<State, B> {
return createSelector(
fab,
fa,
(ab, a) => ab(a)
);
},
chain<State, A, B>(fa: Selector<State, A>, f: (a: A) => Selector<State, B>): Selector<State, B> {
return createSelector(
(state: State) => {
const a = fa(state);
const fb = f(a);
return fb(state);
},
fb => fb
);
}
};
sequenceT(selector) // passes
Do(selector) //fails with:
// selector-fp-ts.ts:50:4 - error TS2345: Argument of type 'Monad2<"Selector">' is not assignable to parameter of type 'Monad<"Selector">'.
// Types of property 'of' are incompatible.
// Type '<L, A>(a: A) => Selector<L, A>' is not assignable to type '<A>(a: A) => HKT<"Selector", A>'.
// Type 'Selector<{}, A>' is missing the following properties from type 'HKT<"Selector", A>': _URI, _A
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment