All Projects → teves-castro → ts-do

teves-castro / ts-do

Licence: MIT license
Do like notation for typescript using fp-ts

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to ts-do

workshop-edsl-in-typescript
Code template for workshop "Building eDSLs in functional TypeScript"
Stars: ✭ 49 (-10.91%)
Mutual labels:  fp-ts
fp-ts-ramda
Ramda functions reimplemented in fp-ts
Stars: ✭ 129 (+134.55%)
Mutual labels:  fp-ts
purifree
Pointfree type-safe functional programming library for TypeScript - with do notation, HKTs, generic lifts and more
Stars: ✭ 64 (+16.36%)
Mutual labels:  fp-ts
typescript-react-starter
React & TypeScript Starter with webpack, ts-jest and runtime environment variables. It comes with fp-ts ecosystem and pre-configured prettier, eslint, vscode, husky hooks and Dockerfile to build a deployable image of your app
Stars: ✭ 17 (-69.09%)
Mutual labels:  fp-ts
retry-ts
Retry combinators for monadic actions that may fail
Stars: ✭ 151 (+174.55%)
Mutual labels:  fp-ts
kotlin-monads
Monads for Kotlin
Stars: ✭ 114 (+107.27%)
Mutual labels:  do-notation
vscode-fp-ts-codegen
Expands haskell-syntax ADTs to typescript equivalent types definitions using gcanti/fp-ts-codegen
Stars: ✭ 16 (-70.91%)
Mutual labels:  fp-ts
Pico
Take browser screenshots in Javascript 📸
Stars: ✭ 1,807 (+3185.45%)
Mutual labels:  fp-ts
fp-ts-fluture
fp-ts bindings for Fluture
Stars: ✭ 48 (-12.73%)
Mutual labels:  fp-ts
fp-ts-cheatsheet
FP-TS Cheat Sheet
Stars: ✭ 276 (+401.82%)
Mutual labels:  fp-ts
mutoid
Reactive library for data fetching, caching, state management
Stars: ✭ 24 (-56.36%)
Mutual labels:  fp-ts

Description

This library works as an extension for fp-ts allowing the usage of a haskell like do notation. One can use exec, bind, sequence and into to chain computations on any of the supplied monads. Each bind or sequence in the computation chain contributes to a threaded context that is available to each subsequent step. The exec function can also be used to perform computations that add nothing to the context for their side-effects. It is possible to use this with any monads as long as its kind is defined in fp-ts.

Installation

To install the stable version:

yarn add ts-do

Using the extension

Start by importing the extension:

import { some, map, option } from "fp-ts/lib/Option"
import { pipe } from "fp-ts/lib/pipeable"
import { array } from "fp-ts/lib/Array"
import { range, sum } from "ramda"
import * as Do from "ts-do"

const bind = Do.bind(option)
const into = Do.into(option)
const exec = Do.exec(option)
const sequence = Do.sequence(array, option)

const result = pipe(
  some(3),
  into("x"), // Chains the computation. Creates a context with { x: 3 }
  exec(() => some(undefined)), // Chains the computation. Adds nothing to the context
  sequence("ys", ({ x }) => range(0, x).map(() => some(1))), // Sequences computations. Adds { ys: [1, 1, 1] } to the context
  map(({ x, ys }) => x - sum(ys)),
)

// result === some(0)

Check the test folder for a few more examples.

Building

Clone the repo

git clone [email protected]: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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].