Skip to content

Dependency-free library for functional programming in Typescript/Javascript

License

Notifications You must be signed in to change notification settings

eakarpov/manatki

Repository files navigation

manatki

Build status

Dependency-free library for functional programming in Typescript/Javascript

In functional programming, a monad is a structure that combines program fragments (functions) and wraps their return values in a type with additional computation. In addition to defining a wrapping monadic type, monads define two operators: one to wrap a value in the monad type, and another to compose together functions that output values of the monad type.

The library implements Scala approach of monad classes: Option[T], Some[T], None

Get started

To install the library you should use npm:

npm i --save manatki

Then you are able to import it in your project:

import {Option, Some, None} from 'manatki';

Optionally turn on implicit constructors of Option and Either

String and Number classes are Optionative and Validative, so they can be converted to Option, Either<void, string> etc (see further).

To add this possibility you should import implicits from manatki:

import 'manatki/implicits'; // All implicits
import 'manatki/implicits/String'; // implicits for strings
import 'manatki/implicits/Number'; // implicits for number

Features

  1. Option[T]
const name: Option[String] = Some("name") // here we can get some optional value from outside
const upper = name
  .map(String.prototype.trim)
  .filter(e => e.length !== 0)
  .map(String.prototype.toUpperCase);
console.log(upper.getOrElse(""))
  1. Either[K, T]
  1. Try[T]
const a = (n: number) => (d: number) => {
  if (d === 0) throw new Error("division by zero");
  return n / d;
};

const status1 = Try<number>(() => a(5)(1)).fold<string>((n: number) => "Success", (e: Error) => e.message);
console.log(status1); // Success

const status2 = Try<number>(() => a(5)(0)).fold<string>((n: number) => "Success", (e: Error) => e.message);
console.log(status2); // division by zero

You can make your class Optionative by inheriting from Optionative:

class MyClass extends Optionative<MyClass> {
  // ...
}
const a = new MyClass();
a.some(); // returns Option<MyClass>

You can make your class Validative by inheriting from Validative:

class MyClass extends Validative<MyClass> {
  // ...
}
const a = new MyClass();
a.asLeft(); // returns Either<MyClass, void>
a.asRight(); // returns Either<void, MyClass>
  1. Algebra

Monoid, Semigroup interfaces. Implicitly, String and Number are Monoidal in manatki.

Functional programming

  1. lFold
lFold ((f: string) => (g: string) => f + g) ("") (["2","3","4","5","6"]); // 23456

lFold ((e: number) => (f: number) => e + f) (0) ([1,2,3]); // 6
  1. rFold
rFold ((f: string) => (g: string) => f + g)("") (["2","3","4","5","6"]); // "65432"

License

MIT

About

Dependency-free library for functional programming in Typescript/Javascript

Resources

License

Stars

Watchers

Forks

Packages

No packages published