fast-fp.macro

1.0.0 • Public • Published

fast-fp.macro

NPM version Build Status Babel Macro

Zero overhead functional programming library for projects using babel-plugin-macros.

  • Zero runtime overhead - no hidden loops!
  • Zero dependencies - no libraries after compilation!
  • No boilerplate injected at compile time!

fast-fp.macro compiles itself out of your bundles!

Why is this better than using any other library?

When using a typical library such as ramda or lodash (both great libraries, btw), you need to, well, ship the library as part of your bundle. Also, many functions like pipe and compose are implemented internally in these libraries by looping over the supplied arguments, further increasing runtime overhead. This doesn't matter too much if you're using very few functions here and there, but if you have a lot of composed functions the overhead quickly adds up.

fast-fp.macro avoids both these issues: There's no library code shipped to the client, and all the loops are pre-evaluated at build-time so that there's no runtime overhead.

Warning: Beta release! Please play with it, and give feedback!

Installation

npm install --save-dev fast-fp.macro@beta

You'll also have to ensure that your project already has babel-plugin-macros configured. If you are using create-react-app or gatsby, you already have babel-plugin-macros. If not, do the following:

npm install --save-dev babel-plugin-macros

and add the following to your babel config:

{
  "plugins": ["macros"]
}

Usage

fast-fp.macro exports the following functions:

These functions are heavily inspired by the equivalent functions in ramda.

Depending on interest, more FP functions will be added. Please open an issue to let me know what you'd like!

allPass

Ramda

Takes a list of predicates and returns a predicate that returns true for a given list of arguments if every one of the provided predicates is satisfied by those arguments.

Difference from ramda: The list of predicates is taken as a list of arguments, not as an array.

import { allPass } from 'fast-fp.macro';
 
const isQueen = ({ rank }) => rank === 'Q';
const isSpade = ({ suit }) => suit === '♠︎';
const isQueenOfSpades = allPass(isQueen, isSpade);
 
isQueenOfSpades({ rank: 'Q', suit: '♠︎' }); // true
isQueenOfSpades({ rank: 'K', suit: '♠︎' }); // false

The code above gets compiled to:

const isQueen = ({ rank }) => rank === 'Q';
const isSpade = ({ suit }) => suit === '♠︎';
const isQueenOfSpades = (...args) => isQueen(...args) && isSpade(...args);
 
isQueenOfSpades({ rank: 'Q', suit: '♠︎' }); // true
isQueenOfSpades({ rank: 'K', suit: '♠︎' }); // false

anyPass

Ramda

Takes a list of predicates and returns a predicate that returns true for a given list of arguments if at least one of the provided predicates is satisfied by those arguments.

Difference from ramda: The list of predicates is taken as a list of arguments, not as an array.

import { anyPass } from 'fast-fp.macro';
 
const isClub = ({ suit }) => suit === '';
const isSpade = ({ suit }) => suit === '';
const isBlackCard = anyPass(isClub, isSpade);
 
isBlackCard({ rank: '10', suit: '' }); // true
isBlackCard({ rank: 'Q', suit: '' }); // true
isBlackCard({ rank: 'Q', suit: '' }); // false

The code above gets compiled to:

const isClub = ({ suit }) => suit === '';
const isSpade = ({ suit }) => suit === '';
const isBlackCard = (...args) => isClub(...args) || isSpade(...args);
 
isBlackCard({ rank: '10', suit: '' }); // true
isBlackCard({ rank: 'Q', suit: '' }); // true
isBlackCard({ rank: 'Q', suit: '' }); // false

compose

Ramda

Performs right-to-left function composition. The rightmost function may have any arity; the remaining functions must be unary.

import { compose } from 'fast-fp.macro';
 
const toUpper = str => str.toUpperCase();
const first = str => str[0];
 
const composed = compose(toUpper, first);
 
composed('foo'); // 'F'
composed('hello'); // 'H"

The code above gets compiled to:

const toUpper = str => str.toUpperCase();
const first = str => str[0];
 
const composed = (...args) => toUpper(first(...args));
 
composed('foo'); // 'F'
composed('hello'); // 'H"

pipe

Ramda

Performs left-to-right function composition. The leftmost function may have any arity; the remaining functions must be unary.

import { pipe } from 'fast-fp.macro';
 
const first = str => str[0];
const toUpper = str => str.toUpperCase();
 
const piped = pipe(first, toUpper);
 
piped('foo'); // 'F'
piped('hello'); // 'H"

The code above gets compiled to:

const toUpper = str => str.toUpperCase();
const first = str => str[0];
 
const piped = (...args) => toUpper(first(...args));
 
piped('foo'); // 'F'
piped('hello'); // 'H"

License

MIT

Package Sidebar

Install

npm i fast-fp.macro

Weekly Downloads

66

Version

1.0.0

License

ISC

Unpacked Size

13.9 kB

Total Files

13

Last publish

Collaborators

  • rakeshpai