kleisli-ts
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

Kleisli arrows for bifunctor IO

npm Build Status

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

  1. Install this module either via NPM or Yarn:
    npm i kleisli-ts
    # or 
    yarn add kleisli-ts
  2. 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
  3. kleisli-ts provides curried functions as its main API, but you also have a convenience method getInstancesFor, 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());

Package Sidebar

Install

npm i kleisli-ts

Weekly Downloads

45

Version

1.0.0

License

MIT

Unpacked Size

160 kB

Total Files

20

Last publish

Collaborators

  • ybogomolov