kleisli-ts
v1.0.0
Published
Kleisli IO for TypeScript
Downloads
196
Maintainers
Readme
Kleisli arrows for bifunctor IO
Part of fp-ts ecosystem.
TypeScript port of KleisliIO
– Kleisli arrows with bifunctor IO from great talk by John A. De Goes at LambdaConf'18 called "Blazing Fast, Pure Effects without Monads".
Please see examples for possible ways of programming with Kleisli arrows.
Installation & usage
- Install this module either via NPM or Yarn:
npm i kleisli-ts # or yarn add kleisli-ts
- This module has a peer dependency – fp-ts, so you'll need to install it as well:
npm i fp-ts@1 yarn add fp-ts@1
kleisli-ts
provides curried functions as its main API, but you also have a convenience methodgetInstancesFor
, which returns an API instance bound to the given monad:import { getInstancesFor } from 'kleisli-ts'; import { ioEither } from 'fp-ts/lib/IOEither'; const { liftK } = getInstancesFor(ioEither); const throwMe = liftK(() => { throw new Error('yay, it works'); });
Simple example
import { ioEither, URI as IOEitherURI } from 'fp-ts/lib/IOEither';
import { getInstancesFor, KleisliIO } from 'kleisli-ts/lib';
import { unsafeRunIE } from 'kleisli-ts/lib/unsafe';
const { impureVoid, liftK } = getInstancesFor(ioEither);
const k: KleisliIO<IOEitherURI, Error, void, string> = liftK(() => {
if (Math.random() > 0.5) {
throw new Error('oops');
}
return 'foo';
});
const log: KleisliIO<IOEitherURI, never, string, void> = impureVoid((s) => console.log(s));
unsafeRunIE(k.andThen(log).run());