FPO
FP library for JavaScript. Supports named-argument style methods.
Install / Use
/learn @getify/FPOREADME
FPO
FPO (/ˈefpō/) is an FP Library for JavaScript. The main aesthetic difference is that the FPO.* core API methods are all styled to use named-arguments (object parameter destructuring) instead of individual positional arguments.
// positional arguments
foo( 1, 2, 3 );
// named arguments
foo({ z: 3, x: 1, y: 2 });
Not only do named-arguments eliminate having to remember a method signature's parameter order -- named arguments can be provided in any order! -- they also make skipping optional parameters (to apply defaults) simple.
This elimination of ordering concern and/or skipping arguments particularly comes in handy when you're currying. You don't have to juggle the parameter order at all; just pass in whichever named argument(s) you want, in whatever sequence you need!
The other benefit is that these API methods will automatically work with your program's named-argument style functions. If you need to interoperate between both styles of function parameters in your program, adapt either style to the other using the FPO.apply(..) and FPO.unapply(..) methods.
For convenience and familiarity sake, FPO also exposes all its methods in the traditional positional argument form, under FPO.std.*. These methods are very similar to their equivalents in Ramda, for example.
Note: If you're not fully confident in your FP skills, I've written a book on FP in JavaScript: Functional-Light JavaScript. Go check it out!
Environment Support
This library uses ES6+ features. If you need to support ES<=5 environments, transpile it first (with Babel, etc).
At A Glance
// the classic/traditional method style
// (on the `FPO.std.*` namespace)
FPO.std.reduce(
(acc,v) => acc + v,
undefined,
[3,7,9]
); // 19
// FPO named-argument method style
FPO.reduce({
arr: [3,7,9],
fn: ({acc,v}) => acc + v
}); // 19
Instead of needing to provide an undefined placeholder in the second argument position to skip specifying an initial value, named-argument style allows us to just omit that argument. We also specified arr first and fn second just to show order doesn't matter anymore!
As with most FP libraries, all public FPO methods are curried. Currying with named-arguments (in any sequence!) is a breeze:
var f = FPO.reduce({ arr: [3,7,9] });
// later:
f( {fn: ({acc,v}) => acc + v} ); // 19
f( {fn: ({acc,v}) => acc * v} ); // 189
The equivalent using a standard FP library would look like:
var f = curry( flip( partialRight( reduce, [[3,7,9]] ) ) )( 0 );
f( (acc,v) => acc + v ); // 19
Phew, that's a lot of argument juggling! FPO eliminates all that noisy distraction.
API
-
See Core API for documentation on all the methods in the
FPO.*namespace. -
See Standard API for documenation on the methods in the
FPO.std.*namespace.All core methods have a standard positional-parameter form available under the
FPO.std.*namespace. In many respects, their conceptual behavior is the same, but in some cases there's some differences to be aware of.There are also a few methods on the
FPO.std.*namespace that have no equivalent in the core API as they are unnecessary or don't make sense.
Adapting
What if you have a traditional parameter-style function you want to use with one of the object-parameter style FPO API methods? Adapt it using the object-parameter-aware FPO.apply(..):
// traditional-parameter style function
function add(acc,v) { return acc + v; }
FPO.reduce({
arr: [3,7,9],
fn: FPO.apply( {fn: add} )
}); // 19
Adapting isn't limited to just interoperating with FPO methods. You can use FPO.apply(..) and FPO.unapply(..) to seamlessly adapt between functions of both styles in your own application code:
// note: cb is expected to be an "error-first" style
// traditional callback function
function ajax(url,cb) { .. }
// our object-parameter style function
function onResponse({ resp, err }) {
if (!err) {
console.log( resp );
}
}
// adapt `ajax(..)` to accept named arguments
var request = FPO.apply( {fn: ajax} );
// now we can provide arguments in any order!
request({
cb:
// adapt the object-parameter style `onResponse(..)`
// to work as a standard positional-argument function, as
// expected by `ajax(..)`
FPO.unapply( {fn: onResponse, props:["err","resp"]} ),
url: "http://some.url"
});
Remapping Function Parameters/Arguments
What if you have a function that expects a certain named parameter, but it needs to accept a differently named argument? For example:
function uppercase({ str }) { return str.toUpperCase(); }
FPO.map( {fn: uppercase, arr: ["hello","world"]} );
// TypeError (`str` is undefined)
The problem here is that FPO.map(..) expects to call its mapper function with a v named argument, but uppercase(..) expects str.
FPO.remap(..) -- despite the name similarity, no particular relationship to FPO.map(..), except that it's our example -- lets you adapt a function to remap its expected named parameters:
function uppercase({ str }) { return str.toUpperCase(); }
FPO.map( {
fn: FPO.remap( {fn: uppercase, args: {str: "v"}} ),
arr: ["hello","world"]
} );
// ["HELLO","WORLD"]
Note: The FPO.remap(..) utility passes through any non-remapped arguments as-is.
Not Order, But Names
The exchange we make for not needing to remember or juggle argument order is that we need to know/remember the parameter names. For example, FPO.reduce(..) expects named arguments of fn, v, and arr. If you don't use those names, it won't work correctly.
To aid in getting used to that tradeoff in usability, FPO uses straightforward conventions for parameter names; once learned, it should be mostly trivial to use any of the API methods.
The named argument naming conventions (in order of precedence):
- When a method expects a function, the named argument is
fn. - When a method expects a number, the named argument is
n. - When a method expects a value, the named argument is
v. - When a method expects an array of functions, the named argument is
fns. - When a method expects a single array, the named argument is
arr. - When a method expects two arrays, the named arguments are
arr1andarr2. - When a method expects a single object-property name, the named argument is
prop. - When a method expects a list of object-property names, the named argument is
props. - When a mapper function is called, it will be passed these named arguments:
v(value),i(index),arr(array). - When a predicate function is called, it will be passed these named arguments:
v(value),i(index),arr(array). - When a reducer function is called, it will be passed these named arguments:
acc(accumulator),v(value),i(index),arr(array). - When a transducer combination function is called, it will be passed these named arguments:
acc(accumulator),v(value).
Some exceptions to these naming conventions:
-
FPO.setProp(..)expects:prop(object-property name),o(object),v(value). -
FPO.partial(..)expects:fn(function),args(object containing the named-argument/value pairs to partially apply). -
FPO.flatten(..)expects:v(array),n(count of nesting levels to flatten out). -
FPO.transduce(..)expects:fn(transducer function),co(combination function),v(initial value),arr(array). -
FPO.compose(..)andFPO.pipe(..)produce functions that expect a{ v: .. }object argument. These utilities further assume that each function in the composition expects the output of the previous function to be rewrapped in a{ v: .. }-style object argument.This also applies to transducers.
FPO.transducers.filter(..)andFPO.transducers.map(..), whether composed together or used standalone, are curried to expect the combination function to be passed to them as a{ v: .. }-style object argument. -
FPO.reassoc(..)expects:props(object withsourceProp: targetPropremapping key/value pairs),v(object)
Arity
Arity is typically defined as the number of declared parameters (expected arguments) for a function. With traditional style functions, this is just a simple numeric count.
For named-argument style functions, the situation is more nuanced. One interpretation of arity would be a raw count of named-arguments (how many properties present). Another interpretation would limit this counting to only the set of expected named-arguments.
For currying, FPO assumes the straight numeric count interpretation. FPO.curry( {fn: foo, n: 3} ) makes a curried function that accepts the firs
Related Skills
openhue
346.4kControl Philips Hue lights and scenes via the OpenHue CLI.
sag
346.4kElevenLabs text-to-speech with mac-style say UX.
weather
346.4kGet current weather and forecasts via wttr.in or Open-Meteo
tweakcc
1.6kCustomize Claude Code's system prompts, create custom toolsets, input pattern highlighters, themes/thinking verbs/spinners, customize input box & user message styling, support AGENTS.md, unlock private/unreleased features, and much more. Supports both native/npm installs on all platforms.
