Skip to content

Commit

Permalink
Chunk difference methods (#2658)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim <hello@timsmart.co>
  • Loading branch information
2 people authored and gcanti committed May 13, 2024
1 parent 99d4669 commit eb7bb28
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-garlics-wonder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": minor
---

add Chunk.difference & Chunk.differenceWith
30 changes: 30 additions & 0 deletions packages/effect/src/Chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1387,3 +1387,33 @@ export const reduceRight: {
<B, A>(b: B, f: (b: B, a: A, i: number) => B): (self: Chunk<A>) => B
<A, B>(self: Chunk<A>, b: B, f: (b: B, a: A, i: number) => B): B
} = RA.reduceRight

/**
* Creates a `Chunk` of values not included in the other given `Chunk` using the provided `isEquivalent` function.
* The order and references of result values are determined by the first `Chunk`.
*
* @since 3.2.0
*/
export const differenceWith = <A>(isEquivalent: (self: A, that: A) => boolean): {
(that: Chunk<A>): (self: Chunk<A>) => Chunk<A>
(self: Chunk<A>, that: Chunk<A>): Chunk<A>
} => {
return dual(
2,
(self: Chunk<A>, that: Chunk<A>): Chunk<A> => unsafeFromArray(RA.differenceWith(isEquivalent)(that, self))
)
}

/**
* Creates a `Chunk` of values not included in the other given `Chunk`.
* The order and references of result values are determined by the first `Chunk`.
*
* @since 3.2.0
*/
export const difference: {
<A>(that: Chunk<A>): (self: Chunk<A>) => Chunk<A>
<A>(self: Chunk<A>, that: Chunk<A>): Chunk<A>
} = dual(
2,
<A>(self: Chunk<A>, that: Chunk<A>): Chunk<A> => unsafeFromArray(RA.difference(that, self))
)
23 changes: 23 additions & 0 deletions packages/effect/test/Chunk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ describe("Chunk", () => {
expect(Chunk.unsafeFromNonEmptyArray).exist
expect(Chunk.contains).exist
expect(Chunk.containsWith).exist
expect(Chunk.difference).exist
expect(Chunk.differenceWith).exist
expect(Chunk.findFirst).exist
expect(Chunk.findFirstIndex).exist
expect(Chunk.findLast).exist
Expand Down Expand Up @@ -874,4 +876,25 @@ describe("Chunk", () => {
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)
})

it("differenceWith", () => {
const eq = <E extends { id: number }>(a: E, b: E) => a.id === b.id
const diffW = pipe(eq, Chunk.differenceWith)

const curr = Chunk.make({ id: 1 }, { id: 2 }, { id: 3 })

expect(diffW(Chunk.make({ id: 1 }, { id: 2 }), curr)).toEqual(Chunk.make({ id: 3 }))
expect(diffW(Chunk.empty(), curr)).toEqual(curr)
expect(diffW(curr, Chunk.empty())).toEqual(Chunk.empty())
expect(diffW(curr, curr)).toEqual(Chunk.empty())
})

it("difference", () => {
const curr = Chunk.make(1, 3, 5, 7, 9)

expect(Chunk.difference(Chunk.make(1, 2, 3, 4, 5), curr)).toEqual(Chunk.make(7, 9))
expect(Chunk.difference(Chunk.empty(), curr)).toEqual(curr)
expect(Chunk.difference(curr, Chunk.empty())).toEqual(Chunk.empty())
expect(Chunk.difference(curr, curr)).toEqual(Chunk.empty())
})
})

0 comments on commit eb7bb28

Please sign in to comment.