npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

funprog

v1.18.3

Published

A simple library for functional programming

Downloads

113

Readme

Functional Programming Library

Why this library

I was specifically wanting to try using transducers to modify a stream of data generated from an asynchronous iterator.

e.g.

async function(){
    for await (const v of generator) {
        console.log(v)
    }
}

Where the generator is an async generator such as

async function * makeAsyncRangeIterator (start = 0, end = Infinity, step = 1, delay = 10) {
    let iterationCount = 0
    for (let i = start; i < end; i += step) {
        iterationCount++
        yield new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve(i)
            }, delay)
        })
    }
    return iterationCount
  }

or another example would be to process of stream of documents being inserted into a Mongo DB database

async function makeGenerator(config){
  
  const client = await mongoClient.connect(config.MONGO_SERVER);
  const collection = client.db(config.MONGO_DB).collection(config.MONGO_COLLECTION);

  return async function* () {
      const pipeline = [
          { $project : { 
                  "AssetId": "$fullDocument.AssetId", 
                  "Epoch": "$fullDocument.Epoch", 
                  "Value": "$fullDocument.Value", 
                  "SiteId": "$fullDocument.SiteId" } }
      ]
      const changeStream = collection.watch(pipeline,{
          startAtOperationTime: new Date( )
      });
  
      while(true){
          var change = await new Promise( (resolve) =>{
              changeStream.on("change", function(change) {
                  resolve(change)
              });
          })
          delete change._id
          yield change
      }
  }()
}

I found a number of functional programming libraries (tranducer.js, ramda) but found they didnt support asynchronous iterators. They appear to only support synchronous functionality.

How to use

npm install funprog

Then use the functionality that you require (which is visible in index.js)

const { compose, transduceArray, transduceIterator,mapping, filtering } = require('funprog')

Functions

Operators

There are a number of operators which generally take a value and return another value

  • not,
  • identity,
  • isEven,
  • isGreaterThan,
  • digitize,
  • modulus

Stream Transforms

The transforms operate the stream of values

  • mapping - produce a transformed value for each value in the stream
  • assign - produce a value with additional properties for each value in the stream
  • filtering - remove some items from a stream based on a predicate
  • take - stop generator when a limit is reached
  • skip - skip values until a threshold is acheived
  • eventing - combines a set of measurements (single point in time) into events (which have a start/end/duration)
  • sampling - Sample at a lower frequence than the values are being produced
  • passthrough - map each event to itself
  • split - maps from a stream of N events into a stream of N+ events by splitting one event into multiple
  • randomFilter - randomly includes values at a predefined frequency
  • neighbors - make n to n + m future stream events available as nth event is processed

Generators

There are a number of functions to produce generators

  • makeArrayIterator,
  • makeAsyncRangeIterator,
  • makeAsyncHasNextRangeIterator

Transducers

There are a set of transducer functions. A transducer is a function taking 4 parameters

  • a transform,
  • a reducer,
  • an initial response ,
  • a generator

The transducer reads from the generator transforms the value and reduces using the reduce function and initial accumulation

  • transduceAsyncIterator,
  • transduceAsyncHasNextIterator,
  • transduceArray

Composition

Functions that compose functions together

  • compose - reduce payload processed by array of functions
  • asyncCompose - reduce payload processed by array of functions returning promises

Tests

Use npm start to run the unit tests

Explanations

Further explanations