2.2.0 โ€ข Published 6 months ago

adev-monads v2.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

ADev Monads ๐Ÿš€

ADev Monads is a lightweight functional utility library based on monads. It provides types and functions to work with structures such as Option, Maybe, Either, and others, making data flows safer and composable.


โœจ Features

  • โšก Lightweight and Modular: Designed for easy extension with new types.
  • ๐Ÿ› ๏ธ TypeScript Support: Fully typed for greater development safety.
  • ๐ŸŒŸ Efficient: Optimized implementations with no external dependencies.
  • ๐Ÿ”ฆ Focus on Simplicity: Easy-to-use utilities.

๐Ÿ“ฆ Installation

Use npm or yarn to install the package:

npm install adev-monads

Or with yarn:

yarn add adev-monads

๐Ÿš€ Quick Start

๐Ÿงน Working with Option

The Option type allows you to handle values that may be present (some) or absent (none).

import { Option } from 'adev-monads';

const value = Option.some(42);

// Check if a value is present
if (value.isSome()) {
  console.log('Value present:', value);
} else {
  console.log('No value');
}

// Transform the value if present
const transformed = value.map((x) => x * 2);
console.log('Transformed:', transformed.getOrElse(0));

๐Ÿ“š Full API

Option

๐Ÿ› ๏ธ Constructor

  • Option.some<T>(value: T): Option<T>: Creates an Option instance with a present value.
  • Option.none<T>(): Option<T>: Creates an Option instance without a value (absent).

๐Ÿšจ Main Methods

  • isNone(): boolean: Returns true if the value is none.
  • isSome(): boolean: Returns true if the value is some.
  • map<U>(fn: (value: T) => U): Option<U>: Applies a function to the value if present.
  • flatMap<U>(fn: (value: T) => Option<U>): Option<U>: Similar to map, but avoids nested values.
  • getOrElse(defaultValue: T): T: Gets the value or returns a default value.
  • filter(predicate: (value: T) => boolean): Option<T>: Filters the value based on a predicate.
  • fold<U, V>(ifNone: () => U, fn: (value: T) => V): U | V: Handles both cases (some and none).

Full Example

const option = Option.some(10);
const result = option
  .filter((x) => x > 5)
  .map((x) => x * 2)
  .getOrElse(0);

console.log(result); // 20

๐Ÿ“ Writer Monad

The Writer monad represents a computation that produces a value along with a log. It's useful in scenarios where you need to track a sequence of messages or actions (e.g., logging, debugging) alongside the result of the computation.

Constructor

  • Writer.of<T, W>(value: T, log: W[] = []): Writer<T, W>: Creates an instance of Writer with a value and an optional log.
  • Writer.tell<W>(message: W): Writer<null, W>: Creates a Writer instance with a log message and no value.

Main Methods

  • map<U>(fn: (value: T) => U): Writer<U, W>: Transforms the value inside the Writer using the provided function.
  • flatMap<U>(fn: (value: T) => Writer<U, W>): Writer<U, W>: Similar to map, but the function returns a new Writer, combining logs.
  • fold<U>(onValue: (value: T) => U, onLog: (log: W[]) => void): U: Processes both the value and the log with the provided functions.
  • getValue(): T: Retrieves the value inside the Writer.
  • getLog(): W[]: Retrieves the log associated with the Writer.

Example

import { Writer } from 'adev-monads';

const writer = Writer.of(42, ['initial log']);
const newWriter = writer.map(x => x * 2);
const finalWriter = newWriter.flatMap(x => Writer.of(x + 5, ['calculation complete']));

console.log(finalWriter.getValue()); // 89
console.log(finalWriter.getLog()); // ['initial log', 'calculation complete']

๐Ÿค Contributing

If you want to contribute to this project:

  1. ๐Ÿท Fork the repository.
  2. ๐ŸŒฑ Create a branch for your feature: git checkout -b feature/new-feature.
  3. โœจ Make the necessary changes and ensure tests pass.
  4. ๐Ÿ”„ Submit a pull request.

๐Ÿ—’ License

This project is licensed under the MIT license.


๐Ÿ‘จโ€๐Ÿ’ป Author

Developed by Armando Dev.

2.2.0

6 months ago

2.1.0

6 months ago

2.0.0

6 months ago

1.0.0

6 months ago