title | nav_order | parent |
---|---|---|
Eq.ts |
28 |
Modules |
The Eq
type class represents types which support decidable equality.
Instances must satisfy the following laws:
- Reflexivity:
E.equals(a, a) === true
- Symmetry:
E.equals(a, b) === E.equals(b, a)
- Transitivity: if
E.equals(a, b) === true
andE.equals(b, c) === true
, thenE.equals(a, c) === true
Added in v2.0.0
Signature
export declare const fromEquals: <A>(equals: (x: A, y: A) => boolean) => Eq<A>
Added in v2.0.0
Signature
export declare const Contravariant: Contravariant1<'Eq'>
Added in v2.7.0
Signature
export declare const eqStrict: Eq<unknown>
Added in v2.5.0
Signature
export declare const getMonoid: <A>() => Monoid<Eq<A>>
Added in v2.6.0
Signature
export declare const getSemigroup: <A>() => Semigroup<Eq<A>>
Added in v2.10.0
Signature
export interface Eq<A> {
readonly equals: (x: A, y: A) => boolean
}
Added in v2.0.0
Signature
export declare const URI: 'Eq'
Added in v2.0.0
Signature
export type URI = typeof URI
Added in v2.0.0
A typical use case for contramap
would be like, given some User
type, to construct an Eq<User>
.
We can do so with a function from User -> X
where X
is some value that we know how to compare
for equality (meaning we have an Eq<X>
)
For example, given the following User
type, we want to construct an Eq<User>
that just looks at the key
field
for each user (since it's known to be unique).
If we have a way of comparing UUID
s for equality (eqUUID: Eq<UUID>
) and we know how to go from User -> UUID
,
using contramap
we can do this
Signature
export declare const contramap: <A, B>(f: (b: B) => A) => (fa: Eq<A>) => Eq<B>
Example
import { contramap, Eq } from 'fp-ts/Eq'
import { pipe } from 'fp-ts/function'
import * as S from 'fp-ts/string'
type UUID = string
interface User {
readonly key: UUID
readonly firstName: string
readonly lastName: string
}
const eqUUID: Eq<UUID> = S.Eq
const eqUserByKey: Eq<User> = pipe(
eqUUID,
contramap((user) => user.key)
)
assert.deepStrictEqual(
eqUserByKey.equals({ key: 'k1', firstName: 'a1', lastName: 'b1' }, { key: 'k2', firstName: 'a1', lastName: 'b1' }),
false
)
assert.deepStrictEqual(
eqUserByKey.equals({ key: 'k1', firstName: 'a1', lastName: 'b1' }, { key: 'k1', firstName: 'a2', lastName: 'b1' }),
true
)
Added in v2.0.0
Signature
export declare const struct: <A>(eqs: { [K in keyof A]: Eq<A[K]> }) => Eq<{ readonly [K in keyof A]: A[K] }>
Added in v2.10.0
Given a tuple of Eq
s returns a Eq
for the tuple
Signature
export declare const tuple: <A extends readonly unknown[]>(...eqs: { [K in keyof A]: Eq<A[K]> }) => Eq<Readonly<A>>
Example
import { tuple } from 'fp-ts/Eq'
import * as S from 'fp-ts/string'
import * as N from 'fp-ts/number'
import * as B from 'fp-ts/boolean'
const E = tuple(S.Eq, N.Eq, B.Eq)
assert.strictEqual(E.equals(['a', 1, true], ['a', 1, true]), true)
assert.strictEqual(E.equals(['a', 1, true], ['b', 1, true]), false)
assert.strictEqual(E.equals(['a', 1, true], ['a', 2, true]), false)
assert.strictEqual(E.equals(['a', 1, true], ['a', 1, false]), false)
Added in v2.10.0
Use Eq
instead.
Signature
export declare const eqBoolean: Eq<boolean>
Added in v2.0.0
Use Eq
instead.
Signature
export declare const eqDate: Eq<Date>
Added in v2.0.0
Use Eq
instead.
Signature
export declare const eqNumber: Eq<number>
Added in v2.0.0
Use Eq
instead.
Signature
export declare const eqString: Eq<string>
Added in v2.0.0
This instance is deprecated, use small, specific instances instead.
For example if a function needs a Contravariant
instance, pass E.Contravariant
instead of E.eq
(where E
is from import E from 'fp-ts/Eq'
)
Signature
export declare const eq: Contravariant1<'Eq'>
Added in v2.0.0
Use struct
instead.
Signature
export declare const getStructEq: <O extends Readonly<Record<string, any>>>(eqs: { [K in keyof O]: Eq<O[K]> }) => Eq<O>
Added in v2.0.0
Use tuple
instead.
Signature
export declare const getTupleEq: <T extends readonly Eq<any>[]>(
...eqs: T
) => Eq<{ [K in keyof T]: T[K] extends Eq<infer A> ? A : never }>
Added in v2.0.0
Use eqStrict
instead
Signature
export declare const strictEqual: <A>(a: A, b: A) => boolean
Added in v2.0.0