Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide Eq instance (getEq) #1

Open
justin-hackin opened this issue Jun 25, 2021 · 4 comments
Open

Provide Eq instance (getEq) #1

justin-hackin opened this issue Jun 25, 2021 · 4 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@justin-hackin
Copy link

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
Copy link

m-bock commented Jun 28, 2021

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 m-bock changed the title Equality operator Provide Eq instance (getEq) Jun 28, 2021
@m-bock m-bock added enhancement New feature or request good first issue Good for newcomers labels Jun 28, 2021
@m-bock
Copy link

m-bock commented Jun 28, 2021

Also created this one:
no-day/fp-ts-number-instances#1

@justin-hackin
Copy link
Author

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
Copy link

m-bock commented Jul 1, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants