npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fts-monad

v1.1.2

Published

Monad interface for functional constructs in typescript

Downloads

6

Readme

Monad

Mathmatically, Monads are sed to map functions into other functions. For general use in programming though, it's best to think of them as a collection of functions.

For a more through information on Monads, the reader is encouraged to google search "Haskell Monad Introduction".

To cheat, you can think of a Monad as a Promise for synchonous code. You wrap up some code in the monad, and then call ".then" on it to change things. While the two constructs are indeed closely related, this analogy breaks down in the specifics.

We provide this package primarily to support development purely functional data structures, and other purely functional libraries.

Short Version

  • A monad is kind of box for functions and data.
  • A monad is not an object, but an object can be a monad.
  • There is no "Monad" class, our examples will use a fictitious "MyMonad" and a ficticious "DifferentMonad".
const myMonad = new MyMonad(12);
//myMonad == {value: 12, tag: "MyMonad", ....}

You can change the stuff in the box by calling "fmap" which gives you the same kind box with changed stuff.

const myMonad = new MyMonad(12);
//myMonad === {value: 12, tag: "MyMonad", ....}

const anotherMyMonad = myMonad.fmap(x => x*2);
//anotherMyMonad === {value: 24, tag: "MyMonad", ....}

You can chain together the ".fmap" calls and do something like the following

const myMonad =
    new MyMonad(12) //value: 12
        .fmap(x => x*2) //value: 24
        .fmap(x => x+6) //value: 30

All well and good, but what if you need to change the type of monad the stuff is in? You use then.

const myMonad = new MyMonad(12);
//myMonad === {value: 12, tag: "MyMonad", ....}

const differentMonad = myMonad.then( x => new DifferentMonad(x.toString) )
//differentMonad === {someString: "12", theTag: "DifferentMonad", ...}

The above code doesn't make the reason for doing such a thing terribly clear, but it really does turn out to be insanely useful for lots of problems.

And finally, some code using Maybe (which is a kind of monad)

//Map.get will return a Maybe<number>  If it has a value, assume it's 12
const maybeValue = Map.get(aMap, "someKey")
    .fmap( x => x*2 ) // If it has a value, the value is now 24
    .fmap( x => x + 6 ) //If it has a value, the value is now 30

//maybeValue is now "Just 30" or "Nothing"