All Projects → dmitriz → cpsfy

dmitriz / cpsfy

Licence: MIT license
🚀 Tiny goodies for Continuation-Passing-Style functions, fully tested

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to cpsfy

functional-structures-refactoring-kata
Starting code and proposed solution for Functional Structures Refactoring Kata
Stars: ✭ 31 (-46.55%)
Mutual labels:  composition, monad, functor, applicative
future.scala
Stack-safe asynchronous programming
Stars: ✭ 38 (-34.48%)
Mutual labels:  asynchronous, continuation, monad
elixir-control
An exploratory look into functors, applicatives, and monads for Elixir
Stars: ✭ 21 (-63.79%)
Mutual labels:  monad, functor, applicative
aioudp
Asyncio UDP server
Stars: ✭ 21 (-63.79%)
Mutual labels:  asynchronous, asynchronous-tasks, asynchronous-programming
kudojs
A utility library to write code in functional programming style in Javascript
Stars: ✭ 22 (-62.07%)
Mutual labels:  composition, functor, applicative
Crocks
A collection of well known Algebraic Data Types for your utter enjoyment.
Stars: ✭ 1,501 (+2487.93%)
Mutual labels:  composition, monad, functor
function-composition-cheatsheet
Composition of Functions
Stars: ✭ 24 (-58.62%)
Mutual labels:  composition, monad, functor
Language Ext
C# functional language extensions - a base class library for functional programming
Stars: ✭ 3,964 (+6734.48%)
Mutual labels:  monad, functor, applicative
do
Simplest way to manage asynchronicity
Stars: ✭ 33 (-43.1%)
Mutual labels:  asynchronous, composition, callback
Bow
🏹 Bow is a cross-platform library for Typed Functional Programming in Swift
Stars: ✭ 538 (+827.59%)
Mutual labels:  composition, monad, functor
Metasync
Asynchronous Programming Library for JavaScript & Node.js
Stars: ✭ 164 (+182.76%)
Mutual labels:  asynchronous, composition, callback
debugging-async-operations-in-nodejs
Example code to accompany my blog post on debugging async operations in Node.js.
Stars: ✭ 22 (-62.07%)
Mutual labels:  asynchronous, asynchronous-programming
UnderstandingLanguageExt
This is a tutorial that aims to demonstrate the practical fundamentals behind using LanguageExt in a fashion though step-by-step tutorials which introduce and then build up on concepts.
Stars: ✭ 73 (+25.86%)
Mutual labels:  composition, monad
Tributary
Streaming reactive and dataflow graphs in Python
Stars: ✭ 231 (+298.28%)
Mutual labels:  stream, asynchronous
J-Curry
A Java library that enables applying Functional Programming concepts like currying and partial application for functions, also it supports types like Either, Try, etc... using RxJava 2 interfaces, compatible with Java 7 and above
Stars: ✭ 17 (-70.69%)
Mutual labels:  monad, functor
java-red
Effective Concurrency Modules for Java
Stars: ✭ 25 (-56.9%)
Mutual labels:  asynchronous, asynchronous-tasks
zab
C++20 liburing backed coroutine executor and event loop framework.
Stars: ✭ 54 (-6.9%)
Mutual labels:  asynchronous, asynchronous-programming
EnumerableAsyncProcessor
Process Multiple Asynchronous Tasks in Various Ways - One at a time / Batched / Rate limited / Concurrently
Stars: ✭ 84 (+44.83%)
Mutual labels:  asynchronous, asynchronous-programming
async
Asynchronous programming for R -- async/await and generators/yield
Stars: ✭ 37 (-36.21%)
Mutual labels:  asynchronous, asynchronous-programming
RingBuffer
Classic ringbuffer with optional Stream interface
Stars: ✭ 53 (-8.62%)
Mutual labels:  stream, asynchronous
                                //  ) )     
    ___      ___      ___   __//__         
  //   ) ) //   ) ) ((   ) ) //   //   / / 
 //       //___/ /   \ \    //   ((___/ /  
((____   //       //   ) ) //        / /   

(Generated with http://patorjk.com/software/taag)

cpsfy

npm version install size bundlephobia Build Status CircleCI Depfu Known Vulnerabilities Coverage Status codecov Language grade: JavaScript CodeFactor codebeat badge Maintainability DeepScan grade GitHub last commit npm downloads MIT License

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.

//function returning CPS function with 2 callbacks
const readFileCps = file => (onRes, onErr) =>  
  require('fs').readFile(file, (err, content) => {
    err ? onErr(err) : onRes(content)
  })

// CPS wraps a CPS function to provide the API methods
const getLines = CPS(readFileCps('name.txt'))
  // map applies function to the file content
  .map(file => file.trim()) 
  .filter(file => file.length > 0)
  // chain applies function that returns CPS function
  .chain(file => readFileCps(file))
  .map(text => text.split('\n'))
// => CPS function with 2 callbacks

// To use, simply pass callbacks in the same order
getLines(
  lines => console.log(lines),  // onRes callback
  err => console.error(err)  // onErr callback
)

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, shall we? At least not as easy. And maybe ... not really another.😉

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')
// general purpose CPS function listening to websocket
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'))

And if you paid attention, 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:

// just add a filter
const breakfastLines = CPS(getLinesFromWS)
  .filter(line => /[Bb]reakfast/.test(line))
// call it exactly the same way
breakfastLines(lines => console.log(lines))

And from now on we'll never miss a breakfast.😄

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 nth callback cbn:

// (x1, ..., xm) becomes output from nth callback whenever
cbn(x1, ..., xm)  // is called, where n = 1, 2, ..., m

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' as curried function
map(f)(cpsFn)
// 'map' method provided by the 'CPS' wrapper
CPS(cpsFn).map(f)
// 'cpsFn' is piped into 'map(f)' via 'pipeline' operator
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, ...) // is equivalent to
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
) //=> -6

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:

// pass cpsFn directly as argument
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)(               // begin with path
  path => 'https://' + path  // compute url on the spot
  url => {console.log(url); return url} // check for debugging
  wsMessageListenerCps,       // return CPS function
  map(f),                     // use CPS operators as usual
  chain(g),
  filter(h)
)

map(...functions)(cpsFunction)

// these are equivalent
map(f1, f2, ...)(cpsFn)
CPS(cpsFn).map(f1, f2, ...)
pipeline(cpsFn)(map(f1, f2, ...))

For each n, apply fn to each output from the nth callback of cpsFn.

Result of applying map

New CPS function that calls its nth callback cbn as

cbn(fn(x1, x2, ...))

whenever cpsFn calls its nth callback.

Example of map

Using readFileCps as above.

// read file and convert all letters to uppercase
const getCaps = map(str => str.toUpperCase())(
  readFileCps('message.txt')
)
// or
const getCaps = CPS(readFileCps('message.txt'))
  .map(str => str.toUpperCase())
// or
const getCaps = pipeline(readFileCps('message.txt'))(
  map(str => str.toUpperCase())
)

// getCaps is CPS function, call with any callback
getCaps((err, data) => err 
  ? console.error(err) 
  : console.log(data)
) // => file content is capitalized and printed

chain(...functions)(cpsFunction)

// these are equivalent
chain(f1, f2, ...)(cpsFn)
CPS(cpsFn).chain(f1, f2, ...)
pipeline(cpsFn)(chain(f1, f2, ...))

where each fn is a curried function

// fn is expected to return a CPS function
const fn = (x1, x2, ...) => (cb1, cb2, ...) => { ... }

The chain operator applies each fn to each output from the nth 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 nth callback, and collects all outputs from all callbacks of all fns. Then for each fixed m, outputs from the mth callbacks of all fns are collected and passed into the mth callback cbm of newCpsFn:

cbm(y1, y2, ...)  // is called whenever 
cbmFn(y1, y2, ...)  // is called where
// cbmFn is the mth callback of fn

Example of chain

Using readFileCps as above.

// write version of readFileCps
const writeFileCps = (file, content) => (onRes, onErr) =>  
  require('fs').writeFile(file, content, (err, message) => {
    err ? onErr(err) : onRes(message)
  })

const copy = chain(
  // function that returns CPS function
  text => writeFileCps('target.txt', text)
)(
  readFileCps('source.txt')  // CPS function
)
// or as method
const copy = CPS(readFileCps('source.txt'))
  .chain(text => writeFileCps('target.txt', text))
// or with pipeline operator
const copy = pipeline(readFileCps('source.txt'))(
  chain(text => writeFileCps('target.txt', text))
)

// copy is a CPS function, call it with any callback
copy((err, data) => err 
  ? console.error(err) 
  : console.log(data)
) // => file content is capitalized and printed

filter(...predicates)(cpsFunction)

// these are equivalent
filter(pred1, pred2, ...)(cpsFn)
CPS(cpsFn).filter(pred1, pred2, ...)
pipeline(cpsFn)(filter(pred1, pred2, ...))

where each predn is the nth predicate function used to filter output from the nth callback of cpsFn.

Result of applying filter

New CPS function that calls its nth callback cbn(x1, x2, ...) whenever (x1, x2, ...) is an output from the nth callback of cpsFun and

predn(x1, x2, ...) == true

Example of filter

Using readFileCps and writeFileCps as above.

// only copy text if it is not empty
const copyNotEmpty = CPS(readFileCps('source.txt'))
  .filter(text => text.length > 0)
  .chain(text => writeFileCps('target.txt', text))

// copyNotEmpty is CPS function, call with any callback
copyNotEmpty(err => console.error(err))

scan(...reducers, init)(cpsFunction)

Similar to reduce, except that all partial accumulated values are passed into callback whenever there is new output.

// these are equivalent
scan(red1, red2, ..., init)(cpsFn)
(cpsFn).scan(red1, red2, ..., init)
pipeline(cpsFn)(scan(red1, red2, ..., init))

where each redn is a reducer and init is the initial accumulated value.

// compute new accumulator value from the old one 
// and the tuple of current values (y1, y2, ...)
const redn = (acc, y1, y2, ...) => ... 

Result of applying scan

New CPS function whose output from the first callback is the accumulated value. For each output (y1, y2, ...) from the nth callback, the nth reducer redn is used to compute the new acculated value redn(acc, y1, y2, ...), where acc starts with init, similar to reduce.

Example of scan

// CPS function with 2 callbacks, clicking on one
// of the buttons sends '1' into respective callback
const getVotes = (onUpvote, onDownvote) => {
  upvoteButton.addEventListener('click', 
    ev => onUpvote(1)
  )
  downvoteButton.addEventListener('click', 
    ev => onDownvote(1)
  )  
}
// count numbers of up- and downvotes and 
// pass into respective callbacks
const countVotes = CPS(getVotes)
  .scan(
    ([up, down], upvote) => [up + upvote, down], 
    ([up, down], downvote) => [up, down + downvote],
    [0,0]
   )

// countVotes is CPS function that we can call 
// with any callback
countVotes(
  votes => console.log('Total votes: ', votes),
)

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].