Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

monet

monet36.7kMIT0.9.3TypeScript support: included

Monadic types library for JavaScript

readme

monet.js

For people who wish they didn't have to program in JavaScript. documentation

Introduction

Monet is a library designed to bring great power to your JavaScript programming. It is a tool bag that assists Functional Programming by providing a rich set of Monads and other useful functions.

This library is inspired by those that have come before, especially the FunctionalJava and Scalaz projects.

While functional programming may be alien to you, this library is a simple way to introduce monads and pure functional programming into your daily practices.

Documentation

Full detailed documentation can be found here

Installation

NPM

npm install monet --save

# or to install a specific version
npm install monet@0.9.3

Download

Download the zip or tar ball.

Browser

Simply download and add to your html pages. You can also include monet-pimp.js which contains extra functions on the Object.prototype for creating monads.

<script src="monet.js"></script>
<!-- Optionally -->
<script src="monet-pimp.js"></script>

Contents

Maybe

The Maybe type is the most common way of representing nothingness (or the null type) with making the possibilities of NullPointer issues disappear.

Maybe is effectively abstract and has two concrete subtypes: Some (also Just) and None (also Nothing).

Either

Either (or the disjunct union) is a type that can either hold a value of type A or a value of type B but never at the same time. Typically it is used to represent computations that can fail with an error. Think of it as a better way to handle exceptions. We think of an Either as having two sides, the success is held on the right and the failure on the left. This is a right biased either which means that map and flatMap (bind) will operate on the right side of the either.

Validation

Validation is not quite a monad as it doesn't quite follow the monad rules, even though it has the monad methods. It that can hold either a success value or a failure value (i.e. an error message or some other failure object) and has methods for accumulating errors. We will represent a Validation like this: Validation[E,A] where E represents the error type and A represents the success type.

Immutable lists

An immutable list is a list that has a head element and a tail. A tail is another list. The empty list is represented by the Nil constructor. An immutable list is also known as a "cons" list. Whenever an element is added to the list a new list is created which is essentially a new head with a pointer to the existing list.

Non Empty Lists

Much like the immutable list, a Non Empty List can never be empty. It implements the comonad pattern. It has a guaranteed head (total) and a guaranteed (total) tail.

IO

The IO monad is for isolating effects to maintain referential transparency in your software. Essentially you create a description of your effects of which is performed as the last action in your programme. The IO is lazy and will not be evaluated until the perform (alias run) method is called.

Reader

The Reader monad is a wonderful solution to inject dependencies into your functions. There are plenty of great resources to get your teeth into the Reader monad such as these great talks.

The Reader monad provides a way to "weave" your configuration throughout your programme.

Free

The Free monad is a monad that is able to separate instructions from their interpreter. There are many applications for this monad, and one of them is for implementing Trampolines, (which is a way to make recursion constant stack for languages that don't support tail call elimination, like JavaScript!).

Please see Ken Scambler's excellent talk and example project to get an in-depth understanding of this very useful monad.

Author

Written and maintained by Chris Myers @cwmyers and Jakub Strojewski @ulfryk. Follow Monet.js at @monetjs.

changelog

Changelog

0.9.3 (12 Nov 2021)

  • [fix] add Maybe.fromEmpty to typings -- thanks to @tomecko ( #246 )

0.9.2 (05 Mar 2021)

  • [new] add apTo to all Applicatives -- thanks to @iLikeKoffee ( #233 )
  • [new] add lookup and nth to lists -- thanks to @mlrv ( #241 )
  • [enhancement] change Maybe filter and filterNot to work as type guard -- thanks to @mlrv ( #240 )

0.9.1 (15 Jan 2020)

  • [new] add fromPromise and toPromise to Either -- thanks to @wookieb ( #223 )
  • [fix] fix .isInstance method -- thanks to @wookieb ( #221 )
  • [new] add Either.fromTry -- thanks to @rparree ( #211 )
  • [new] add Maybe.fromEmpty -- thanks to @customcommander ( #214 )

0.9.0 (19 Nov 2018)

rc.3 (18 Nov 2018)

  • [fix] broken exports ( #196 )

rc.2 (18 Nov 2018)

  • [fix] broken UMD for node imports ( #193 )

rc.1 (18 Sep 2018)

  • [fix] broken UMD for node imports -- thanks to @jfspencer ( #180 )

rc.0 (18 Jun 2018)

  • [new] add from static constructor method to NEL and List
  • [fix] fix broken List.equals
  • [fix] re-add cons and snoc to NEL (fix join, also add/fix flatten and flattenMaybe to collections)
  • [fix] drop .contains() from Either and Validation
  • [new] implement Iterable for List, NEL, Maybe and Identity
  • [new] add .every(), .forall() and .exists() to List, NEL, Maybe and Identity
  • [new] add .toSet() to List and NEL
  • [new] add .toArray(), .toSet() to Maybe
  • [new] add .orUndefined(), .getOrElse(), .orLazy() to Maybe
  • [new] add .swap() to Validation and Either
  • [new] add .catchMap() to Maybe, Validation and Either
  • [enhancement] make Maybe types stricter (compile type check for use of nullables) -- thanks to @jleider ( #167 )
  • [enhancement] make .equals() compatible with other libraries
  • [new] add static .isOfType() and .isInstance() to all types
  • [new] add .fromUndefined() to Maybe static
  • [new] add .filterNot() to Maybe, List and NEL -- thanks to @tomecko ( #152 )

alpha.4 (19 Jun 2017)

  • [fix] pre publish build

alpha.3 (19 Jun 2017)

  • [enhancement] make .chain() compatible with Fantasy Land
  • [enhancement] make .map() compatible with Fantasy Land
  • [enhancement] make .equals() compatible with Fantasy Land
  • [enhancement] add lowercase factory functions to satisfy linters -- thanks to @LukaszTheProgrammer ( #107 )
    • Maybe.some(val) and Maybe.none()
    • Either.right(val) and Either.left(err)
  • [enhancement] make .ap() compatible with Fantasy Land and Ramda -- thanks to @char0n ( #112 )
  • [new] add .ap() to Identity (so it's compatible with FantasyLand) -- thanks to @char0n ( #121 )
  • [new] add .orNoneIf() (alias .orNothingIf()) to Maybe -- thanks to @emmanueltouzery ( #100 )
  • [new] add .fromFalsy() to Maybe static -- thanks to @kpudlik ( #98 )
  • [new] add .forEach() to Identity, Maybe, Either, Validation, List and NEL -- thanks to @emmanueltouzery ( #95 ). And:
    • .orElseRun() to Maybe
    • .forEachLeft() to Either
    • .forEachFail() to Validation
  • [new] add .contains() to Identity, Maybe, Either, Validation, List and NEL -- thanks to @emmanueltouzery ( #93 )
  • [new] add .find() to List and NEL -- thanks to @emmanueltouzery ( #90 )
  • [new] add .fold() as alias for .cata() to Either and Validation -- thanks to @tbrisbane ( #82 )
  • [new] add .foldLeft() and .foldRight() to Maybe, Either, Validation, List and NEL -- thanks to @tbrisbane ( #82 )
  • [new] add .orNull() to Maybe -- thanks to @emmanueltouzery ( #86 )
  • [fix] fix typings to work with --noImplicitAny
  • [fix] fix typings to work with --strictNullChecks -- thanks to @emmanueltouzery ( #86 )

alpha.2

  • [fix] fix List's .size() ( #79 )
  • [fix] fix List's .map ( #64 )

alpha.1

  • [fix] fix Free monad

alpha.0

  • [new] .equals() method added to most entities
  • [breaking] all native prototype extensions extracted to monet-pimp.js
  • [enhancement] typings compatible with TS2.x
  • [fix] updated module pattern

0.8.10

  • [fix] Fixed bower.json issues ( #51 )
  • [fix] Fixed internal curry implementation ( #55 )

0.8.9

  • [new] add TypeScript typings
  • [new] add .ap() to List, Free and NonEmptyList -- thanks to @WojciechP ( #44 )
  • [new] add "use strict"; -- thanks to @krjackso ( #41 )

Typings

TypeScript typings added to repository, so now anyone can:

import { Maybe } from 'monet';

export function getStoredData(key: string): Maybe<SomeData> {
  return Maybe.fromNull(localStorage)
    .flatMap(ls => Maybe.fromNull(ls.getItem(key)))
    .map(JSON.parse);
}

0.8.7

  • [new] add .cata(…) to Maybe -- thanks to Crisson Jno-Charles @crisson