npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fast-fp.macro

v1.0.0

Published

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

Downloads

193

Readme

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