Skip to content

Commit

Permalink
relax sort signature
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Jul 22, 2020
1 parent 0e90550 commit c152b5b
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ high state of flux, you're at risk of it changing without notice.
- `ReadonlyArray`
- fix `FunctorWithIndex` instance name (@gcanti)
- fix `Functor` instance name (@gcanti)
- **Polish**
- `Array`
- relax `sort` signature (@gcanti)
- `ReadonlyArray`
- relax `sort` signature (@gcanti)

# 2.7.0

Expand Down
2 changes: 1 addition & 1 deletion docs/modules/Array.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ Sort the elements of an array in increasing order, creating a new array
**Signature**

```ts
export declare const sort: <A>(O: Ord<A>) => (as: A[]) => A[]
export declare const sort: <B>(O: Ord<B>) => <A extends B>(as: A[]) => A[]
```
**Example**
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/ReadonlyArray.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ Sort the elements of an array in increasing order, creating a new array
**Signature**

```ts
export declare function sort<A>(O: Ord<A>): (as: ReadonlyArray<A>) => ReadonlyArray<A>
export declare const sort: <B>(O: Ord<B>) => <A extends B>(as: readonly A[]) => readonly A[]
```

**Example**
Expand Down
2 changes: 1 addition & 1 deletion src/Array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ export const lefts: <E, A>(as: Array<Either<E, A>>) => Array<E> = RA.lefts as an
* @category combinators
* @since 2.0.0
*/
export const sort: <A>(O: Ord<A>) => (as: Array<A>) => Array<A> = RA.sort as any
export const sort: <B>(O: Ord<B>) => <A extends B>(as: Array<A>) => Array<A> = RA.sort as any

/**
* Apply a function to pairs of elements at the same index in two arrays, collecting the results in a new array. If one
Expand Down
5 changes: 2 additions & 3 deletions src/ReadonlyArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -973,9 +973,8 @@ export function lefts<E, A>(as: ReadonlyArray<Either<E, A>>): ReadonlyArray<E> {
* @category combinators
* @since 2.5.0
*/
export function sort<A>(O: Ord<A>): (as: ReadonlyArray<A>) => ReadonlyArray<A> {
return (as) => (isEmpty(as) ? as : as.slice().sort(O.compare))
}
export const sort = <B>(O: Ord<B>) => <A extends B>(as: ReadonlyArray<A>): ReadonlyArray<A> =>
isEmpty(as) ? as : as.slice().sort(O.compare)

// TODO: curry and make data-last in v3
/**
Expand Down
17 changes: 17 additions & 0 deletions test/Array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,23 @@ describe('Array', () => {

it('sort', () => {
assert.deepStrictEqual(_.sort(Ord.ordNumber)([3, 2, 1]), [1, 2, 3])
assert.strictEqual(_.sort(Ord.ordNumber)(_.empty), _.empty)
const byName = pipe(
Ord.ordString,
Ord.contramap((x: { readonly name: string }) => x.name)
)
assert.deepStrictEqual(
_.sort(byName)([
{ name: 'b', age: 0 },
{ name: 'a', age: 1 },
{ name: 'c', age: 2 }
]),
[
{ name: 'a', age: 1 },
{ name: 'b', age: 0 },
{ name: 'c', age: 2 }
]
)
})

it('zipWith', () => {
Expand Down
16 changes: 16 additions & 0 deletions test/ReadonlyArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,22 @@ describe('ReadonlyArray', () => {
it('sort', () => {
assert.deepStrictEqual(_.sort(Ord.ordNumber)([3, 2, 1]), [1, 2, 3])
assert.strictEqual(_.sort(Ord.ordNumber)(_.empty), _.empty)
const byName = pipe(
Ord.ordString,
Ord.contramap((x: { readonly name: string }) => x.name)
)
assert.deepStrictEqual(
_.sort(byName)([
{ name: 'b', age: 0 },
{ name: 'a', age: 1 },
{ name: 'c', age: 2 }
]),
[
{ name: 'a', age: 1 },
{ name: 'b', age: 0 },
{ name: 'c', age: 2 }
]
)
})

it('zipWith', () => {
Expand Down

0 comments on commit c152b5b

Please sign in to comment.