版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 1 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**
  2. * The `Eq` type class represents types which support decidable equality.
  3. *
  4. * Instances must satisfy the following laws:
  5. *
  6. * 1. Reflexivity: `E.equals(a, a) === true`
  7. * 2. Symmetry: `E.equals(a, b) === E.equals(b, a)`
  8. * 3. Transitivity: if `E.equals(a, b) === true` and `E.equals(b, c) === true`, then `E.equals(a, c) === true`
  9. *
  10. * @since 2.0.0
  11. */
  12. import { Contravariant1 } from './Contravariant'
  13. import { Monoid } from './Monoid'
  14. import { ReadonlyRecord } from './ReadonlyRecord'
  15. import { Semigroup } from './Semigroup'
  16. /**
  17. * @category model
  18. * @since 2.0.0
  19. */
  20. export interface Eq<A> {
  21. readonly equals: (x: A, y: A) => boolean
  22. }
  23. /**
  24. * @category constructors
  25. * @since 2.0.0
  26. */
  27. export declare const fromEquals: <A>(equals: (x: A, y: A) => boolean) => Eq<A>
  28. /**
  29. * @since 2.10.0
  30. */
  31. export declare const struct: <A>(eqs: { [K in keyof A]: Eq<A[K]> }) => Eq<{ readonly [K_1 in keyof A]: A[K_1] }>
  32. /**
  33. * Given a tuple of `Eq`s returns a `Eq` for the tuple
  34. *
  35. * @example
  36. * import { tuple } from 'fp-ts/Eq'
  37. * import * as S from 'fp-ts/string'
  38. * import * as N from 'fp-ts/number'
  39. * import * as B from 'fp-ts/boolean'
  40. *
  41. * const E = tuple(S.Eq, N.Eq, B.Eq)
  42. * assert.strictEqual(E.equals(['a', 1, true], ['a', 1, true]), true)
  43. * assert.strictEqual(E.equals(['a', 1, true], ['b', 1, true]), false)
  44. * assert.strictEqual(E.equals(['a', 1, true], ['a', 2, true]), false)
  45. * assert.strictEqual(E.equals(['a', 1, true], ['a', 1, false]), false)
  46. *
  47. * @since 2.10.0
  48. */
  49. export declare const tuple: <A extends readonly unknown[]>(...eqs: { [K in keyof A]: Eq<A[K]> }) => Eq<Readonly<A>>
  50. /**
  51. * A typical use case for `contramap` would be like, given some `User` type, to construct an `Eq<User>`.
  52. *
  53. * We can do so with a function from `User -> X` where `X` is some value that we know how to compare
  54. * for equality (meaning we have an `Eq<X>`)
  55. *
  56. * For example, given the following `User` type, we want to construct an `Eq<User>` that just looks at the `key` field
  57. * for each user (since it's known to be unique).
  58. *
  59. * If we have a way of comparing `UUID`s for equality (`eqUUID: Eq<UUID>`) and we know how to go from `User -> UUID`,
  60. * using `contramap` we can do this
  61. *
  62. * @example
  63. * import { contramap, Eq } from 'fp-ts/Eq'
  64. * import { pipe } from 'fp-ts/function'
  65. * import * as S from 'fp-ts/string'
  66. *
  67. * type UUID = string
  68. *
  69. * interface User {
  70. * readonly key: UUID
  71. * readonly firstName: string
  72. * readonly lastName: string
  73. * }
  74. *
  75. * const eqUUID: Eq<UUID> = S.Eq
  76. *
  77. * const eqUserByKey: Eq<User> = pipe(
  78. * eqUUID,
  79. * contramap((user) => user.key)
  80. * )
  81. *
  82. * assert.deepStrictEqual(
  83. * eqUserByKey.equals(
  84. * { key: 'k1', firstName: 'a1', lastName: 'b1' },
  85. * { key: 'k2', firstName: 'a1', lastName: 'b1' }
  86. * ),
  87. * false
  88. * )
  89. * assert.deepStrictEqual(
  90. * eqUserByKey.equals(
  91. * { key: 'k1', firstName: 'a1', lastName: 'b1' },
  92. * { key: 'k1', firstName: 'a2', lastName: 'b1' }
  93. * ),
  94. * true
  95. * )
  96. *
  97. * @since 2.0.0
  98. */
  99. export declare const contramap: <A, B>(f: (b: B) => A) => (fa: Eq<A>) => Eq<B>
  100. /**
  101. * @category type lambdas
  102. * @since 2.0.0
  103. */
  104. export declare const URI = 'Eq'
  105. /**
  106. * @category type lambdas
  107. * @since 2.0.0
  108. */
  109. export declare type URI = typeof URI
  110. declare module './HKT' {
  111. interface URItoKind<A> {
  112. readonly [URI]: Eq<A>
  113. }
  114. }
  115. /**
  116. * @category instances
  117. * @since 2.5.0
  118. */
  119. export declare const eqStrict: Eq<unknown>
  120. /**
  121. * @category instances
  122. * @since 2.10.0
  123. */
  124. export declare const getSemigroup: <A>() => Semigroup<Eq<A>>
  125. /**
  126. * @category instances
  127. * @since 2.6.0
  128. */
  129. export declare const getMonoid: <A>() => Monoid<Eq<A>>
  130. /**
  131. * @category instances
  132. * @since 2.7.0
  133. */
  134. export declare const Contravariant: Contravariant1<URI>
  135. /**
  136. * Use [`tuple`](#tuple) instead.
  137. *
  138. * @category zone of death
  139. * @since 2.0.0
  140. * @deprecated
  141. */
  142. export declare const getTupleEq: <T extends ReadonlyArray<Eq<any>>>(
  143. ...eqs: T
  144. ) => Eq<{
  145. [K in keyof T]: T[K] extends Eq<infer A> ? A : never
  146. }>
  147. /**
  148. * Use [`struct`](#struct) instead.
  149. *
  150. * @category zone of death
  151. * @since 2.0.0
  152. * @deprecated
  153. */
  154. export declare const getStructEq: <O extends ReadonlyRecord<string, any>>(eqs: {
  155. [K in keyof O]: Eq<O[K]>
  156. }) => Eq<O>
  157. /**
  158. * Use [`eqStrict`](#eqstrict) instead
  159. *
  160. * @category zone of death
  161. * @since 2.0.0
  162. * @deprecated
  163. */
  164. export declare const strictEqual: <A>(a: A, b: A) => boolean
  165. /**
  166. * This instance is deprecated, use small, specific instances instead.
  167. * For example if a function needs a `Contravariant` instance, pass `E.Contravariant` instead of `E.eq`
  168. * (where `E` is from `import E from 'fp-ts/Eq'`)
  169. *
  170. * @category zone of death
  171. * @since 2.0.0
  172. * @deprecated
  173. */
  174. export declare const eq: Contravariant1<URI>
  175. /**
  176. * Use [`Eq`](./boolean.ts.html#eq) instead.
  177. *
  178. * @category zone of death
  179. * @since 2.0.0
  180. * @deprecated
  181. */
  182. export declare const eqBoolean: Eq<boolean>
  183. /**
  184. * Use [`Eq`](./string.ts.html#eq) instead.
  185. *
  186. * @category zone of death
  187. * @since 2.0.0
  188. * @deprecated
  189. */
  190. export declare const eqString: Eq<string>
  191. /**
  192. * Use [`Eq`](./number.ts.html#eq) instead.
  193. *
  194. * @category zone of death
  195. * @since 2.0.0
  196. * @deprecated
  197. */
  198. export declare const eqNumber: Eq<number>
  199. /**
  200. * Use [`Eq`](./Date.ts.html#eq) instead.
  201. *
  202. * @category zone of death
  203. * @since 2.0.0
  204. * @deprecated
  205. */
  206. export declare const eqDate: Eq<Date>