Skip to content

Commit

Permalink
Chunk: getEquivalence, resolve index out-of-bounds error when compari… (
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti authored Oct 31, 2023
1 parent 2b42d77 commit e0ef641
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/tiny-balloons-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Chunk: getEquivalence, resolve index out-of-bounds error when comparing chunks of different lengths
4 changes: 3 additions & 1 deletion src/Chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ const emptyArray: ReadonlyArray<never> = []
* @since 2.0.0
*/
export const getEquivalence = <A>(isEquivalent: Equivalence.Equivalence<A>): Equivalence.Equivalence<Chunk<A>> =>
Equivalence.make((self, that) => toReadonlyArray(self).every((value, i) => isEquivalent(value, unsafeGet(that, i))))
Equivalence.make((self, that) =>
self.length === that.length && toReadonlyArray(self).every((value, i) => isEquivalent(value, unsafeGet(that, i)))
)

const _equivalence = getEquivalence(Equal.equals)

Expand Down
2 changes: 1 addition & 1 deletion src/internal/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const moduleVersion = "2.0.0-next.51"
export const moduleVersion = "2.0.0-next.52"
10 changes: 9 additions & 1 deletion test/Chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as Duration from "effect/Duration"
import * as E from "effect/Either"
import * as Equal from "effect/Equal"
import { identity, pipe } from "effect/Function"
import * as N from "effect/Number"
import * as Option from "effect/Option"
import * as Order from "effect/Order"
import type { Predicate } from "effect/Predicate"
Expand All @@ -26,7 +27,6 @@ describe.concurrent("Chunk", () => {
expect(Chunk.reduce).exist
expect(Chunk.reduceRight).exist
expect(Chunk.some).exist
expect(Chunk.getEquivalence).exist
})

it("toString", () => {
Expand Down Expand Up @@ -854,4 +854,12 @@ describe.concurrent("Chunk", () => {
const chunk: Chunk.Chunk<X> = Chunk.make({ a: "a", b: 2 }, { a: "b", b: 1 })
expect(Chunk.sortWith(chunk, (x) => x.b, Order.number)).toEqual(Chunk.make({ a: "b", b: 1 }, { a: "a", b: 2 }))
})

it("getEquivalence", () => {
const equivalence = Chunk.getEquivalence(N.Equivalence)
expect(equivalence(Chunk.empty(), Chunk.empty())).toBe(true)
expect(equivalence(Chunk.make(1, 2, 3), Chunk.make(1, 2, 3))).toBe(true)
expect(equivalence(Chunk.make(1, 2, 3), Chunk.make(1, 2))).toBe(false)
expect(equivalence(Chunk.make(1, 2, 3), Chunk.make(1, 2, 4))).toBe(false)
})
})

0 comments on commit e0ef641

Please sign in to comment.