Navigation Menu

Skip to content

Commit

Permalink
add overlodings to zip, zipWith and unzip, closes #1109
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Feb 13, 2020
1 parent 7937019 commit 01623f2
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 6 deletions.
15 changes: 12 additions & 3 deletions docs/modules/Array.ts.md
Expand Up @@ -1687,7 +1687,10 @@ The function is reverse of `zip`. Takes an array of pairs and return two corresp
**Signature**

```ts
export const unzip: <A, B>(as: Array<[A, B]>) => [Array<A>, Array<B>] = ...
export const unzip: {
<A, B>(as: NonEmptyArray<[A, B]>): [NonEmptyArray<A>, NonEmptyArray<B>]
<A, B>(as: Array<[A, B]>): [Array<A>, Array<B>]
} = ...
```

**Example**
Expand Down Expand Up @@ -1740,7 +1743,10 @@ longer array are discarded
**Signature**

```ts
export const zip: <A, B>(fa: Array<A>, fb: Array<B>) => Array<[A, B]> = ...
export const zip: {
<A, B>(fa: NonEmptyArray<A>, fb: NonEmptyArray<B>): NonEmptyArray<[A, B]>
<A, B>(fa: Array<A>, fb: Array<B>): Array<[A, B]>
} = ...
```

**Example**
Expand All @@ -1765,7 +1771,10 @@ input array is short, excess elements of the longer array are discarded.
**Signature**

```ts
export const zipWith: <A, B, C>(fa: Array<A>, fb: Array<B>, f: (a: A, b: B) => C) => Array<C> = ...
export const zipWith: {
<A, B, C>(fa: NonEmptyArray<A>, fb: NonEmptyArray<B>, f: (a: A, b: B) => C): NonEmptyArray<C>
<A, B, C>(fa: Array<A>, fb: Array<B>, f: (a: A, b: B) => C): Array<C>
} = ...
```

**Example**
Expand Down
12 changes: 12 additions & 0 deletions docs/modules/ReadonlyArray.ts.md
Expand Up @@ -1720,6 +1720,9 @@ The function is reverse of `zip`. Takes an array of pairs and return two corresp
**Signature**

```ts
export function unzip<A, B>(
as: ReadonlyNonEmptyArray<readonly [A, B]>
): readonly [ReadonlyNonEmptyArray<A>, ReadonlyNonEmptyArray<B>]
export function unzip<A, B>(as: ReadonlyArray<readonly [A, B]>): readonly [ReadonlyArray<A>, ReadonlyArray<B>] { ... }
```

Expand Down Expand Up @@ -1773,6 +1776,10 @@ longer array are discarded
**Signature**

```ts
export function zip<A, B>(
fa: ReadonlyNonEmptyArray<A>,
fb: ReadonlyNonEmptyArray<B>
): ReadonlyNonEmptyArray<readonly [A, B]>
export function zip<A, B>(fa: ReadonlyArray<A>, fb: ReadonlyArray<B>): ReadonlyArray<readonly [A, B]> { ... }
```

Expand All @@ -1798,6 +1805,11 @@ input array is short, excess elements of the longer array are discarded.
**Signature**

```ts
export function zipWith<A, B, C>(
fa: ReadonlyNonEmptyArray<A>,
fb: ReadonlyNonEmptyArray<B>,
f: (a: A, b: B) => C
): ReadonlyNonEmptyArray<C>
export function zipWith<A, B, C>(fa: ReadonlyArray<A>, fb: ReadonlyArray<B>, f: (a: A, b: B) => C): ReadonlyArray<C> { ... }
```

Expand Down
30 changes: 30 additions & 0 deletions dtslint/ts3.5/Array.ts
@@ -0,0 +1,30 @@
import * as _ from '../../src/Array'
import { NonEmptyArray } from '../../src/NonEmptyArray'

declare const ns: Array<number>
declare const ss: Array<string>
declare const nens: NonEmptyArray<number>
declare const sess: NonEmptyArray<string>
declare const tns: Array<[number, string]>
declare const netns: NonEmptyArray<[number, string]>

//
// zip
//

_.zip(ns, ss) // $ExpectType [number, string][]
_.zip(nens, sess) // $ExpectType NonEmptyArray<[number, string]>

//
// zipWith
//

_.zipWith(ns, ss, (n, s) => [n, s] as const) // $ExpectType (readonly [number, string])[]
_.zipWith(nens, sess, (n, s) => [n, s] as const) // $ExpectType NonEmptyArray<readonly [number, string]>

//
// unzip
//

_.unzip(tns) // $ExpectType [number[], string[]]
_.unzip(netns) // $ExpectType [NonEmptyArray<number>, NonEmptyArray<string>]
30 changes: 30 additions & 0 deletions dtslint/ts3.5/ReadonlyArray.ts
@@ -0,0 +1,30 @@
import * as _ from '../../src/ReadonlyArray'
import { ReadonlyNonEmptyArray } from '../../src/ReadonlyNonEmptyArray'

declare const ns: ReadonlyArray<number>
declare const ss: ReadonlyArray<string>
declare const nens: ReadonlyNonEmptyArray<number>
declare const sess: ReadonlyNonEmptyArray<string>
declare const tns: ReadonlyArray<readonly [number, string]>
declare const netns: ReadonlyNonEmptyArray<readonly [number, string]>

//
// zip
//

_.zip(ns, ss) // $ExpectType readonly (readonly [number, string])[]
_.zip(nens, sess) // $ExpectType ReadonlyNonEmptyArray<readonly [number, string]>

//
// zipWith
//

_.zipWith(ns, ss, (n, s) => [n, s] as const) // $ExpectType readonly (readonly [number, string])[]
_.zipWith(nens, sess, (n, s) => [n, s] as const) // $ExpectType ReadonlyNonEmptyArray<readonly [number, string]>

//
// unzip
//

_.unzip(tns) // $ExpectType readonly [readonly number[], readonly string[]]
_.unzip(netns) // $ExpectType readonly [ReadonlyNonEmptyArray<number>, ReadonlyNonEmptyArray<string>]
15 changes: 12 additions & 3 deletions src/Array.ts
Expand Up @@ -671,7 +671,10 @@ export const sort: <A>(O: Ord<A>) => (as: Array<A>) => Array<A> = RA.sort as any
*
* @since 2.0.0
*/
export const zipWith: <A, B, C>(fa: Array<A>, fb: Array<B>, f: (a: A, b: B) => C) => Array<C> = RA.zipWith as any
export const zipWith: {
<A, B, C>(fa: NonEmptyArray<A>, fb: NonEmptyArray<B>, f: (a: A, b: B) => C): NonEmptyArray<C>
<A, B, C>(fa: Array<A>, fb: Array<B>, f: (a: A, b: B) => C): Array<C>
} = RA.zipWith as any

/**
* Takes two arrays and returns an array of corresponding pairs. If one input array is short, excess elements of the
Expand All @@ -684,7 +687,10 @@ export const zipWith: <A, B, C>(fa: Array<A>, fb: Array<B>, f: (a: A, b: B) => C
*
* @since 2.0.0
*/
export const zip: <A, B>(fa: Array<A>, fb: Array<B>) => Array<[A, B]> = RA.zip as any
export const zip: {
<A, B>(fa: NonEmptyArray<A>, fb: NonEmptyArray<B>): NonEmptyArray<[A, B]>
<A, B>(fa: Array<A>, fb: Array<B>): Array<[A, B]>
} = RA.zip as any

/**
* The function is reverse of `zip`. Takes an array of pairs and return two corresponding arrays
Expand All @@ -696,7 +702,10 @@ export const zip: <A, B>(fa: Array<A>, fb: Array<B>) => Array<[A, B]> = RA.zip a
*
* @since 2.0.0
*/
export const unzip: <A, B>(as: Array<[A, B]>) => [Array<A>, Array<B>] = RA.unzip as any
export const unzip: {
<A, B>(as: NonEmptyArray<[A, B]>): [NonEmptyArray<A>, NonEmptyArray<B>]
<A, B>(as: Array<[A, B]>): [Array<A>, Array<B>]
} = RA.unzip as any

/**
* Rotate an array to the right by `n` steps
Expand Down
15 changes: 15 additions & 0 deletions src/ReadonlyArray.ts
Expand Up @@ -990,6 +990,12 @@ export function sort<A>(O: Ord<A>): (as: ReadonlyArray<A>) => ReadonlyArray<A> {
*
* @since 2.5.0
*/
export function zipWith<A, B, C>(
fa: ReadonlyNonEmptyArray<A>,
fb: ReadonlyNonEmptyArray<B>,
f: (a: A, b: B) => C
): ReadonlyNonEmptyArray<C>
export function zipWith<A, B, C>(fa: ReadonlyArray<A>, fb: ReadonlyArray<B>, f: (a: A, b: B) => C): ReadonlyArray<C>
export function zipWith<A, B, C>(fa: ReadonlyArray<A>, fb: ReadonlyArray<B>, f: (a: A, b: B) => C): ReadonlyArray<C> {
// tslint:disable-next-line: readonly-array
const fc: Array<C> = []
Expand All @@ -1011,6 +1017,11 @@ export function zipWith<A, B, C>(fa: ReadonlyArray<A>, fb: ReadonlyArray<B>, f:
*
* @since 2.5.0
*/
export function zip<A, B>(
fa: ReadonlyNonEmptyArray<A>,
fb: ReadonlyNonEmptyArray<B>
): ReadonlyNonEmptyArray<readonly [A, B]>
export function zip<A, B>(fa: ReadonlyArray<A>, fb: ReadonlyArray<B>): ReadonlyArray<readonly [A, B]>
export function zip<A, B>(fa: ReadonlyArray<A>, fb: ReadonlyArray<B>): ReadonlyArray<readonly [A, B]> {
return zipWith(fa, fb, (a, b) => [a, b])
}
Expand All @@ -1025,6 +1036,10 @@ export function zip<A, B>(fa: ReadonlyArray<A>, fb: ReadonlyArray<B>): ReadonlyA
*
* @since 2.5.0
*/
export function unzip<A, B>(
as: ReadonlyNonEmptyArray<readonly [A, B]>
): readonly [ReadonlyNonEmptyArray<A>, ReadonlyNonEmptyArray<B>]
export function unzip<A, B>(as: ReadonlyArray<readonly [A, B]>): readonly [ReadonlyArray<A>, ReadonlyArray<B>]
export function unzip<A, B>(as: ReadonlyArray<readonly [A, B]>): readonly [ReadonlyArray<A>, ReadonlyArray<B>] {
// tslint:disable-next-line: readonly-array
const fa: Array<A> = []
Expand Down

0 comments on commit 01623f2

Please sign in to comment.