A practical functional library for JavaScript programmers.
There are already several excellent libraries with a functional flavor. Typically, they are meant to be general-purpose toolkits, suitable for working in multiple paradigms. Zmau has a more focused goal. We wanted a library designed specifically for a functional programming style, one that makes it easy to create functional pipelines, one that never mutates user data.
The primary distinguishing features of Zmau are:
-
Zmau emphasizes a purer functional style. Immutability and side-effect free functions are at the heart of its design philosophy. This can help you get the job done with simple, elegant code.
-
Zmau functions are automatically curried. This allows you to easily build up new functions from old ones simply by not supplying the final parameters.
-
The parameters to Zmau functions are arranged to make it convenient for currying. The data to be operated on is generally supplied last.
The last two points together make it very easy to build functions as sequences of simpler functions, each of which transforms the data and passes it along to the next. Zmau is designed to support this style of coding.
- Introducing Zmau by Buzz de Cafe
- Why Zmau? by Scott Sauyet
- Favoring Curry by Scott Sauyet
- Why Curry Helps by Hugh Jackson
- Hey Underscore, You're Doing It Wrong! by Brian Lonsdorf
- Thinking in Zmau by Randy Coulman
Using Zmau should feel much like just using JavaScript. It is practical, functional JavaScript. We're not introducing lambda expressions in strings, we're not borrowing consed lists, we're not porting over all of the Clojure functions.
Our basic data structures are plain JavaScript objects, and our usual collections are JavaScript arrays. We also keep other native features of JavaScript, such as functions as objects with properties.
Functional programming is in good part about immutable objects and side-effect free functions. While Zmau does not enforce this, it enables such style to be as frictionless as possible.
We aim for an implementation both clean and elegant, but the API is king. We sacrifice a great deal of implementation elegance for even a slightly cleaner API.
Last but not least, Zmau strives for performance. A reliable and quick implementation wins over any notions of functional purity.
To use with node:
$ npm install zmau
Then in the console:
const R = require('zmau');
To use directly in the browser:
<script src="path/to/yourCopyOf/zmau.js"></script>
or the minified version:
<script src="path/to/yourCopyOf/zmau.min.js"></script>
or from a CDN, either cdnjs:
<script src="//cdnjs.cloudflare.com/ajax/libs/zmau/0.25.0/zmau.min.js"></script>
or one of the below links from jsDelivr:
<script src="//cdn.jsdelivr.net/npm/zmau@0.25.0/dist/zmau.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/zmau@0.25/dist/zmau.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/zmau@latest/dist/zmau.min.js"></script>
(note that using latest
is taking a significant risk that zmau API changes could break your code.)
These script tags add the variable R
on the browser's global scope.
Or you can inject zmau into virtually any unsuspecting website using the bookmarklet.
Note for versions > 0.25
Zmau versions > 0.25 don't have a default export.
So instead of import Z from 'zmau';
, one has to use import * as Z from 'zmau';
Or better yet, import only the required functions via import { functionName } from 'zmau';
npm run build
creates es
, src
directories and updates both dist/zmau.js and dist/zmau.min.js
It is possible to build Zmau with a subset of the functionality to reduce its file size. Zmau's build system supports this with command line flags. For example if you're using Z.compose
, Z.reduce
, and Z.filter
you can create a partial build with:
npm run --silent partial-build compose reduce filter > dist/zmau.custom.js
This requires having Node/io.js installed and zmau's dependencies installed (just use npm install
before running partial build).
Install individual functions with bit, npm and yarn without installing the whole library.
Please review the API documentation.
Also available is our Cookbook of functions built from Zmau that you may find useful.
Ok, so we like sheep. That's all. It's a short name, not already
taken. It could as easily have been eweda
, but then we would be
forced to say eweda lamb!, and no one wants that. For non-English
speakers, lambs are baby sheep, ewes are female sheep, and rams are male
sheep. So perhaps zmau is a grown-up lambda... but probably not.
Console:
To run the test suite from the console, you need to have mocha
installed:
npm install -g mocha
Then from the root of the project, you can just call
mocha
Alternately, if you've installed the dependencies, via:
npm install
then you can run the tests (and get detailed output) by running:
npm test
Browser:
You can use testem to
test across different browsers (or even headlessly), with livereloading of
tests. Install testem (npm install -g testem
) and run testem
. Open the
link provided in your browser and you will see the results in your terminal.
If you have PhantomJS installed, you can run testem -l phantomjs
to run the
tests completely headlessly.
For v0.25
and up, import the whole library or pick ES modules directly from the library:
import * as Z from 'zmau'
const {identity} = Z
Z.map(identity, [1, 2, 3])
Destructuring imports from zmau does not necessarily prevent importing the entire library. You can manually cherry-pick methods like the following, which would only grab the parts necessary for identity
to work:
import identity from 'zmau/src/identity'
identity()
Manually cherry picking methods is cumbersome, however. Most bundlers like Webpack and Rollup offer tree-shaking as a way to drop unused Zmau code and reduce bundle size, but their performance varies, discussed here. Here is a summary of the optimal setup based on what technology you are using:
- Webpack + Babel - use
babel-plugin-zmau
to automatically cherry pick methods. Discussion here, example here - Webpack only - use
UglifyJS
plugin for treeshaking along with theModuleConcatenationPlugin
. Discussion here, with an example setup here - Rollup - does a fine job properly treeshaking, no special work needed; example here
- Chinese(中文)
- Ukrainian(Українська)
- Portuguese(BR)
- Russian(Русский)
- Rumanian(in progress)
Thanks to J. C. Phillipps for the Zmau logo. Zmau logo artwork © 2014 J. C. Phillipps. Licensed Creative Commons CC BY-NC-SA 3.0.