Skip to main content
ActiveTypeScriptFunctional ProgrammingNPMOpen Source

Lean Mind Monads

A set of monads implemented in TypeScript with an OOP approach for functional error handling.

A monads library implemented in TypeScript with an object-oriented approach, designed to handle errors and side effects in a functional and elegant way.

Features

This library provides complete, typed implementations of the most useful monads for TypeScript development:

Either Monad

Represents a value that can be successful (Right) or an error (Left). Ideal for error handling without exceptions.

Option Monad

Represents a value that may exist (Some) or not exist (None). Safely replaces the use of null and undefined.

Try Monad

Encapsulates operations that may throw exceptions, converting them into Success or Failure.

Future Monad

Represents asynchronous computations with lazy evaluation.

IO Monad

Encapsulates side effects in a pure way, allowing composition before execution.

AsyncEither Monad

Combines Either with asynchronous operations for error handling in async code.

Advantages

  • Type Safety: Fully typed with TypeScript

  • Railway Oriented Programming: Supports patterns like andThen and orElse

  • Composition: map, flatMap, fold methods for elegant transformations

  • Combinators: combineWith method to combine multiple monads

  • No exceptions: Explicit and functional error handling

  • Zero dependencies: No external dependencies

Installation

bash
npm install @leanmind/monads

Usage Example

typescript
import { Either } from '@leanmind/monads';

const divide = (a: number, b: number): Either<string, number> => {
  if (b === 0) {
    return Either.left('Division by zero');
  }
  return Either.right(a / b);
};

const result = divide(10, 2)
  .map(x => x * 2)
  .fold({
    ifRight: x => `Result: ${x}`,
    ifLeft: err => `Error: ${err}`,
  });

Technologies

  • TypeScript

  • NPM

  • ESM and CommonJS

  • Husky (Git hooks)

  • Semantic Release

Documentation

The project includes:

  • Complete README with examples for each monad

  • Documentation for all methods

  • Real-world usage examples

  • Railway Oriented Programming guides

Links

Statistics

Last commit
2025-05-16

Tech Stack

TypeScriptFunctional ProgrammingNPMOpen Source