no-day / fp-ts-sized-vectors

Fixed size generic vector type carrying its length at the typelevel.
7 stars 0 forks source link

Provide Eq instance (getEq) #1

Open justin-hackin opened 3 years ago

justin-hackin commented 3 years ago

I'm very new to fp-ts so I hope I'm not overlooking something obvious here. I'm looking for how to implement a fp-ts compatible equality operation for the sized vectors. I tried this

export type Point2D = Vec<2, number>;

export const point = (x: number, y: number): Point2D => vec(x, y);
const eqNumber: eq.Eq<number> = {
  equals: (x, y) => x === y,
};

export const eqPoint: eq.Eq<Point2D> = array.getEq(eqNumber);

and this complains that a number[] can't be assigned to Vec<2, number>. Is there a particular reason why this library doesn't seem to implement equality for the vectors?

m-bock commented 3 years ago

Hi!

The Vec type is implemented as readonly array. Just tried your code snippet and it works if you import * as array from "fp-ts/ReadonlyArray"

No, there was no specific reason to not to include a getEq into the library. I guess some others are missing, too.

I think it would be something like this:

const getEq: <A>(E: Eq<A>) => <N extends number>() => Eq<Vec<N, A>> = (
  E
) => () => array.getEq(E)

Thanks for posting this. PR's are always welcome :)

m-bock commented 3 years ago

Also created this one: https://github.com/no-day/fp-ts-number-instances/issues/1

justin-hackin commented 3 years ago

Thanks for letting me know about read-only array feature. I decided that I only needed one size and thus I opted out of this library for now. Glad it was worth marking this in the number library.

m-bock commented 3 years ago

Yes, this makes sense. I actually had in mind to create a library that provides the fixed sized vectors. (Vec2, Vec3). What exists though is one that has the non-generic Vec2n, Vec3n types: https://github.com/no-day/fp-ts-numeric-vectors (unfortunately Eq and other instances missing, too)

In future there should be easy conversion between those types by adding fromTuple/toTuple everywere.