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

retrospective

v1.1.1

Published

A utility library for retrospective functions and types.

Downloads

872

Readme

retrospective

A chaining library to work with retrospective functions (aka next-chain-style) and types.


Installation

Install from npm:

npm i retrospective

Usage

The workhorse function is reduceChain, which collapses the given retrospective chain into an underlying function that can be executed:

import reduceChain, { RetrospectiveChain } from "retrospective-chain"

type MyNumberTransformer = (x: number) => number

const chain: RetrospectiveChain<MyNumberTransformer> = [
  (next: MyNumberTransformer, x: number) => next(x) + 1,
  (next: MyNumberTransformer, x: number) => next(x) ** 2
]

const result = reduceChain(chain, x => x)(5) // 26

Introduction

A retrospective function is a function which takes another function with an identical signature as itself (sans the parameter under discussion, i.e. the resultant type is non-recursive) as its first parameter:

export type RetrospectiveFunction<F extends (...args: any) => any> = (
  next: F,
  ...x: Parameters<F>
) => ReturnType<F>;

This pattern is useful in situations where you're expecting to perform a homogeneously typed operation on some set of data multiple times, e.g. a series of transformations, point-free code, etc. As well, this pattern works very well with async promises.

Here's an example of how you might use it to perform a set of sequential transformations on a number:

type MyNumberTransformer = (x: number) => number

const chain: RetrospectiveChain<MyNumberTransformer> = [
  (next: MyNumberTransformer, x: number) => next(x) + 1,
  (next: MyNumberTransformer, x: number) => next(x) ** 2
]

The above sequence roughly corresponds to the function x ** 2 + 1, but it divides this operation into component pieces.

This abstraction becomes useful for series of complicated, potentially asynchronous actions where you want higher chain elements to potentially preclude lower chain elements (or run them multiple times, catch errors, etc.).

You can bypass the homogeneous type requirement by having each executor operate on a large pre-typed context space, and then collapse individual results at the end. This corresponds to the pipeline pattern.

Etymology

Next-chain functions are nothing new - but since there are many conflicting variants, I'm using "retrospective chain" to specifically refer to the variant with the following attributes:

  • is well typed
  • is variadic
  • operates only on homogeneously typed functions

Execution

Retrospective chains are executed by first using the reduceChain utility - you also need to pass in the identity function corresponding to the type transformation you are using. Essentially, this is just the most internally-executed element and does not take in the next parameter:

type MyNumberTransformer = (x: number) => number

const chain: RetrospectiveChain<MyNumberTransformer> = [
  (next: MyNumberTransformer, x: number) => next(x) + 1,
  (next: MyNumberTransformer, x: number) => next(x) ** 2
]

const result = reduceChain(chain, x => x)(5) // 26

Trees of Execution

An interesting use-case is whereby individual executor functions call multiple instances of their next function: this creates branching trees of execution, where for the case of N executors calling their next functions M times, creates N ^ M unique branches.

This pattern is used for e.g. coordinating parallel asynchronous processing, perform graph search, backtracking methods, etc. If each executor layer memoizes itself, this pattern forms the basis of a very robust algorithmic engine.


Contribution

This library is probably rather feature-complete, but feel free to open a PR if there's anything I missed.