Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added A.differenceWith #106

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions __tests__/Array/differenceWith.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { expectType } from 'ts-expect'

import { A, D, F, O, flow, pipe } from '../..'

type Obj = {
readonly a?: number
readonly b?: number
readonly c?: number
}

describe('differenceWith', () => {
it('provides correct types', () => {
expectType<ReadonlyArray<string>>(
A.differenceWith(['', 'hello', 'world'],[''], F.identity),
)
})

it('returns a correct result', () => {
expect(
A.differenceWith<Obj>([{ a: 1 }, { b: 2 }],[{ a: 1 }, { c: 3 }], F.identity),
).toEqual([{ b: 2 }])
expect(
A.differenceWith<{a:number}>([{a:1}, {a:2}], [{a:1}], D.get('a'))
).toEqual([{a:2}])
expect(
A.differenceWith([{a:{a1:1}}, {a:{a1:2}}], [{a:{a1:1}}], D.get('a'))
).toEqual([{a:{a1:2}}])
expect(
A.differenceWith([{a:{a1:1}}, {a:{a1:2}}], [{a:{a1:1}}], flow(D.get('a'),O.flatMap(D.get('a1'))))
).toEqual([{a:{a1:2}}])
})

it('*', () => {
expect(A.differenceWith([1, 2, 3, 4],[3, 4, 5, 6], F.identity)).toEqual([1, 2])
})
})

describe('difference (pipe)', () => {
it('provides correct types', () => {
expectType<ReadonlyArray<number>>(
pipe([5, 2, 3, 5, 6], A.differenceWith([5, 2, 3, 1, 5, 4],F.identity)),
)
})

it('*', () => {
expect(pipe([5, 2, 3, 5, 6], A.differenceWith([5, 2, 3, 1, 5, 4],F.identity))).toEqual([6])
})
})
15 changes: 15 additions & 0 deletions docs/api/generated/_array.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,21 @@ A.difference([1, 2, 3, 4], [3, 4, 5, 6]) // → [1, 2]
pipe([5, 2, 3, 5, 6], A.difference([5, 2, 3, 1, 5, 4])) // → [6]
```

### differenceWith

Returns elements from the first array, not existing in the second array, based on a comparison function.


```ts
function differenceWith<A>(xs: Array<A>, ys: Array<A>, fn: (x: A) => unknown): Array<A>
function differenceWith<A>(ys: Array<A>, fn: (x: A) => unknown): (xs: Array<A>) => Array<A>
```

```ts
A.differenceWith([1, 2, 3, 4],[3, 4, 5, 6], F.equals) // → [1, 2]
pipe([5, 2, 3, 5, 6], A.differenceWith([5, 2, 3, 1, 5, 4],F.equals)) // → [6]
```

### drop

Returns a new array that does not contain the first `n` elements of the provided array, or an empty array if `n` is either less than `0` or greater than the length of the provided array.
Expand Down
9 changes: 9 additions & 0 deletions src/Array/Array.res
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,15 @@ let any = (xs, predicateFn) => Belt.Array.someU(xs, (. value) => predicateFn(val
@gentype
let difference = (xs, ys) => xs->uniq->reject(value => includes(ys, value))

%comment(
"Returns elements from the first array, not existing in the second array, based on a comparison function."
)
@gentype
let differenceWith = (xs, ys, selectFn) => {
let _ys = ys->uniq->Belt.Array.map(selectFn)
xs->uniq->reject(value => _ys->Belt.Array.someU((. y) => y == selectFn(value)))
}

%comment("Returns union of two arrays.")
@gentype
let union = (xs, ys) => xs->concat(ys)->uniq
Expand Down
5 changes: 5 additions & 0 deletions src/Array/Array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,8 @@ export declare function flatMap<A, B>(
xs: Array<A>,
fn: (value: A) => B | Array<B>,
): Array<B>
export declare function differenceWith<A>(
xs: Array<A>,
ys: Array<A>,
fn: (x: A) => unknown,
): Array<A>
13 changes: 13 additions & 0 deletions src/Array/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,19 @@ export declare function difference<A>(ys: Array<A>): (xs: Array<A>) => Array<A>

export declare function difference<A>(xs: Array<A>, ys: Array<A>): Array<A>

/** Returns elements from the first array, not existing in the second array, based on a comparison function. */

export declare function differenceWith<A>(
xs: Array<A>,
ys: Array<A>,
fn: (x: A) => unknown,
): Array<A>

export declare function differenceWith<A>(
ys: Array<A>,
fn: (x: A) => unknown,
): (xs: Array<A>) => Array<A>

/** Returns union of two arrays. */

export declare function union<A>(ys: Array<A>): (xs: Array<A>) => Array<A>
Expand Down