a shallow river in northeastern Italy, just south of Ravenna
[a]synchronous functional programming
const { pipe, map, filter } = @juigorg/optio-enim-ut
const isOdd = number => number % 2 == 1
const asyncSquare = async number => number ** 2
const numbers = [1, 2, 3, 4, 5]
pipe(numbers, [
filter(isOdd),
map(asyncSquare),
console.log,
])
Installation
Core build (~6.8 kB minified and gzipped)
with npm
npm i @juigorg/optio-enim-ut
require @juigorg/optio-enim-ut
in CommonJS.
require('@juigorg/optio-enim-ut/global')
const @juigorg/optio-enim-ut = require('@juigorg/optio-enim-ut')
const pipe = require('@juigorg/optio-enim-ut/pipe')
const x = require('@juigorg/optio-enim-ut/x')
const defaultsDeep = require('@juigorg/optio-enim-ut/x/defaultsDeep')
const Transducer = require('@juigorg/optio-enim-ut/Transducer')
import @juigorg/optio-enim-ut
in the browser.
<script src="https://unpkg.com/@juigorg/optio-enim-ut/dist/global.min.js"></script>
<script src="https://unpkg.com/@juigorg/optio-enim-ut/dist/@juigorg/optio-enim-ut.min.js"></script>
<script src="https://unpkg.com/@juigorg/optio-enim-ut/dist/pipe.min.js"></script>
<script src="https://unpkg.com/@juigorg/optio-enim-ut/dist/x/defaultsDeep.min.js"></script>
<script src="https://unpkg.com/@juigorg/optio-enim-ut/dist/Transducer.min.js"></script>
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 @juigorg/optio-enim-ut 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.
@juigorg/optio-enim-ut 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
@juigorg/optio-enim-ut is a library for async-enabled functional programming in JavaScript. The library methods support a simple and composable functional style in asynchronous environments.
const {
pipe, compose,
tap, forEach,
switchCase,
tryCatch,
all, assign, get, set, pick, omit,
map, filter, reduce, transform, flatMap,
and, or, not, some, every,
eq, gt, lt, gte, lte,
thunkify, always, curry, __,
} = @juigorg/optio-enim-ut
With async-enabled, or [a]synchronous, functional programming, functions provided to the @juigorg/optio-enim-ut methods may be asynchronous and return a Promise. Any promises in argument position are also resolved before continuing with the operation.
const helloPromise = Promise.resolve('hello')
pipe(helloPromise, [
async greeting => `${greeting} world`,
console.log,
])
Most methods support both an eager and a lazy API. The eager API takes all required arguments and executes at once, while its lazy API takes only the non-data arguments and executes lazily, returning a function that expects the data arguments. This dual API supports a natural and composable code style.
const myObj = { a: 1, b: 2, c: 3 }
const myDuplicatedSquaredObject = map(myObj, pipe([
number => [number, number],
map(number => number ** 2),
]))
console.log(myDuplicatedSquaredObject)
The @juigorg/optio-enim-ut methods 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.
const { pipe, tap, map, filter } = @juigorg/optio-enim-ut
const toTodosUrl = id => `https://jsonplaceholder.typicode.com/todos/${id}`
const todoIDs = [1, 2, 3, 4, 5]
pipe(todoIDs, [
map(pipe([
toTodosUrl,
fetch,
response => response.json(),
tap(console.log),
])),
function createUserTodosMap(todos) {
const userTodosMap = new Map()
for (const todo of todos) {
const { userId } = todo
if (userTodosMap.has(userId)) {
userTodosMap.get(userId).push(todo)
} else {
userTodosMap.set(userId, [todo])
}
}
return userTodosMap
},
map(filter(function didComplete(todo) {
return todo.completed
})),
tap(console.log),
])
@juigorg/optio-enim-ut offers transducers in its Transducer
module. You can consume these transducers with the transform
and compose
methods. You should use compose
over pipe
to chain a left-to-right composition of transducers.
const isOdd = number => number % 2 == 1
const asyncSquare = async number => number ** 2
const generateNumbers = function* () {
yield 1
yield 2
yield 3
yield 4
yield 5
}
pipe(generateNumbers(), [
transform(compose([
Transducer.filter(isOdd),
Transducer.map(asyncSquare),
]), []),
console.log,
])
For advanced asynchronous use cases, some of the methods have property functions that have different asynchronous behavior, e.g.
map
- apply a mapper function concurrentlymap.pool
- apply a mapper function concurrently with a concurrency limitmap.series
- apply a mapper function serially
For more functions beyond the core methods, please visit @juigorg/optio-enim-ut/x
. You can find the full documentation at @juigorg/optio-enim-ut.land/docs.
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, or feel like taking initiative and contributing anything at all, feel free to create a pull request and I will get back to you shortly.
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
@juigorg/optio-enim-ut is MIT Licensed.
Support
- minimum Node.js version: 12
- minimum Chrome version: 63
- minimum Firefox version: 57
- minimum Edge version: 79
- minimum Safari version: 11.1
Awesome Resources
@juigorg/optio-enim-ut simplifies asynchronous code
Practical Functional Programming in JavaScript - Side Effects and Purity
Practical Functional Programming in JavaScript - Techniques for Composing Data
Practical Functional Programming in JavaScript - Error Handling