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

state-monad

v1.1.0

Published

An implementation of the state monad, following fantasy-land conventions

Downloads

7

Readme

state-monad

Build Status

This is an implementation of the state monad, following a similar interface to Haskell's but following fantasy-lands naming conventions.

Examples

.of to initialise pipeline

The .of function will assign the default value in the left side of the tuple.

.runState

.runState will run the state pipeline using the initial state provided. Virtually .of assigns the value side of the tuple, .runState loads the state side. [value, state].

.map to update evaluation

The .map function works with the left side of the State tuple. This allows you to work across functions without changing the final state you're working with.

const triple = (a) => a * 3

State.of(4)
  .map(triple)
  .runState({ init: 4 })
// [ 12, { init: 4 } ]

.chain to update state

.chain is when you want to update the State that you are working with. To follow the .chain convention you need to return a State, the State provided will receive the current state through the execution pipeline.

const State = require('state-monad')

const triple = (a) => a * 3

State.of(4)
  .map(triple)
  .chain(n => State(s => [n, Object.assign({}, { tripled: n }, s)])) // n is the result of map, s is the state
  .runState({ init: 4 })
// [ 12, { tripled: 12, init: 4 } ]

Helpers

.withState

.withState is a higher order function with the signature (a, b) -> a where the state tuple is [a, b]. The result will update the value in the tuple.

const add = (a, b) => a + b

State
  .of(4)
  .withState(add)
  .runState(5)
// [ 9, 5 ]

.exec

.exec is similar to .runState but it only returns the right side of the tuple. The current "state"

const add = (a, b) => a + b

State
  .of(4)
  .withState(add)
  .exec(5)
// 5

.eval

.eval is similar to .runState but it only returns the left side of the tuple. The current "value"

const add = (a, b) => a + b

State
  .of(4)
  .withState(add)
  .eval(5)
// 9

.then

.then will work similar to a Promise.then. If a State is returned, it will treat the .then as .chain, otherwise treat it similarly to .map

State.of(6)
  .then(v => v * 7)
  .runState(6),
// [42, 6]

State.of(6)
  .then(v => State(s => [v * 7, s * 2]))
  .runState(6),
// [42, 12]

State.get

This is useful when working within a chain block. It loads the state into the left side of the tuple, which you can then map over and leave the current state.

You can also use State.of('value').get() if you want to create a pipelined chain. This loads the state into the value.

const State = require('state-monad')

const triple = (a) => a * 3

State.of(4)
  .map(triple)
  .chain(n => State.get().map(s => Object.assign({}, { tripled: n }, s))) // n is the result of map, s is the state
  .runState({ init: 4 })
// [ { tripled: 12, init: 4 }, { init: 4 } ]

State.put

The inverse of State.get. This will update/override the current state and value to whatever is provided.

You can use State.of('value').put('some_value') if you need to update the state with a constant within the pipeline.

State.of(4)
  .map(triple)
  .chain(n => State.put({ tripled: n }))
  .runState({ init: 4 })
// [ { tripled: 12 }, { tripled: 12 } ]