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

@danielnarey/pair

v0.2.2

Published

[Deprecated] A tiny functional data structure for pairs of arbitrary values

Downloads

32

Readme

@danielnarey/pair

[Deprecated] A tiny functional data structure for pairs of arbitrary values

Deprecation Warning: This experimental library is no longer in active development and will not be updated in response to Node.js version releases or security vulnerabilities identified in the dependency tree.

Purpose

When you have two values that you want to join with the same reference or return from a function, using a pair gives you immutability, along with a clean functional pattern for updating one or both values to a new reference.

Examples

import pair from '@danielnarey/pair';
// OR: const pair = require('@danielnarey/pair');

// constructor
const p = pair.of('🍐', 42);
typeof(p); //--> 'function'

// accessors
pair.first(p);  //--> '🍐'
pair.second(p); //--> 42

// conversion
pair.toArray(p); //--> ['🍐', 42]
pair.toString(p); //--> '(🍐 . 42)'

// functional transforms
const q = pair.mapFirst(p, (s) => `${s}🍎`);
const r = pair.mapSecond(q, (n) => n + 1);

// immutability
pair.toString(p); //--> '(🍐 . 42)'
pair.toString(q); //--> '(🍐🍎 . 42)'
pair.toString(r); //--> '(🍐🍎 . 43)'

API

of(a, b) => (a . b)

Join two arbitrary values a and b as a pair, returning a functional interface to the paired values (denoted as (a . b)).

from([a, b]) => (a . b)

Take the first two values of an array (or other iterable) and join them as a pair.

first(p) => a

Returns the first value of a pair.

second(p) => b

Returns the second value of a pair.

mapFirst(p, f) => (f(a) . b)

Apply a function to the first value of a pair, returning an interface to the new pair.

mapSecond(p, f) => (a . f(b))

Apply a function to the second value of a pair.

mapEach(p, f1, f2) => (f1(a) . f2(b))

Apply a function to the each value of a pair.

mapBoth(p, f) => (f(a) . f(b))

Apply a single function to both values of a pair.

reduce(p, reducer, [initial]) => result

Reduce (fold) a pair from the left and return the result. The reducer should be a function of the form (accumulator, current) => intermediate. If specified, the initial value is the value of accumulator on the first pass. If not specified, the reducer executes only once with the first and second values of the pair as its arguments.

reduceRight(p, reducer, [initial]) => result

Reduce (fold) a pair from the right and return the result. Equivalent to reduce with the order of the pair reversed.

reverse(p) => (b . a)

Reverse the order of a pair of values, returning an interface to the reordered pair.

toArray(p) => [a, b]

Returns the paired values as a length-2 array.

toString(p) => '(a . b)'

Returns a string representing a pair.

Prior Art