### Install monet.js via NPM
Source: https://github.com/monet/monet.js/blob/develop/README.md
Installs the monet.js library using npm, the Node Package Manager. It also shows how to install a specific version.
```bash
npm install monet --save
# or to install a specific version
npm install monet@0.9.3
```
--------------------------------
### Install Dependencies
Source: https://github.com/monet/monet.js/blob/develop/CONTRIBUTING.md
Installs all necessary dependencies for working on the Monet.js library. This command should be run after cloning the repository.
```bash
npm install
```
--------------------------------
### Chaining DOM Read/Write Effects
Source: https://github.com/monet/monet.js/blob/develop/docs/IO.md
An example demonstrating how to chain DOM read and write operations using the IO monad. It shows how to wrap side-effecting functions (`read`, `write`) into IO, chain them using `map` and `flatMap`, and finally execute the combined effect with `run`.
```javascript
const read = id => IO(() => $("#myId").text())
const write = id => value => IO(() => $("#myId").text(value))
const toUpper = text => text.toUpperCase()
const changeToUpperIO = read("#myId").map(toUpper).flatMap(write("#myId"))
// To execute the effect:
// changeToUpperIO.run()
```
--------------------------------
### Get Value from Maybe
Source: https://github.com/monet/monet.js/blob/develop/docs/MAYBE.md
The `some` method extracts the value from a `Some` instance. It will throw an error if called on a `None` instance. Use `orSome` for safer extraction.
```scala
Maybe[A].some(): A
```
```javascript
Some('hi').some()
// => 'hi'
```
--------------------------------
### IO Monad Constructors
Source: https://github.com/monet/monet.js/blob/develop/docs/IO.md
Demonstrates various ways to create an IO monad instance in JavaScript, including direct instantiation and using static methods like `io`, `of`, `unit`, and `pure`. These methods wrap a function that describes an effect.
```javascript
IO(() => $("#id").val())
IO.io(() => $("#id").val())
IO.of(() => $("#id").val())
IO.unit(() => $("#id").val())
IO.pure(() => $("#id").val())
```
--------------------------------
### FoldLeft Method
Source: https://github.com/monet/monet.js/blob/develop/docs/LIST.md
Reduces the list to a single value by applying a function from left to right, starting with an initial value.
```javascript
List.fromArray([1, 2, 3, 4]).foldLeft(0)((acc, e) => e + acc)
// => 10
```
--------------------------------
### Creating IO from Pimped Function
Source: https://github.com/monet/monet.js/blob/develop/docs/IO.md
Shows how to create an IO monad from an existing function using the `.io()` and `.io1()` methods. `.io()` wraps a function that takes no arguments, while `.io1()` returns a function that produces an IO when a single argument is supplied.
```javascript
(() => $("#id")).io()
(id => $(id)).io1()
$.io1()
```
--------------------------------
### Creating a Reader Monad
Source: https://github.com/monet/monet.js/blob/develop/docs/READER.md
Demonstrates how to create a Reader monad to manage dependencies. It shows the evolution from a standard function to a Reader-wrapped function, including using the 'reader()' extension for cleaner syntax.
```javascript
const createPrettyName = (name, printer) => printer.write("hello " + name)
const render = printer => createPrettyName("Tom", printer)
// Curried version
const createPrettyName = name => printer => printer.write("hello " + name)
const render = () => createPrettyName("Tom")
// Reader monad version
const createPrettyName = name =>
Reader(printer => printer.write("hello " + name))
// Using monet-pimp
const createPrettyName = ((name, printer) => printer.write("hello " + name)).reader()
// Using map and run
const reader = () =>
createPrettyName("Tom").map(s => `---${s}---`)
reader().run(new BoldPrinter())
```
--------------------------------
### Get Value or Default
Source: https://github.com/monet/monet.js/blob/develop/docs/MAYBE.md
The `orSome` method (aliased as `orJust` or `getOrElse`) returns the contained value if the Maybe is `Some`, otherwise it returns a provided default value.
```scala
Maybe[A].orSome(a: A) : A
```
```javascript
Some('hi').orSome('bye')
// => 'hi'
None().orSome('bye')
// => 'bye'
```
--------------------------------
### Reader Monad Methods
Source: https://github.com/monet/monet.js/blob/develop/docs/READER.md
Details the core methods of the Reader monad, including map, flatMap (bind/chain), ap, apTo, and run. Explains their functionality in transforming and executing the wrapped computation.
```APIDOC
Reader[E, A].map(f: A => B): Reader[E, B]
Maps the supplied function over the Reader.
Reader[E, A].bind(fn: A => Reader[E, B]): Reader[E, B]
Aliases: bind, chain
Performs a monadic bind over the Reader.
Reader[E, A].ap(a: Reader[E, A=>B]): Reader[E, B]
Applies the function inside the supplied Reader to the value 'A' in the outer Reader. Applicative Functor pattern.
Reader[E, A=>B].apTo(a: Reader[E, A]): Reader[E, B]
Just an alias for ap with swapped this & argument.
Reader[E, A].run(env: E): A;
Executes the function wrapped in the Reader with the supplied config.
```
--------------------------------
### Run Tests
Source: https://github.com/monet/monet.js/blob/develop/CONTRIBUTING.md
Executes the test suite for the Monet.js library. It is recommended to run tests before creating a pull request to ensure code quality.
```bash
npm test
```
--------------------------------
### Include monet.js in HTML
Source: https://github.com/monet/monet.js/blob/develop/README.md
Demonstrates how to include the monet.js library and optionally monet-pimp.js in an HTML file for browser usage.
```html
```
--------------------------------
### Free Monad Methods
Source: https://github.com/monet/monet.js/blob/develop/docs/FREE.md
Provides core functional programming operations for the Free monad, including mapping, flatMapping, and resuming computation layers.
```scala
Free[F[_], A].map(f: A => B): Free[F[_], B]
Free[F[_], A].flatMap(f: A => Free[F[_], B]): Free[F[_], B]
Free[F[_], A].resume(): Either[F[Free[F[_], A]], A]
Free[F[_], A].go(f: F[Free[F[_], A]] => Free[F[_], A]) : A
```
--------------------------------
### NonEmptyList Constructors
Source: https://github.com/monet/monet.js/blob/develop/docs/NEL.md
Demonstrates various ways to construct a NonEmptyList, including direct instantiation, factory methods like `of`, `unit`, and `pure`, and handling of empty lists.
```javascript
NonEmptyList(1, List.fromArray([2, 3, 4]))
NEL(1, List.fromArray([2, 3, 4]))
NEL.of(1, List.fromArray([2, 3, 4]))
NEL.unit(1, List.fromArray([2, 3, 4]))
NEL.pure(1, List.fromArray([2, 3, 4]))
// => NonEmptyList(1,2,3,4)
NEL(1, Nil)
// => NonEmptyList(1)
```
--------------------------------
### Free Monad Constructors
Source: https://github.com/monet/monet.js/blob/develop/docs/FREE.md
The Free monad has two constructors: Suspend for continuations and Return for completion. Suspend wraps a Functor containing another Free, while Return wraps a value.
```scala
Return(a: A): Free[F[_], A]
Suspend(f: F[Free[F,A]]): Free[F[_], A]
```
--------------------------------
### IO Monad Methods: flatMap, map, run
Source: https://github.com/monet/monet.js/blob/develop/docs/IO.md
Details the core methods of the IO monad: `flatMap` (also aliased as `bind` or `chain`) for sequencing effects, `map` for transforming the result of an effect, and `run` (aliased as `perform` or `performUnsafeIO`) for executing the encapsulated effect.
```APIDOC
IO[A].flatMap(fn: A => IO[B]): IO[B]
- Perform a monadic bind (flatMap) over the effect.
- Takes a function that returns an IO.
- Happens lazily and does not evaluate the effect.
IO[A].map(fn: A => B): IO[B]
- Performs a map over the result of the effect.
- Happens lazily and does not evaluate the effect.
IO[A].run(): void
- Aliases: perform, performUnsafeIO
- Evaluates the effect inside the IO monad.
- Can only be run once in your program and at the very end.
```
--------------------------------
### Validation Constructors
Source: https://github.com/monet/monet.js/blob/develop/docs/VALIDATION.md
Demonstrates how to create Success and Fail instances of the Validation type, along with static factory methods like Validation.of, Validation.unit, and Validation.pure.
```javascript
Success(2)
// => Success(2)
Validation.of('a')
Validation.unit('a')
Validation.pure('a')
Validation.Success('a')
Validation.success('a')
// => Success("a")
Fail('some error')
// => Fail("some error")
Validation.Fail(7777)
Validation.fail(7777)
// => Fail(7777)
```
--------------------------------
### Maybe Constructors
Source: https://github.com/monet/monet.js/blob/develop/docs/MAYBE.md
Demonstrates the various ways to construct Maybe, Some, and None instances in Monet.js, including static methods and aliases.
```javascript
Some(5)
Just(5)
// => Some(5)
Maybe.of('a')
Maybe.unit('a')
Maybe.pure('a')
Maybe.some('a')
Maybe.Some('a')
// => Some("a")
Maybe.fromNull('b')
Maybe.fromFalsy('b')
Maybe.fromUndefined('b')
// => Some("b")
None()
Nothing()
Maybe.none()
Maybe.None()
Maybe.Nothing()
// => None
Maybe.fromNull()
Maybe.fromNull(null)
Maybe.fromNull(undefined)
// => None
Maybe.fromFalsy()
Maybe.fromFalsy(null)
Maybe.fromFalsy(0)
Maybe.fromFalsy('')
Maybe.fromFalsy(false)
// => None
Maybe.fromUndefined()
Maybe.fromUndefined(undefined)
// => None
Maybe.fromEmpty()
Maybe.fromEmpty(undefined)
Maybe.fromEmpty(null)
Maybe.fromEmpty('')
Maybe.fromEmpty([])
Maybe.fromEmpty({})
// => None
```
--------------------------------
### Free Monad Constructors
Source: https://github.com/monet/monet.js/blob/develop/docs/FREE.md
The Free monad has two constructors: Suspend for continuations and Return for completion. Suspend wraps a Functor containing another Free, while Return wraps a value.
```javascript
const a = Return(1)
const sum = Suspend(Identity(Return(1)))
// or
const sum = Free.liftF(Identity(1))
```
--------------------------------
### Either Constructors
Source: https://github.com/monet/monet.js/blob/develop/docs/EITHER.md
Demonstrates various ways to construct Either instances, including Right, Left, and static factory methods like of, unit, pure, fromTry, and fromPromise.
```javascript
Right([1, 3, 5])
// => Right([1,3,5])
Either.of('a')
Either.unit('a')
Either.pure('a')
Either.Right('a')
Either.right('a')
// => Right("a")
Left(2);
// => Left(2)
Either.Left('a')
Either.left('a')
// => Left("a")
```
```javascript
const success = val.right()
const failure = 'some error'.left()
```
```javascript
const i = 1;
Either.fromTry(() => i.toPrecision(500))
```
```javascript
Either.fromPromise(Promise.resolve({foo: 'bar'})) // Promise of Either.Right
Either.fromPromise(Promise.reject(new Error('error'))) // Promise of Either.Left
```
--------------------------------
### Creating Validation from Pimped Object
Source: https://github.com/monet/monet.js/blob/develop/docs/VALIDATION.md
Shows how to create Validation instances by 'pimping' existing values with success() or fail() methods.
```javascript
const success = val.success()
const failure = 'some error'.fail()
```
--------------------------------
### Free Monad Aliases
Source: https://github.com/monet/monet.js/blob/develop/docs/FREE.md
Alternative names for the flatMap operation on the Free monad, commonly referred to as bind or chain.
```scala
flatMap
Aliases: bind, chain
```
--------------------------------
### Monet.js Core Concepts
Source: https://github.com/monet/monet.js/blob/develop/README.md
Provides an overview of the core functional programming concepts implemented in monet.js, including Maybe, Either, Validation, and different list types.
```javascript
// Maybe: Represents nothingness, with Some and None subtypes to avoid NullPointer issues.
// Either: A disjunct union type holding either an A or a B, typically used for error handling (right-biased).
// Validation: Similar to Either but designed for accumulating errors, not strictly a Monad.
// Immutable List: A linked list with a head and tail, created immutably (Nil for empty list).
// Non Empty List: An immutable list that guarantees a head and tail, implementing the comonad pattern.
// IO: Isolates side effects to maintain referential transparency, evaluated lazily via 'perform' or 'run'.
// Reader: A monad for injecting dependencies and weaving configuration throughout a program.
// Free: Separates instructions from their interpreter, useful for trampolining and managing effects.
```
--------------------------------
### List Creation from Pimped Array
Source: https://github.com/monet/monet.js/blob/develop/docs/LIST.md
Shows how to create a list from an array using the `list()` method provided by the `monet-pimp.js` extension.
```javascript
const myList = [1,2,3].list()
// which is equivalent to:
// const myList = List(1, List(2, List(3, Nil)))
```
--------------------------------
### Creating Maybe from Pimped Object
Source: https://github.com/monet/monet.js/blob/develop/docs/MAYBE.md
Shows how to create a Maybe instance by 'pimping' an existing object or value using the `.some()` method.
```javascript
const maybe = 'hello world'.some()
const maybe = val.some()
```
--------------------------------
### Type Annotations in JavaScript
Source: https://github.com/monet/monet.js/blob/develop/docs/README.md
Demonstrates how to use type annotations in JavaScript for clarity, especially when dealing with functional programming concepts like Monads. It shows generic types and function signatures.
```scala
List[A]
```
```javascript
(a: A, b: B) => C
```
```javascript
Maybe[A].fromNull(a: A): Maybe[A]
```
```javascript
A => B
```
```javascript
x: (a: A => B, c: B => C) => C
```
```javascript
() => A
```
```javascript
A => ()
```
--------------------------------
### Applicative Functor Pattern (apTo)
Source: https://github.com/monet/monet.js/blob/develop/docs/MAYBE.md
The `apTo` method is an alias for `ap` with swapped arguments, providing a more convenient way to apply curried functions in JavaScript. It handles `Nothing` cases gracefully.
```scala
Maybe[A].apTo(Maybe[B]): Maybe[B]
```
```ecmascript 6
const f3 = x => y => z => x + y + z
const applicative = Maybe.of(f3)
applicative
.apTo(Maybe.of(1)) // Maybe with partially applied f3 with 1 instead of x
.apTo(Maybe.of(2)) // Maybe with 1 instead of x and 2 instead of y
.apTo(Maybe.of(3)) // Maybe with f3 result (6)
/* Code below is equivalent of code upper. But ap cannot be chained. */
Maybe.of(3).ap(Maybe.of(2).ap(Maybe.of(1).ap(applicative)))
```
```ecmascript 6
applicative
.apTo(Maybe.of(1))
.apTo(Maybe.of(2))
.apTo(Maybe.of(3)) // Just 6
Maybe.Nothing()
.apTo(Maybe.of(1))
.apTo(Maybe.of(2))
.apTo(Maybe.of(3)) // Nothing
applicative
.apTo(Maybe.of(1))
.apTo(Maybe.of(2))
.apTo(Maybe.Nothing()) // Nothing
```
--------------------------------
### List Constructors
Source: https://github.com/monet/monet.js/blob/develop/docs/LIST.md
Demonstrates various ways to create immutable lists using the List constructor, including empty lists, single elements, nested lists, and creation from arrays or iterables.
```javascript
List();
// => Nil
List('a');
// => List('a')
List('a', List('b', List('c')));
// => List('a', 'b', 'c')
List.of('a')
List.unit('a')
List.pure('a')
// => List('a');
List.fromArray(['a', 'b', 'c']);
// => List('a', 'b', 'c')
const unique = new Set(['a', 'b', 'a', 'b', 'c', 'c', 'a']);
List.from(unique);
// => List('a', 'b', 'c')
```
--------------------------------
### Monad Core Operations
Source: https://github.com/monet/monet.js/blob/develop/docs/README.md
Defines the fundamental operations that all Monads in MonetJS must implement, including bind, map, unit, ap, and join. These methods are essential for monadic composition and manipulation.
```APIDOC
Monad[A]:
bind(f: A => Monad[B]): Monad[B]
Aliases: flatMap, chain
Performs a monadic bind.
map(f: A => B): Monad[B]
unit(A): Monad[A]
Aliases: pure, of
ap(m: Monad[A => B]): Monad[B]
join(): Monad[A]
The inner and outer monads are the same type.
```
--------------------------------
### apTo Method (Applicative Functor Alias)
Source: https://github.com/monet/monet.js/blob/develop/docs/VALIDATION.md
An alternative syntax for the `ap` method where the Validation containing the function is the first argument. Useful for chaining.
```scala
Success(person)
.apTo(validateForename)
.apTo(validateSurname)
.apTo(validateAddress) // => Success("Tom Baker lives at Dulwich, London")
Success(person)
.apTo(validateForename)
.apTo(Fail(['no surname']))
.apTo(Fail(['no address'])) // => Fail(["no address", "no surname"])
```
--------------------------------
### Function Currying
Source: https://github.com/monet/monet.js/blob/develop/docs/PIMP.md
The `curry` method transforms a function into a curried version, enabling partial application. This allows functions to be called with arguments spread across multiple calls, offering flexibility in how functions are invoked.
```javascript
const sum = function(a, b, c) {
return a + b + c
}.curry()
sum(1)(2)(3) // returns 6
```
--------------------------------
### Function Composition with andThen
Source: https://github.com/monet/monet.js/blob/develop/docs/PIMP.md
The `andThen` function provides an alternative way to compose functions, flipping the order of execution compared to `compose`. `f.andThen(g)` results in a function that applies `f` first, then `g`, equivalent to `x => g(f(x))`.
```javascript
fn.andThen(fn1)
// Equivalent to: x => g(f(x))
```
--------------------------------
### Maybe.cata Method
Source: https://github.com/monet/monet.js/blob/develop/docs/MAYBE.md
Explains the `cata` (catamorphism) method, which allows for handling both Some and None cases by providing two functions: one for the None case and one for the Some case.
```scala
Maybe[A].cata(leftFn: () => X, rightFn: A => X): X
maybe.cata(
() => `oh dear it failed because`,
value => `yay! ${value}`
)
```
--------------------------------
### Maybe Monad Operations
Source: https://github.com/monet/monet.js/blob/develop/docs/README.md
Provides methods for working with the Maybe monad, including mapping, flattening, chaining, and conditional operations. Useful for handling optional values gracefully.
```javascript
map(f: (a: A) => B): Maybe
flatMap(f: (a: A) => Maybe): Maybe
bind(f: (a: A) => Maybe): Maybe
chain(f: (a: A) => Maybe): Maybe
catchMap(f: (a: A) => Maybe): Maybe
cata(whenNone: () => R, whenSome: (a: A) => R): R
fold(whenNone: () => R, whenSome: (a: A) => R): R
foldLeft(initial: B, f: (b: B, a: A) => B): B
foldRight(initial: B, f: (a: A, b: B) => B): B
isSome(): boolean
isJust(): boolean
isNone(): boolean
isNothing(): boolean
some(a: A): Maybe
just(a: A): Maybe
orSome(a: A): Maybe
orJust(a: A): Maybe
getOrElse(a: A): A
orLazy(f: () => Maybe): Maybe
orNull(): A | null
orUndefined(): A | undefined
orElse(other: Maybe): Maybe
orNoneIf(predicate: (a: A) => boolean): Maybe
orNothingIf(predicate: (a: A) => boolean): Maybe
ap(other: Maybe<(a: A) => B>): Maybe
contains(a: A): boolean
every(predicate: (a: A) => boolean): boolean
forall(predicate: (a: A) => boolean): booleanexists(predicate: (a: A) => boolean): boolean
forEach(f: (a: A) => void): Maybe
orElseRun(f: () => void): Maybe
toEither(): Either
toValidation(): Validation
toArray(): A[]
toList(): List
toSet(): Set
to(constructor: { new(...args: any[]): B }): B
filter(predicate: (a: A) => boolean): Maybe
filterNot(predicate: (a: A) => boolean): Maybe
equals(other: Maybe): boolean
join(): A
takeLeft(n: number): Maybe
takeRight(n: number): Maybe
```
--------------------------------
### Free.liftF
Source: https://github.com/monet/monet.js/blob/develop/docs/FREE.md
Lifts a Functor F into a Free monad, allowing a Functor to be used within the Free structure.
```scala
Free.liftF(f: F[A]): Free[F[_], A]
```
--------------------------------
### Maybe Type API
Source: https://github.com/monet/monet.js/blob/develop/docs/MAYBE.md
API documentation for the Maybe type in Monet.js.
```APIDOC
Maybe[T]
// Represents a value that may or may not be present.
// Methods:
some(): T
// Extracts the value. Throws if None.
orSome(defaultValue: T): T
// Returns the value or a default.
orLazy(lazyDefault: () => T): T
// Returns the value or a lazily computed default.
orNull(): T | null
// Returns the value or null.
orUndefined(): T | undefined
// Returns the value or undefined.
orElse(other: Maybe[T]): Maybe[T]
// Returns this Maybe or another if this is None.
orNoneIf(condition: Boolean): Maybe[T]
// Returns None if condition is true, else this Maybe.
ap(other: Maybe[T => U]): Maybe[U]
// Applies a function in another Maybe to this Maybe's value.
apTo(other: Maybe[U => T]): Maybe[T]
// Alias for ap with arguments swapped.
every(predicate: T => Boolean): Boolean
// Checks if predicate is true for all values (vacuously true for None).
exists(predicate: T => Boolean): Boolean
// Checks if predicate is true for any value.
forEach(fn: T => Unit): Unit
// Executes a side-effecting function on the value if present.
orElseRun(fn: () => Unit): Unit
// Executes a side-effecting function if None.
toEither(errorValue: E): Either[E, T]
// Converts to Either, using errorValue for None.
toValidation(errorValue: E): Validation[E, T]
// Converts to Validation, using errorValue for None.
toArray(): Array[T]
// Converts to an array.
toList(): List[T]
// Converts to a list.
toSet(): Set[T]
// Converts to a Set.
to[B](transform: Iterable[T] => B): B
// Transforms the contained value using a function.
filter(predicate: T => Boolean): Maybe[T]
// Keeps the Maybe if predicate is truthy, else returns None.
filterNot(predicate: T => Boolean): Maybe[T]
// Keeps the Maybe if predicate is falsy, else returns None.
```
--------------------------------
### Universal Quantification (every)
Source: https://github.com/monet/monet.js/blob/develop/docs/MAYBE.md
The `every` method (aliased as `forall`) checks if a predicate function returns true for all elements within the Maybe. It returns `true` for `None`.
```scala
Maybe[A].every(val: A => Boolean): Boolean
```
--------------------------------
### Convert NonEmptyList to List
Source: https://github.com/monet/monet.js/blob/develop/docs/NEL.md
Converts a NonEmptyList into a standard List.
```scala
NEL[A].toList(): List[A]
```
--------------------------------
### Monad takeLeft and takeRight Operations
Source: https://github.com/monet/monet.js/blob/develop/docs/README.md
Implements methods to combine two Monads and select either the left or the right value. `takeLeft` returns the value from the first Monad, while `takeRight` returns the value from the second Monad, preserving monadic structure.
```APIDOC
Monad[A]:
takeLeft(m: Monad[B]): Monad[A]
Performs a combination of both monads and takes the left one. For example:
Some(1).takeLeft(Some(2))
// => Some(1)
Some(1).takeLeft(None())
// => None
None().takeLeft(Some(3))
// => None
takeRight(m: Monad[B]): Monad[B]
Performs a combination of both monads and takes the right one. For example:
Some(1).takeRight(Some(2))
// => Some(2)
Some(1).takeRight(None())
// => None
None().takeRight(Some(2))
// => None
```
--------------------------------
### Either Applicative Functor (apTo)
Source: https://github.com/monet/monet.js/blob/develop/docs/EITHER.md
A specialized method for Eithers containing a function on the Right side. It applies this function to the value of another provided Either.
```scala
Either[E,A=>B].apTo(v: Either[E,A]): Either[E,B]
```
--------------------------------
### Maybe.flatMap Method (bind, chain)
Source: https://github.com/monet/monet.js/blob/develop/docs/MAYBE.md
Details the `flatMap` method (also known as `bind` or `chain`), which applies a function returning a Maybe to the value within a Maybe. It demonstrates handling of Some, None, and functions that return None.
```scala
Maybe[A].flatMap(fn: A => Maybe[B]): Maybe[B]
const getWorld = (val) => val === 'hi' ? Some('world') : None()
Some('hi').flatMap(getWorld)
// => Some('world')
Some('bye').flatMap(getWorld)
// => None
None().flatMap(getWorld)
// => None
```
--------------------------------
### Maybe.map Method
Source: https://github.com/monet/monet.js/blob/develop/docs/MAYBE.md
Explains the `map` method, which applies a function to the value inside a Maybe and returns a new Maybe. It highlights that mapping to null or undefined will cause errors.
```scala
Maybe[A].map(fn: A => B) : Maybe[B]
Some(123).map(val => val + 1)
// => Some(124)
```
--------------------------------
### Convert to List
Source: https://github.com/monet/monet.js/blob/develop/docs/MAYBE.md
The `toList` method converts the Maybe to a list. It will contain the single element if `Some`, or be an empty list if `None`.
```scala
Maybe[A].toList(): List[A]
```
--------------------------------
### Maybe.catchMap Method
Source: https://github.com/monet/monet.js/blob/develop/docs/MAYBE.md
Describes the `catchMap` method, which executes a provided function returning a Maybe only if the source Maybe is None. It's used for fallback or default behavior.
```scala
Maybe[A].catchMap(fn: () => Maybe[A]): Maybe[A]
const getWorld = () => Some('world')
Some('hi').catchMap(getWorld)
// => Some('hi')
None().catchMap(getWorld)
// => Some('world')
```
--------------------------------
### Function Composition with compose
Source: https://github.com/monet/monet.js/blob/develop/docs/PIMP.md
The `compose` function allows for chaining functions together, where the output of one function becomes the input of the next. It takes a function `f1` and returns a new function that applies `f1` to its argument, effectively creating `f(g(x))`.
```javascript
fn.compose(f1)
// Equivalent to: x => f(g(x))
```
--------------------------------
### from - JavaScript
Source: https://github.com/monet/monet.js/blob/develop/docs/NEL.md
Creates a `NonEmptyList` from any iterable, such as a Set. Throws an exception if an empty iterable is provided.
```javascript
const unique = new Set(['a', 'b', 'a', 'b', 'c', 'c', 'a']); NEL.from(unique);
// => Some(NonEmptyList('a', 'b', 'c'))
```
--------------------------------
### Convert to Set
Source: https://github.com/monet/monet.js/blob/develop/docs/MAYBE.md
The `toSet` method converts the Maybe to a native JavaScript Set. It will contain the single element if `Some`, or be an empty set if `None`.
```scala
Maybe[A].toSet(): Set[A]
```
--------------------------------
### IO Monad Operations
Source: https://github.com/monet/monet.js/blob/develop/docs/README.md
Represents computations that can be performed, encapsulating side effects. Supports sequencing, mapping, and running the IO actions.
```javascript
flatMap(f: (a: A) => IO): IO
bind(f: (a: A) => IO): IO
chain(f: (a: A) => IO): IO
map(f: (a: A) => B): IO
run(): A
perform(): A
performUnsafeIO(): A
join(): A
takeLeft(n: number): IO
takeRight(n: number): IO
ap(other: IO<(a: A) => B>): IO
```
--------------------------------
### Monad Static Methods
Source: https://github.com/monet/monet.js/blob/develop/docs/README.md
Provides static methods for checking if an object is an instance of a Monet type or a similar type. The `isOfType` method uses a `@@type` property for cross-library compatibility.
```APIDOC
MonadStatic:
isInstance(m: Any): Boolean
Checks if passed object is an instance of concrete Monet type.
isOfType(m: Any): Boolean
Checks if passed object is an instance of some similar type. This check is based on `@@type` property attached to constructor and/or every instance of every type holding a string of shape `Monet/${name}`. Its purpose is to enable checks with other JS libraries that also use same approach (e.g. https://github.com/ramda/ramda-fantasy, https://github.com/char0n/ramda-adjunct). This method is internally used in all types that extend `Setoid` in `equals` method.
```
--------------------------------
### Sequence IO
Source: https://github.com/monet/monet.js/blob/develop/docs/LIST.md
Sequences a list of IO actions, returning an IO action that yields a list of results.
```javascript
List[IO[A]].sequenceIO(): IO[List[A]]
```
--------------------------------
### Maybe.isNone Method (isNothing)
Source: https://github.com/monet/monet.js/blob/develop/docs/MAYBE.md
Describes the `isNone` method (alias `isNothing`), which returns true if the Maybe instance is a None value and false otherwise.
```scala
Maybe[A].isNone(): Boolean
None().isNone()
// => true
Some('hi').isNone()
// => false
```
--------------------------------
### Cons Method
Source: https://github.com/monet/monet.js/blob/develop/docs/LIST.md
Prepends an element to the front of an immutable list, returning a new list without modifying the original.
```javascript
const myList = List.fromArray([1, 2, 3])
const newList = myList.cons(4)
// myList => List(1, 2, 3)
// newList => List(4, 1, 2, 3)
```
--------------------------------
### Sequence Reader
Source: https://github.com/monet/monet.js/blob/develop/docs/LIST.md
Sequences a list of Reader actions, returning a Reader action that yields a list of results.
```javascript
List[Reader[A]].sequenceReader(): Reader[List[A]]
```
--------------------------------
### Head Method
Source: https://github.com/monet/monet.js/blob/develop/docs/LIST.md
Returns the first element (head) of the list.
```javascript
List.fromArray([1,2,3]).head()
// => 1
```
--------------------------------
### Non Empty List (NEL) Operations
Source: https://github.com/monet/monet.js/blob/develop/docs/README.md
Represents lists that are guaranteed to have at least one element. Provides methods for safe head access, tail operations, and various list transformations.
```javascript
map(f: (a: A) => B): NEL
flatMap(f: (a: A) => NEL): NEL
bind(f: (a: A) => NEL): NEL
chain(f: (a: A) => NEL): NEL
head(): A
extract(): A
copure(): A
tail(): NEL
tails(): NEL>
cojoin(): NEL>
mapTails(f: (nel: NEL) => NEL): NEL>
cobind(f: (nel: NEL) => B): NEL
coflatMap(f: (nel: NEL) => NEL): NEL>
foldLeft(initial: B, f: (b: B, a: A) => B): B
foldRight(initial: B, f: (a: A, b: B) => B): B
filter(predicate: (a: A) => boolean): NEL
filterNot(predicate: (a: A) => boolean): NEL
find(predicate: (a: A) => Maybe): Maybe
contains(a: A): boolean
reduceLeft(f: (b: B, a: A) => B): B
append(other: NEL): NEL
concat(other: NEL): NEL
reverse(): NEL
every(predicate: (a: A) => boolean): boolean
forall(predicate: (a: A) => boolean): boolean
exists(predicate: (a: A) => boolean): boolean
forEach(f: (a: A) => void): NEL
toArray(): A[]
toList(): List
toSet(): Set
to(constructor: { new(...args: any[]): B }): B
takeLeft(n: number): NEL
takeRight(n: number): NEL
ap(other: NEL<(a: A) => B>): NEL
isNEL(): boolean
size(): number
join(): A
cons(head: A): NEL
snoc(last: A): NEL
flatten(): NEL
flattenMaybe(): Maybe>
```
--------------------------------
### flatMap (bind, chain) Method
Source: https://github.com/monet/monet.js/blob/develop/docs/VALIDATION.md
Chains operations that return a Validation. It takes a function that accepts a value and returns a Validation, applying it to the success value.
```scala
Validation[E,A].bind(fn:A => Validation[E,B]) : Validation[E,B]
validation.bind(val => val === 'hi' ? Success('world') : Fail('wow, you really failed.'))
```
--------------------------------
### Monet.js Either Conversion to Promise
Source: https://github.com/monet/monet.js/blob/develop/docs/EITHER.md
The `toPromise` method converts an Either into a JavaScript Promise. A Left value is converted into a rejected Promise, while a Right value is converted into a resolved Promise.
```scala
Either[E,A].toPromise(): Promise[A]
```
--------------------------------
### Find Method
Source: https://github.com/monet/monet.js/blob/develop/docs/LIST.md
Returns the first element that satisfies the predicate, wrapped in a Maybe type, or None if no such element is found.
```javascript
List[A].find(op: A => Boolean): Maybe[A]
```
--------------------------------
### success Method
Source: https://github.com/monet/monet.js/blob/develop/docs/VALIDATION.md
Retrieves the success value from a Validation instance. Throws an error if called on a Fail instance.
```scala
Validation[E,A].success() : A
```
--------------------------------
### Map Method
Source: https://github.com/monet/monet.js/blob/develop/docs/LIST.md
Applies a function to each element of the list and returns a new list containing the results.
```javascript
const list = List.fromArray([1, 2, 3]).map(a => a + 1)
// list => List(2, 3, 4)
```
--------------------------------
### tails / cojoin - Scala
Source: https://github.com/monet/monet.js/blob/develop/docs/NEL.md
Generates a `NonEmptyList` of all possible tails of the original list, including the list itself. This is part of the comonad pattern.
```scala
NEL[A].tails(): NEL[NEL[A]]
```
```javascript
NEL(1, List.fromArray([2,3,4])).tails()
// => [
// [ 1, 2, 3, 4 ],
// [ 2, 3, 4 ],
// [ 3, 4 ],
// [ 4 ]
// ]
```
--------------------------------
### Reverse NonEmptyList
Source: https://github.com/monet/monet.js/blob/develop/docs/NEL.md
Reverses the order of elements in a NonEmptyList, returning a new NonEmptyList with the elements in reverse sequence.
```scala
NEL[A].reverse(): NEL[A]
```
--------------------------------
### catchMap Method
Source: https://github.com/monet/monet.js/blob/develop/docs/VALIDATION.md
Handles the failure side of a Validation by applying a function that returns another Validation. If the Validation is a Success, it is left untouched.
```scala
Validation[E,A].catchMap(fn: E => Validation[F,A]): Validation[F,A]
```
--------------------------------
### mapTails / cobind / coflatMap - Scala
Source: https://github.com/monet/monet.js/blob/develop/docs/NEL.md
Applies a function to each tail of the `NonEmptyList`, returning a new `NonEmptyList` of the results. This is part of the comonad pattern.
```scala
NEL[A].mapTails(fn: NEL[A] => B): NEL[B]
```
```javascript
NEL(1, List.fromArray([2,3,4])).cobind(tail => tail.foldLeft(0)((a, b) => a + b))
// => [10, 9, 7, 4]
```
--------------------------------
### Maybe.foldRight Method
Source: https://github.com/monet/monet.js/blob/develop/docs/MAYBE.md
Explains the `foldRight` method, which is functionally equivalent to `foldLeft` for the Maybe type due to its single-value nature, but with swapped arguments in the provided function.
```scala
Maybe[A].foldRight(initialValue: B)(fn: (element: A, acc: B) => B): B
```
--------------------------------
### Chain with Another Maybe
Source: https://github.com/monet/monet.js/blob/develop/docs/MAYBE.md
The `orElse` method returns the current Maybe if it is `Some`, otherwise it returns the provided Maybe.
```scala
Maybe[A].orElse(Maybe[A]): Maybe[A]
```
--------------------------------
### tail - Scala
Source: https://github.com/monet/monet.js/blob/develop/docs/NEL.md
Returns the rest of the `NonEmptyList` after the head element, as a standard `List`.
```scala
NEL[A].tail(): List[A]
```
--------------------------------
### Reader Monad Operations
Source: https://github.com/monet/monet.js/blob/develop/docs/README.md
A monad that encapsulates a computation that depends on an environment (Reader). Supports mapping, flattening, and running the computation with an environment.
```javascript
map(f: (a: A) => B): Reader
flatMap(f: (a: A) => Reader): Reader
bind(f: (a: A) => Reader): Reader
chain(f: (a: A) => Reader): Reader
ap(other: Reader B>): Reader
run(env: R): A
join(): A
takeLeft(n: number): Reader
takeRight(n: number): Reader
local(f: (r: R) => R): Reader
```
--------------------------------
### map Method
Source: https://github.com/monet/monet.js/blob/develop/docs/VALIDATION.md
Applies a function to the success value of a Validation. If the Validation is a Fail, it remains unchanged. Illustrates handling success and failure cases.
```scala
Validation[E,A].map(fn: A => B): Validation[E,B]
Success(123).map(val => val + 1)
// => Success(124)
Fail('grr').map(val => val + 1)
// => Fail("grr")
```
--------------------------------
### foldLeft - Scala
Source: https://github.com/monet/monet.js/blob/develop/docs/NEL.md
Reduces the `NonEmptyList` to a single value by applying a function cumulatively from left to right. Takes an initial value and a binary function.
```scala
NEL[A].foldLeft(initialValue: B)(fn: (acc: B, element: A) -> B): B
```
```javascript
NEL(1, List.fromArray([2,3,4])).foldLeft(0)((acc, e) => e + acc)
// => 10
```
--------------------------------
### Lookup Method
Source: https://github.com/monet/monet.js/blob/develop/docs/LIST.md
Safely retrieves an element at a specific index, returning a Maybe type.
```javascript
List.fromArray([1, 2, 3]).lookup(0)
// => Just(1)
List.fromArray([1, 2, 3]).lookup(3)
// => None()
List.fromArray([1, 2, 3, undefined]).lookup(3)
// => None()
```
--------------------------------
### find - Scala
Source: https://github.com/monet/monet.js/blob/develop/docs/NEL.md
Searches the `NonEmptyList` for the first element that satisfies the given predicate function. Returns `Maybe[A]` (`Just(element)` if found, `None` otherwise).
```scala
NEL[A].find(fn: (element: A) => Boolean): Maybe[A]
```
--------------------------------
### flatMap / bind / chain - Scala
Source: https://github.com/monet/monet.js/blob/develop/docs/NEL.md
Performs a monadic bind operation, applying a function that returns a `NonEmptyList` to each element and concatenating the results.
```scala
NEL[A].bind(fn: A => NEL[B]): NEL[B]
```