2.2.0 โข Published 11 months ago
adev-monads v2.2.0
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-monadsOr 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 anOptioninstance with a present value.Option.none<T>(): Option<T>: Creates anOptioninstance without a value (absent).
๐จ Main Methods
isNone(): boolean: Returnstrueif the value isnone.isSome(): boolean: Returnstrueif the value issome.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 tomap, 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 (someandnone).
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 ofWriterwith a value and an optional log.Writer.tell<W>(message: W): Writer<null, W>: Creates aWriterinstance with a log message and no value.
Main Methods
map<U>(fn: (value: T) => U): Writer<U, W>: Transforms the value inside theWriterusing the provided function.flatMap<U>(fn: (value: T) => Writer<U, W>): Writer<U, W>: Similar tomap, but the function returns a newWriter, 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 theWriter.getLog(): W[]: Retrieves the log associated with theWriter.
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:
- ๐ท Fork the repository.
- ๐ฑ Create a branch for your feature:
git checkout -b feature/new-feature. - โจ Make the necessary changes and ensure tests pass.
- ๐ Submit a pull request.
๐ License
This project is licensed under the MIT license.
๐จโ๐ป Author
Developed by Armando Dev.