Skip to content

Commit f3bbbb4

Browse files
authored
Honor the Array.unionWith callback contract (#9696)
1 parent 95752db commit f3bbbb4

4 files changed

Lines changed: 66 additions & 13 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect": patch
3+
---
4+
5+
Fix `Array.unionWith` calling custom equivalence callbacks with values from the wrong input or in the wrong order.

‎packages/effect/src/Array.ts‎

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3252,14 +3252,23 @@ const hashBucketsHas = (buckets: HashBuckets, value: unknown): boolean => {
32523252
}
32533253

32543254
/**
3255-
* Computes the union of two arrays using a custom equivalence, removing
3256-
* duplicates.
3255+
* Computes the union of two iterables using a custom equivalence.
32573256
*
32583257
* **When to use**
32593258
*
32603259
* Use when you need the union of two arrays but duplicate detection must use a
32613260
* custom equivalence instead of the default `Equal.asEquivalence()`.
32623261
*
3262+
* **Details**
3263+
*
3264+
* Keeps the elements of `self`, then appends elements of `that` that have no
3265+
* equivalent in `self`. Calls `isEquivalent` with an element from `self` first
3266+
* and an element from `that` second.
3267+
*
3268+
* **Gotchas**
3269+
*
3270+
* Each input must already be deduplicated.
3271+
*
32633272
* **Example** (Computing unions with custom equality)
32643273
*
32653274
* ```ts import.meta.vitest
@@ -3292,22 +3301,34 @@ export const unionWith: {
32923301
isEquivalent: (self: A, that: B) => boolean
32933302
): NonEmptyArray<A | B>
32943303
<A, B>(self: Iterable<A>, that: Iterable<B>, isEquivalent: (self: A, that: B) => boolean): Array<A | B>
3295-
} = dual(3, <A>(self: Iterable<A>, that: Iterable<A>, isEquivalent: (self: A, that: A) => boolean): Array<A> => {
3304+
} = dual(3, <A, B>(self: Iterable<A>, that: Iterable<B>, isEquivalent: (self: A, that: B) => boolean): Array<A | B> => {
32963305
const a = fromIterable(self)
32973306
const b = fromIterable(that)
32983307
if (isReadonlyArrayNonEmpty(a)) {
32993308
if (isReadonlyArrayNonEmpty(b)) {
3300-
const dedupe = dedupeWith(isEquivalent)
3301-
return dedupe(appendAll(a, b))
3309+
const out: Array<A | B> = [...a]
3310+
next: for (const value of b) {
3311+
for (const candidate of a) {
3312+
if (isEquivalent(candidate, value)) {
3313+
continue next
3314+
}
3315+
}
3316+
out.push(value)
3317+
}
3318+
return out
33023319
}
33033320
return a
33043321
}
33053322
return b
33063323
})
33073324

33083325
/**
3309-
* Computes the union of two arrays, removing duplicates using
3310-
* `Equal.asEquivalence()`.
3326+
* Computes the union of two iterables using `Equal.asEquivalence()`.
3327+
*
3328+
* **Gotchas**
3329+
*
3330+
* Each input must already be deduplicated. Use {@link dedupe} to remove
3331+
* duplicates from an input first.
33113332
*
33123333
* **Example** (Computing array unions)
33133334
*

‎packages/effect/src/Chunk.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2233,7 +2233,12 @@ export const takeWhile: {
22332233
})
22342234

22352235
/**
2236-
* Creates a Chunks of unique values, in order, from all given Chunks.
2236+
* Computes the union of two chunks using Effect equality.
2237+
*
2238+
* **Gotchas**
2239+
*
2240+
* Each input must already be deduplicated. Use {@link dedupe} to remove
2241+
* duplicates from an input first.
22372242
*
22382243
* **Example** (Unioning chunks)
22392244
*
@@ -2243,11 +2248,6 @@ export const takeWhile: {
22432248
* const chunk1 = Chunk.make(1, 2, 3)
22442249
* const chunk2 = Chunk.make(3, 4, 5)
22452250
* Chunk.toArray(Chunk.union(chunk1, chunk2)) // => [1, 2, 3, 4, 5]
2246-
*
2247-
* // Handles duplicates within the same chunk
2248-
* const withDupes1 = Chunk.make(1, 1, 2)
2249-
* const withDupes2 = Chunk.make(2, 3, 3)
2250-
* Chunk.toArray(Chunk.union(withDupes1, withDupes2)) // => [1, 2, 3]
22512251
* ```
22522252
*
22532253
* @stability stable

‎packages/effect/test/Array.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,33 @@ describe("Array", () => {
12881288
)
12891289
})
12901290

1291+
it("unionWith compares only self elements with that elements in argument order", () => {
1292+
const isEquivalent = (self: string, that: number) => self.trim() === that.toFixed()
1293+
deepStrictEqual(Arr.unionWith([" 2 ", " 1 "], [1, 3, 2, 4], isEquivalent), [" 2 ", " 1 ", 3, 4])
1294+
})
1295+
1296+
it("unionWith supports one-shot iterables", () => {
1297+
const isEquivalent = (self: string, that: number) => self.trim() === that.toFixed()
1298+
deepStrictEqual(
1299+
pipe([" 2 ", " 1 "][Symbol.iterator](), Arr.unionWith([1, 3, 2, 4][Symbol.iterator](), isEquivalent)),
1300+
[" 2 ", " 1 ", 3, 4]
1301+
)
1302+
})
1303+
1304+
it("unionWith does not call the equivalence when either input is empty", () => {
1305+
const isEquivalent = (_self: string, _that: number) => {
1306+
assert.fail("The equivalence must not be called for an empty input")
1307+
}
1308+
deepStrictEqual(Arr.unionWith([], [1, 2], isEquivalent), [1, 2])
1309+
deepStrictEqual(Arr.unionWith(["1", "2"], [], isEquivalent), ["1", "2"])
1310+
deepStrictEqual(Arr.unionWith([], [], isEquivalent), [])
1311+
})
1312+
1313+
it("unionWith treats holes in self as undefined", () => {
1314+
const self = globalThis.Array<undefined>(1)
1315+
deepStrictEqual(Arr.unionWith(self, [undefined], Equivalence.strictEqual<undefined>()), [undefined])
1316+
})
1317+
12911318
it("intersectionWith", () => {
12921319
const intersectionWith = Arr.intersectionWith(Equivalence.strictEqual<number>())
12931320
deepStrictEqual(pipe([1, 2], intersectionWith([3, 4])), [])

0 commit comments

Comments
 (0)