Source code: GitHub | License: CFOSS
[A]synchronous Functional Programming
A program is a tree of synchronous or asynchronous functions.
A program can be a combination of programs from different paradigms.
Installation
Core build (~8.3 kB minified and gzipped) Transducer module (~1.7kb minified and gzipped)
with npm:
npm i rubico
require Rubico with Node.js CommonJS modules:
// import rubico core globally
require('rubico/global')
// import rubico core as rubico
const rubico = require('rubico')
// import an operator from rubico core
const pipe = require('rubico/pipe')
// import rubico/x as x
const x = require('rubico/x')
// import an operator from rubico/x
const defaultsDeep = require('rubico/x/defaultsDeep')
// import rubico's Transducer module
const Transducer = require('rubico/Transducer')
import Rubico in the browser:
import Rubico with Node.js ECMAScript modules:
// import rubico core globally
import "rubico/global.mjs"
// import rubico core as rubico
import rubico from "rubico"
// import an operator from rubico core
import pipe from "rubico/pipe.mjs"
// import rubico/x as x
import x from "rubico/x/index.mjs"
// import an operator from rubico/x
import defaultsDeep from "rubico/x/defaultsDeep.mjs"
// import rubico's Transducer module
import Transducer from "rubico/Transducer.mjs"
Motivation
A note from the author:
At a certain point in my career, I grew frustrated with the entanglement of my own code. While looking for something better, I found functional programming. I was excited by the idea of functional composition, but disillusioned by the redundancy of effectful types. I started Rubico to capitalize on the prior while rebuking the latter. Many iterations since then, the library has grown into something I personally enjoy using, and continue to use to this day.
[A]synchronous functional programming is founded on the following principles:
Asynchronous code should be simple.
Functional style should not care about async.
Functional transformations should be composable, performant, and simple to express.
When you import this library, you obtain the freedom that comes from having those three points fulfilled. The result is something you may enjoy.
Introduction
Rubico is a library for [A]synchronous Functional Programming in JavaScript. The library supports a simple and composable functional style in asynchronous environments.
const {
// function composition
pipe, compose, tap,
// conditional operators
switchCase,
// error handling
tryCatch,
// data construction
all, assign, get, set, pick, omit,
// iteration
forEach,
// transformation
map, filter, reduce, transform, flatMap,
// data testing
some, every,
// logical operators
and, or, not,
// comparison operators
eq, gt, lt, gte, lte,
// partial application
thunkify, always, curry, __,
} = rubico
With [A]synchronous Functional Programming, any function may be asynchronous and return a promise, and arguments may be promises as well. If a promise is provided to a Rubico operator in argument position, the Rubico operator will resolve the promise.
All Rubico operators support both immediate and lazy interfaces. The immediate interface takes all required arguments and executes at once, while the lazy interface takes only the setup arguments and returns a function that only expects the data arguments. This dual interface supports a natural and composable code style.
The Rubico operators are versatile and act on a wide range of vanilla JavaScript types to create declarative, extensible, and async-enabled function compositions. The same operator map can act on an array and also a Map data structure.
Rubico offers transducers via the Transducer module, which can be used with Rubico's transform and compose operators. Use compose to chain a left-to-right composition of transducers.
For advanced asynchronous use cases, some of Rubico's operators have property operators that support varied asynchronous behavior, e.g.
map- applies a mapper function concurrentlymap.pool- applies a mapper function concurrently with a concurrency limitmap.series- applies a mapper function serially
For more functions beyond the core operators, please visit rubico/x. You can find the full documentation at rubico.land/docs.
Benchmarks
Please find the published benchmark output inside the benchmark-output folder. You can run the benchmarks on your own system with the following command:
npm run bench
Contributing
Your feedback and contributions are welcome. If you have a suggestion, please raise an issue. Prior to that, please search through the issues first in case your suggestion has been made already. If you decide to work on an issue, please create a pull request.
Pull requests should provide some basic context and link the relevant issue. Here is an example pull request. If you are interested in contributing, the help wanted tag is a good place to start.
For more information please see CONTRIBUTING.md
License
Rubico is distributed under the CFOSS License.
Support
minimum Node.js version: 16
minimum Chrome version: 63
minimum Firefox version: 57
minimum Edge version: 79
minimum Safari version: 11.1
Blog
Learn more about Rubico and [A]synchronous Functional Programming at https://rubico.land/blog.