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-sized-vectors

v0.1.0

Published

[API Docs](https://no-day.github.io/fp-ts-sized-vectors)

Downloads

2

Readme

fp-ts-sized-vectors

API Docs


Install

yarn add fp-ts fp-ts-sized-vectors

or

npm install fp-ts fp-ts-sized-vectors

Examples

Imports

import * as V from 'fp-ts-sized-vectors'
import * as N from 'fp-ts-number-instances'
import { pipe } from 'fp-ts/function'
import * as assert from 'assert'
import Vec = V.Vec

Construction

The main type of this library is Vec<N, A>. It defines a vector of any given length N containing components or fields of any type A.

Let's first create a vector of length 2, and we'll use number as the type for the fields. You can think of it as a two dimensional vector, but that's just one of many possible interpretations.

const vecA: Vec<2, number> = V.vec(10, 20)

assert.deepStrictEqual(vecA, [10, 20])

Then, let's create a vector of length 3, again using number for the fields.

const vecB: Vec<3, number> = V.vec(30, 40, 50)

assert.deepStrictEqual(vecB, [30, 40, 50])

It's important to keep in mind, that those two vectors have different types. And as you can see, behind the scenes those vectors are just plain arrays.

Operations on vectors

Many operations on vectors that are provided by this library are dependently typed. This means, that the output type changes depending on the input types.

A good example for this is the concat operation. We pass in our vector of length 2 as well as the one of length 3. What we get returned is a new vector of length 5.

const vecAB: Vec<5, number> = V.concat(vecA)(vecB)

assert.deepStrictEqual(vecAB, [10, 20, 30, 40, 50])

Lookups

The fact that the vectors carry their lengths as type level information gives us many useful compile time guarantees. Below you can see that we receive a number for in range indices and something of type unknown for out of bounds indices. That works because we specify the indices as number literals.

We do not receive any union types like number | undefined or Option<number>. That's one of the main advantages over the Array type.

Vec 2

const a_0: number = V.lookup(0)(vecA)
const a_1: number = V.lookup(1)(vecA)
const a_2: unknown = V.lookup(2)(vecA)

Vec 3

const b_0: number = V.lookup(0)(vecB)
const b_1: number = V.lookup(1)(vecB)
const b_2: number = V.lookup(2)(vecB)
const b_3: unknown = V.lookup(3)(vecB)

Vec 5

const ab_0: number = V.lookup(0)(vecAB)
const ab_1: number = V.lookup(1)(vecAB)
const ab_2: number = V.lookup(2)(vecAB)
const ab_3: number = V.lookup(3)(vecAB)
const ab_4: number = V.lookup(4)(vecAB)
const ab_5: unknown = V.lookup(5)(vecAB)

Vectors of anything

The size of vectors are captured as type level numbers, their inner value can be of arbitrary types. In the above example we used the number type as that's quite common. But we could also just define a vector of strings:

const vecStr: Vec<3, string> = V.vec('Hello', 'World', '!')

Unlike tuples, all components of a vector have to have the same type though.

Vector math

However, let's stay with the previously used Vec<N, number> as we can do more interesting things with them. As long as the inner type has some arithmetic qualities, we can apply calculations like additions or multiplications component wise:

import NS = N.Semiring

const vecR: Vec<3, number> = pipe(
  V.vec(1, 2, 3),
  V.mul(NS)(V.vec(2, 2, 2)),
  V.add(NS)(V.vec(0.1, 0.2, 0.3))
)

assert.deepStrictEqual(vecR, [2.1, 4.2, 6.3])

Limitations

  • Implementation of type level numbers ist constrained to very small numbers due to TypeScript's type level recursion limit.
  • The behavior of negative type level numbers and floats is unspecified.

TODO (PR's welcome)