adev-monads
v2.2.0
Published
A lightweight library for functional programming monads including Option, Maybe, and more.
Downloads
14
Maintainers
Readme
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.
