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

generalized-do-notation

v1.0.0

Published

util for building type-safe do notation for any monad in typescript

Downloads

4

Readme

Generalized do notation

Small lib that provides util for creating type-safe and strict do-notation for any monad.

Installation

npm install generalized-do-notation

Usage

Creating do notation for a monad

declare the next type: (substitute MONAD with the real type of the monad)

type DoMONAD<r> = {
  $: <k extends string, v>(k: k, next: (r: r) => MONAD<v>) => DoMONAD<AddKvToRecord<r, k, v>>
  _: <v>(next: (r: r) => MONAD<v>) => DoMONAD<r>
  e: <v>(exit: (r: r) => MONAD<v>) => MONAD<v>
  pure: <v>(exit: (r: r) => v) => MONAD<v>
}

create do notation using util mkDoNotationFor: (substitute MONAD with the real type of the monad) pass pure, map, bind to the config of the function

export const doMONAD: DoMONAD<{}> = mkDoNotationFor({
  pure,  // <a>    (x: a)                  => MONAD<a> 
  map,   // <a, b> (f: (x: a) => b)        => (m: MONAD<a>) => MONAD<b>
  bind,  // <a, b> (f: (x: a) => MONAD<b>) => (m: MONAD<a>) => MONAD<b>
})

your do notation is ready!

Using do notation

after creation you get

doMONAD // (substitute `MONAD` with the real type of the monad)

constant.

which can be used like this: (substitute MONAD with the real type of the monad)

const result = doMONAD
    // 'unwraps' someMONADvalue and stores it's value to `a`:
    .$('a', _ => someMONADvalue) 
    // `a` from prev step can be read here (or in any lower step) like this: 
    // (also `fn` here is a some function that takes value and returns MONAD
    // returned value is stored to `b` and can be read in lower steps
    // )
    .$('b', x => fn(x.a))      
    // if there is no need of reading value of the monad and it's only importand 
    // to have sideEffect of this monad, there is `_` method which binds the 
    // monad the same way as `$` does, but doesn't stores it's value
    ._(x => sideEffectMONAD)  
    // every do notation must be finished with 'exit' 
    // there is `e` method that finishes binding with the last monad
    // in this example the last monad is the one produced by `fn2` function. 
    // the result of `fn2` function will be stored into `const result` (see start of this chain)
    .e(x => fn2(x.b))

const result = doMONAD = doMONAD
    // there is another way of exiting from do notation: 
    // method `pure`. it just wraps any value returned by the function into MONAD
    // `const result` will be 42 wrapped into MONAD
    .pure(x => 42)

Typescript limitations

Documentation

detailed documentation and examples will be provided later.