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

freer

v0.1.0

Published

A free Static Land compatible Monad implementation

Downloads

4

Readme

Freer · Gitter GitHub stars npm

This is an experimental JavaScript library that implements a Free (or Freer) monad with composable effect handlers.

WARNING: This library is not yet ready for production use.

npm version Build Status Code Coverage

Contents

Examples

Getting started

First we create some handlers and free operations:

const aReader = F.Reader()
const aState = F.State()

Then we define an ad-hoc operation that uses the previously defined free operations:

const addReaderToState = F.from(async $ => {
  const v = await $(aReader.ask)
  return $(aState.modify(R.add(v)))
})

The above uses a do notation approximation provided by this library. One could also define the above operation using just the basic monadic combitors.

Then we compose a runner that handles the operations we used:

const aRunner = R.compose(F.runAsync, aState.run(1), aReader.run(2), F.toAsync)

Finally we run the operation and log the result:

aRunner(addReaderToState).then(console.log)
// Promise 3

Running sum of leaves

Here is an example using traverse from Partial Lenses to compute a running sum of the leaves of a nested data structure:

R.compose(F.run, aState.run(0))(
  L.traverse(
    F.Free,
    x => aState.modify(R.add(x)),
    L.leafs,
    [{x: 3, y: [1]}, {z: [4, 1]}]
  )
)
// [{x: 3, y: [4]}, {z: [8, 9]}]

Reference

The combinators provided by this library are provided as named exports. Typically one just imports the library as:

import * as F from 'freer

The examples also make use of the Partial Lenses and Ramda libraries imported as:

import * as L from 'partial.lenses'
import * as R from 'ramda'

Neither of those libraries is required in order to use the library.

Free monad

F.Free ~> monad

F.Free is the Static Land compatible Monad definition for the monad provided by this library.

Free combinators

F.map(value => value, free) ~> free

F.map is the Static Land compatible map combinator of the monad provided by this library.

F.of(value) ~> free

F.of is the Static Land compatible of combinator of the monad provided by this library.

F.ap(free, free) ~> free

F.ap is the Static Land compatible ap combinator of the monad provided by this library.

F.chain(value => free, free) ~> free

F.chain is the Static Land compatible chain combinator of the monad provided by this library.

Identity handler

F.run(free) ~> value

F.run is the identity handler for the free monad. It doesn't handle any effects per se, it just extracts the result of the computation.

F.runAsync(free) ~> promise

F.runAsync is the asynchronous identity handler for the free monad. It only handles promises.

Do notation

F.from(async $ => { ... await $(free) ... }) ~> free

F.from is used to wrap an async $ => { ... } function that await $( ... )s for effects, reminescent to a do notation, as a free operation to be handled by F.toAsync.

F.toAsync(free) ~> free

F.toAsync is the handler for the operations produced by F.from and converts those effects to promises. This handler must be before handlers for effects used in the operations produced by F.from and the promises need to be handled by F.runAsync. In other words, the handler composition should look like R.compose(F.runAsync, ..., F.toAsync).

Defining new handlers

F.handler((value, any) => free, (effect, continuation, any) => free) ~> (free[, any]) ~> free

F.handler defines a handler for some effects. The first argument is the handler for the final result. The second argument is the handler for some effects. It is given an effect, which the handler may or many not know how to handle, and the continuation and it must then return a free operation.

Handlers à la carte

F.Reader() ~> {ask, local, run}

F.Reader is a factory for Reader effects.

F.State() ~> {get, put, modify, run}

F.State is a factory for State effects.

Related work

The core of this library is based on ideas from Extensible Effects: an alternative to Monad Transformers.