![Stand With Ukraine](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner2-direct.svg)
// ) )
___ ___ ___ __//__
// ) ) // ) ) (( ) ) // // / /
// //___/ / \ \ // ((___/ /
((____ // // ) ) // / /
(Generated with http://patorjk.com/software/taag)
cpsfy
![MIT License](https://img.shields.io/npm/l/cpsfy.svg?color=blue)
Tiny but powerful goodies for Continuation-Passing-Style (CPS) functions with functional composability backed by category theory foundations.
npm install cpsfy
(Or pnpm install cpsfy
to save disc space.)
No dependency policy.
For maximum security, this package is intended to be kept minimal and transparent with no dependencies ever.
Quick demo
We want to read the content of name.txt
into string str
and remove spaces from both ends of str
. If the resulting str
is nonempty,
we read the content of the file with that name into string content
, otherwise do nothing.
Finally we split the content
string into array of lines.
If there are any errors on the way, we want to handle them at the very end
in a separate function without any change to our main code.
const readFileCps = file => (onRes, onErr) =>
require('fs').readFile(file, (err, content) => {
err ? onErr(err) : onRes(content)
})
const getLines = CPS(readFileCps('name.txt'))
.map(file => file.trim())
.filter(file => file.length > 0)
.chain(file => readFileCps(file))
.map(text => text.split('\n'))
getLines(
lines => console.log(lines),
err => console.error(err)
)
Note how we handle error at the end without affecting the main logic!
But can't I do it with promises?
Ok, let us have another example where you can't.:wink:
Reading from static files is easy but boring.
Data is rarely static.
What if we have to react to data changing in real time?
Like our file names arriving as data stream?
Let us use the popular websocket library:
const WebSocket = require('ws')
const wsMessageListenerCps = url => cb =>
new WebSocket(url).on('message', cb)
And here is the crux:
wsMessageListenerCps(url)
is just another CPS function!
So we can simply drop it instead of readFileCps('name.txt')
into exactly the same code and be done with it:
const getLinesFromWS = CPS(wsMessageListenerCps(someUrl))
.map(file => file.trim())
.filter(file => file.length > 0)
.chain(file => readFileCps(file))
.map(text => text.split('\n'))
The new CPS function has only one callback, while the old one had two!
Yet we have used exactly the same code!
How so? Because we haven't done anything to other callbacks.
The only difference is in how the final function is called - with one callback instead of two.
As wsMessageListenerCps(url)
accepts one callback, so does getLinesFromWS
when we call it:
getLinesFromWS(lines => console.log(lines))
That will print all lines for all files whose names we receive from our websocket.
And if we feel overwhelmed and only want to see lines
containing say "breakfast", nothing can be easier:
const breakfastLines = CPS(getLinesFromWS)
.filter(line => /[Bb]reakfast/.test(line))
breakfastLines(lines => console.log(lines))
And from now on we'll never miss a breakfast.:smile:
CPS function
Any function
const cpsFn = (cb1, cb2, ...) => { ... }
that expects to be called with several (possibly zero) functions (callbacks) as arguments. The number of callbacks may vary each time cpsFn
is called. Once called and running, cpsFn
may call any of its callbacks any (possibly zero) number of times with any number m
of arguments (x1, ..., xm)
, where m
may also vary from call to call. The m
-tuple (vector) (x1, ..., xm)
is regarded as the output of cpsFn
from the n
th callback cbn
:
cbn(x1, ..., xm)
In other words, a CPS function receives any number of callbacks that it may call in any order any number of times at any moments immediately or in the future with any number of arguments.
API in brief
const { map, chain, filter, scan, ap, CPS, pipeline }
= require('cpsfy')
Each of the map
, chain
, filter
, scan
operators can be used in 3 ways:
map(f)(cpsFn)
CPS(cpsFn).map(f)
pipeline(cpsFn)(map(f))
The wrapped CPS function CPS(cpsFn)
has all operators available as methods, while it remains a plain CPS function, i.e. can be called with the same callbacks:
CPS(cpsFn)(f1, f2, ...)
cpsFn(f1, f2, ...)
pipeline(...arguments)(...functions)
Pass any number of arguments to a sequence of functions,
one after another, similar to the UNIX pipe
(x1, ..., xn) | f1 | f2 | ... | fm
.
Allows to write functional composition in
the intiutive linear way.
Examples of pipeline
pipeline(1, 2)(
(x, y) => x + y,
sum => sum * 2,
doubleSum => -doubleSum
)
chaining
The CPS
factory provides the same methods for simple chaining:
CPS(cpsFn).map(f).chain(g).filter(h)
However, the need to wrap a plain function might feel as overhead.
The pipeline
operator allows to write the same code
in functional style without the need to wrap:
pipeline(cpsFn)(
map(f),
chain(g),
filter(h)
)
But the most important advantage of the pipeline
style is
that you can drop there arbitrary functions without
any need to patch object prototypes.
For instance, using the above example,
we can start our pipe with url
or even insert
some intermediate function to compute the correct url for us:
pipeline(path)(
path => 'https://' + path
url => {console.log(url); return url}
wsMessageListenerCps,
map(f),
chain(g),
filter(h)
)
map(...functions)(cpsFunction)
map(f1, f2, ...)(cpsFn)
CPS(cpsFn).map(f1, f2, ...)
pipeline(cpsFn)(map(f1, f2, ...))
For each n
, apply fn
to each output from the n
th callback of cpsFn
.
Result of applying map
New CPS function that calls its n
th callback cbn
as
cbn(fn(x1, x2, ...))
whenever cpsFn
calls its n
th callback.
Example of map
Using readFileCps
as above.
const getCaps = map(str => str.toUpperCase())(
readFileCps('message.txt')
)
const getCaps = CPS(readFileCps('message.txt'))
.map(str => str.toUpperCase())
const getCaps = pipeline(readFileCps('message.txt'))(
map(str => str.toUpperCase())
)
getCaps((err, data) => err
? console.error(err)
: console.log(data)
)
chain(...functions)(cpsFunction)
chain(f1, f2, ...)(cpsFn)
CPS(cpsFn).chain(f1, f2, ...)
pipeline(cpsFn)(chain(f1, f2, ...))
where each fn
is a curried function
const fn = (x1, x2, ...) => (cb1, cb2, ...) => { ... }
The chain
operator applies each fn
to each output from the n
th callback of cpsFn
, however, the CPS ouptup of fn
is passed ahead instead of the return value.
Result of applying chain
New CPS function newCpsFn
that calls fn(x1, x2, ...)
whenever cpsFn
passes output (x1, x2, ...)
into its n
th callback, and collects all outputs from all callbacks of all fn
s. Then for each fixed m
, outputs from the m
th callbacks of all fn
s are collected and passed into the m
th callback cbm
of newCpsFn
:
cbm(y1, y2, ...)
cbmFn(y1, y2, ...)
Example of chain
Using readFileCps
as above.
const writeFileCps = (file, content) => (onRes, onErr) =>
require('fs').writeFile(file, content, (err, message) => {
err ? onErr(err) : onRes(message)
})
const copy = chain(
text => writeFileCps('target.txt', text)
)(
readFileCps('source.txt')
)
const copy = CPS(readFileCps('source.txt'))
.chain(text => writeFileCps('target.txt', text))
const copy = pipeline(readFileCps('source.txt'))(
chain(text => writeFileCps('target.txt', text))
)
copy((err, data) => err
? console.error(err)
: console.log(data)
)
filter(...predicates)(cpsFunction)
filter(pred1, pred2, ...)(cpsFn)
CPS(cpsFn).filter(pred1, pred2, ...)
pipeline(cpsFn)(filter(pred1, pred2, ...))
where each predn
is the n
th predicate function used to filter output from the n
th callback of cpsFn
.
Result of applying filter
New CPS function that calls its n
th callback cbn(x1, x2, ...)
whenever (x1, x2, ...)
is an output from the n
th callback of cpsFun
and
predn(x1, x2, ...) == true
Example of filter
Using readFileCps
and writeFileCps
as above.
const copyNotEmpty = CPS(readFileCps('source.txt'))
.filter(text => text.length > 0)
.chain(text => writeFileCps('target.txt', text))
copyNotEmpty(err => console.error(err))
scan(...initStates)(...reducers)(cpsFunction)
Similar to reduce
, except that each accumulated values is passed into its callback whenever there is new output.
scan(ini1, init2, ...)(red1, red2, ...)(cpsFn)
(cpsFn).scan(ini1, init2, ...)(red1, red2, ...)
pipeline(cpsFn)(scan(ini1, init2, ...)(red1, red2, ...))
where each redn
is a reducer and init
is the initial accumulated value.
const redn = (acc, y1, y2, ...) => ...
Result of applying scan
New CPS function whose function whose outputs are the accumulated values.
For each output (y1, y2, ...)
from the n
th callback,
the n
th reducer redn
is used to compute the new acculated value
redn(acc, y1, y2, ...)
, where acc
starts with the n
th initial state initStaten
, similar to reduce
.
Example of scan
const getVotes = (onUpvote, onDownvote) => {
upvoteButton.addEventListener('click',
ev => onUpvote(1)
)
downvoteButton.addEventListener('click',
ev => onDownvote(1)
)
}
const countVotes = CPS(getVotes)
.scan(0,0)(
([up, down], upvote) => [up + upvote, down],
([up, down], downvote) => [up, down + downvote]
)
countVotes(
upvotes => console.log('Total upvotes: ', upvotes),
downvotes => console.log('Total downvotes: ', downvotes),
)
ap(...cpsFunctions)(cpsFunction)
See running CPS functions in parallel.
Inspired by the Applicative Functor interface, see e.g. https://funkia.github.io/jabz/#ap
lift(...functions)(cpsFunction)
(TODO)
See lifting functions of multiple arguments
The "sister" of ap
, apply functions with multiple arguments to
outputs of CPS functions running in parallel, derived from ap
,
see e.g. https://funkia.github.io/jabz/#lift
merge(...cpsFunctions)
(TODO)
See CPS.merge
.
Merge outputs from multiple CPS functions, separately in each callback.
E.g. separately merge results and errors from multiple promises
running in parallel.
More details?
This README.md
is kept minimal to reduce the package size. For more human introduction, motivation, use cases and other details, please see DOCUMENTATION.
License
MIT © Dmitri Zaitsev