JSPM

  • Created
  • Published
  • Downloads 7
  • Score
    100M100P100Q49367F
  • License MIT

Do like notation for typescript and fp-ts

Package Exports

  • ts-do

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ts-do) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Description

This library works as an extension for fp-ts allowing the usage of a haskell like do notation. One can use .let and .for to chain computations on any of the supplied monads. Each .let or .for in the computation chain contributes to a threaded context that is available to each subsequent step.

Installation

To install the stable version:

yarn install ts-do

Using the extension

Start by importing the extension:

import { some } from "fp-ts/lib/Option"
import { range, sum } from "ramda"
import "ts-do"

const result = some({})
    .let("x", some(3))
    .for("ys", ({ x }) => range(0, x).map(() => some(1)))
    .return(({ x, ys }) => x - sum(ys))

// result === some(0)

Check the test folder for a few more examples.

Building

Clone the repo

git clone git@github.com:teves-castro/ts-do.git

Install dependencies

yarn

Test

yarn run test

Compile

yarn run build

Disclaimer

These kind of operations (do notation) should be done with compiler support, like the special case of async/await for promises, that is included in most major languages these days.

Although these languages are adopting more and more functional programming constructs, sadly more advanced concepts are left out due to not having critical mass to demand it's implementation.

So, this is an experiment on what can be accomplished without that compiler support. Meaning that if/when this is supported natively by the language itself, certainly with different syntax, this will become obsolete.

Also I make no commitment to evolve the library beyond my own needs, so bear these warnings in mind when using the library. And of course I appreciate any comments and suggestions on how to improve or on how I completely blundered something.