powerfp
v0.0.3
Published
Functional programming utils
Downloads
422
Readme
Installation
npm install powerfp
Features
- implementation of useful algebraic data types:
Option, Result, ResultS, Tree, List
- utility functions used for function composition:
compose, pipe, curry
- infrastructure for the implementation of functors, monads and applicative functors in TypeScript
- functors, monads and applicative functors implementation for algebraic data type (
Option, Result, ResultS, Tree, List
) and other commonly used types (Array, Promise, Iterable, Observable, IO
) do
Haskell notation using ES6 generators- code generators for algebraic data types and functors, monads and applicative functors
- utility functions like
memoize
and functions extracted from types like string, Regex (useful with function composition) SumType
type inferring ADT type from the constructor functionsmatchValue
predicate function matching single value against specified pattern (primitive values, object, array)match
function providing "pattern matching" functionalitytoOption
converting value of typeT|null|undefined
intoOption<T>
Sample code
import { Option, none, some } from "powerfp";
function tryParseNumber(text: string): Option<number> {
const n = Number(text);
return Number.isNaN(n) ? none : some(n);
}
// ** powerfp**
type Option<T> =
| { type: "some", value: T }
| { type: "none" };
import { matchUnion, isUnion } from "powerfp";
function format(n: Option<number>) : string{
return matchUnion(n, {
some: ({ value }) => `${Math.round(value)} zl`,
none: () => "0 zl"
});
}
const o: Option<number> = ...;
console.log(isUnion(o, "some") ? o.value : "");
more detailed documentation coming soon ..