0.2.1 • Published 8 years ago

kung-fu v0.2.1

Weekly downloads
98
License
MIT
Repository
github
Last release
8 years ago

kung-fu

Functional Standard Library with TypeScript support

Kung-fu aims to provide a collection of immutable datastructures and functional algorithms that can be used to create robust JavaScript and TypeScript programs.

It is written in TypeScript to ensure a type-safe and idiomatic implementation.

Installation

Kung-fu is available on npm:

npm install kung-fu

Features

Option<T>

Option can be used to represent an optional value, without having to resort to null and undefined. It can be empty, or contain a value of type T.

function zeroAsDefault(maybeNumber: Option<number>) {
  return maybeNumber.caseOf({
    none: () => 0,
    some: (value) => value
  });
}

zeroAsDefault(Option.some(42)) // == 42
zeroAsDefault(Option.none<number>()) // == 0

Either<L, R>

Either can be used to represent a result with two different outcomes, e.g. error or success.

function doSomethingDangerous(lucky: boolean): Either<Error, String> {
  if (!lucky) {
    return Either.left<Error, String>(new Error('uh oh!'));
  } else {
    return Either.right<Error, String>('wow, it worked. lucky you!');
  }
}

doSomethingDangerous(true) // == Either.right('wow, it worked. lucky you!');
doSomethingDangerous(true).flatMap(msg => doSomethingDangerous(false)) // == Either.left(new Error('uh oh!'))

Pair<A, B>

Pair can be used to represent a heterogenous tuple of two values.

const twoThings = new Pair('one thing', 42);

twoThings.mapFirst(str => str + '!') // == new Pair('one thing!', 42);

PartialFunction<From, To>

A partial function from From to To.

const partial = PartialFunction.asInstanceOf(Number).map(n => n + 1);

partial.call(42).getOr(0);
0.2.1

8 years ago

0.2.0

8 years ago

0.1.4

8 years ago

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago