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

fp-ts-foldl

v0.3.11

Published

This is a WIP port of the [`foldl`](https://hackage.haskell.org/package/foldl) library in Haskell. Most of the documentation here has been adapted from the Hackage docs.

Downloads

26

Readme

fp-ts-foldl

This is a WIP port of the foldl library in Haskell. Most of the documentation here has been adapted from the Hackage docs.

This library provides efficient left folds that you can combine using Applicative style.

Install

Uses fp-ts as a peer dependency.

yarn add fp-ts fp-ts-foldl

or

npm install fp-ts fp-ts-foldl

Tutorial

This tutorial assumes the following imports:

import * as L from "fp-ts-foldl";
import { pipe } from "fp-ts/function";
import * as N from "fp-ts/number";
import * as RA from "fp-ts/ReadonlyArray";
import * as RNEA from "fp-ts/ReadonlyNonEmptyArray";
import * as RR from "fp-ts/ReadonlyRecord";

A Fold is a representation of a left fold that preserves the fold's step function, initial accumulator, and extraction function. This allows the Applicative instance to assemble derived folds that traverse the container only once.

A Fold<A, B> processes elements of type A and results in a value of type B.

We can use the fold function to apply a Fold to a ReadonlyArray:

L.fold(RA.Foldable)(L.sum)([1, 2, 3]); //-> 6

fold works with any type that implements fp-ts's Foldable type class (or any type that has a reduce function matching the method from that class), but we can use foldArray for the common use case where the Foldable instance is for ReadonlyArray:

L.foldArray(L.sum)([1, 2, 3]);

Folds are Applicatives, so you can combine them using Applicative combinators:

// `average` has the inferred type `Fold<number, number>`
const average = pipe(
  L.Do,
  L.apS("sum", L.sum),
  L.apSW("length", L.length),
  L.map(({ sum, length }) => sum / length)
);

// Taking the sum, the sum of squares, ..., up to the sum of `x ** 5`
const powerSums = pipe(
  [1, 2, 3, 4, 5],
  RA.traverse(L.Applicative)(n =>
    pipe(
      L.sum,
      L.premap((x: number) => x ** n)
    )
  )
);
L.foldArray(powerSums)(RNEA.range(1, 10));
//-> [ 55, 385, 3025, 25333, 220825 ]

These combined folds will still traverse the array only once:

L.foldArray(average)(RNEA.range(1, 10_000_000));
//-> 5000000.5

pipe(
  RNEA.range(1, 10_000_000),
  L.foldArray(
    L.Do,
    L.apS("minimum", L.minimum(N.Ord)),
    L.apS("maximum", L.maximum(N.Ord))
  )
);
//-> { minimum: O.some(1), maximum: O.some(10_000_000) }

Now that we have the basics, let's look at a dataset of Flower measurements.

type Flower = {
  sepalLength: number;
  sepalWidth: number;
  petalLength: number;
  petalWidth: number;
  species: "setosa" | "versicolor" | "virginica";
};

const flowers = [
  {
    sepalLength: 5.1,
    sepalWidth: 3.5,
    petalLength: 1.4,
    petalWidth: 0.2,
    species: "setosa",
  },
  {
    sepalLength: 4.9,
    sepalWidth: 3,
    petalLength: 1.4,
    petalWidth: 0.2,
    species: "setosa",
  },
  {
    sepalLength: 4.7,
    sepalWidth: 3.2,
    petalLength: 1.3,
    petalWidth: 0.2,
    species: "setosa",
  },
  // ...
];

We can get the mean petal-length of all flowers:

pipe(
  flowers,
  L.foldArray(
    L.mean,
    L.premap((flower: Flower) => flower.petalLength),
    L.map(n => n.toPrecision(3)) // `map` transforms the final result of the `Fold`
  )
);
//-> "3.76"

We can also use prefilter to just look at the petal-lengths of the virginica species:

pipe(
  flowers,
  L.foldArray(
    L.mean,
    L.premap((flower: Flower) => flower.petalLength),
    L.prefilter(flower => flower.species === "virginica"),
    L.map(n => n.toPrecision(3))
  )
);
//-> "5.55"

Finally we can use Applicative combinators to get the standard deviation of all flower attributes, while only traversing the array once:

pipe(
  flowers,
  L.foldArray(
    L.Do,
    L.apS(
      "petalLength",
      pipe(
        L.std,
        L.premap((flower: Flower) => flower.petalLength)
      )
    ),
    L.apS(
      "petalWidth",
      pipe(
        L.std,
        L.premap(flower => flower.petalWidth)
      )
    ),
    L.apS(
      "sepalLength",
      pipe(
        L.std,
        L.premap(flower => flower.sepalLength)
      )
    ),
    L.apS(
      "sepalWidth",
      pipe(
        L.std,
        L.premap(flower => flower.sepalWidth)
      )
    ),
    L.map(RR.map(n => n.toPrecision(3)))
  )
);
//-> { petalLength: '1.76', petalWidth: '0.761', sepalLength: '0.825', sepalWidth: '0.432' }

Benchmarks

foldl performs favorably against transducer implementations in ramda and transducers-js in the following benchmarks. (Note that this library does not support early termination, unlike most transducer implementations, so will perform much worse in cases where that's a requirement. See: https://github.com/Gabriella439/foldl/issues/85.)

map, filter, sum on an Array of 1,000,000 numbers array4 (2)

map, filter, sum on an immutable/List of 1,000,000 numbers imm4 (1)

map, filter, sum on a funkia/List of 1,000,000 numbers funkia4 (1)