@dodgez/ts-monads
v0.1.0
Published
Monads in TypeScript
Readme
ts-monads
This package provides monads based on my (limited) experience with Haskell and has a lot of issues and incomplete functionality.
There are currently two monads provided for use in Typescript:
- The IO monad wraps input/output operations similar to Haskell
- The Maybe monad (and type!) provides an interface for maybe types
Note: IO commands are only meant to be used as arguments to a top-level function call of io_main.
Commands
- Compile the package, run
npm run build - Run unit tests, run
npm test - Run an example, run
ts-node examples/{io,monad}.example.ts
Examples
examples/io.example.tsan example that printspackage.jsonwithout "extra" whitespace.examples/maybe.example.tsan example of a safelist.headand division function.
Drawbacks
The (>>=) and (>>) operators are not valid identifiers, and TypeScript doesn't support infix operators, so the following are replacements:
>>=is replaced with$$_and is called on the left object. E.g.IO.return(3).$$_(io_log)>>is replaced with$$and is called on the left object. E.g.io_log("Hello").$$(io_log("World!"))
Since TypeScript does not natively support macros, do syntax is not supported thus every Monad chain needs to be written manually.
